1
0
Fork 0

the various OSC argument types

This commit is contained in:
Kevin Matz 2023-04-23 11:32:52 -04:00
parent abe41404cc
commit d51d3163f9
3 changed files with 490 additions and 0 deletions

View File

@ -5,16 +5,23 @@ add_library(LCP::OSC ALIAS ${PROJECT_NAME})
target_sources(${PROJECT_NAME}
PUBLIC
argument.h
bundle.h
client.h
message.h
packet.h
PRIVATE
argument.cpp
bundle.cpp
client.cpp
message.cpp
packet.cpp
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
LCP::BufferStream
)
set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

151
protocol/osc/argument.cpp Normal file
View File

@ -0,0 +1,151 @@
/*
osc/argument.cpp
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "argument.h"
#include <cassert>
#include "message.h"
namespace OSC {
/**
* @brief Argument::type
* @return
*/
std::string Argument::type() const
{
return std::string(1, type_);
}
size_t string::streamSize() const
{
auto length = value.size(); // character count
length += 1; // null terminated
length += length % 4 ? 4 - (length % 4) : 0; // padding bytes
assert(0 == length % 4);
return length;
}
void string::iStream(std::shared_ptr<bufferstream> stream)
{
stream->readString(value);
auto pad = (value.size() + 1) % 4;
for (uint i = 0; i < pad; i++)
stream->readType<uint8_t>();
}
void string::oStream(std::shared_ptr<bufferstream> stream) const
{
stream->writeString(value);
auto pad = (value.size() + 1) % 4;
for (uint i = 0; i < pad; i++)
stream->writeType<uint8_t>(0x00);
}
size_t blob::streamSize() const
{
auto length = 4 + value.size(); // length + byte count
length += length % 4 ? 4 - (length % 4) : 0; // padding bytes
assert(0 == length % 4);
return length;
}
void blob::iStream(std::shared_ptr<bufferstream> stream)
{
auto length = stream->readType<int32_t>();
value.resize(length);
stream->read(value.data(), length);
auto pad = length % 4;
for (int i = 0; i < pad; i++)
stream->readType<uint8_t>();
}
void blob::oStream(std::shared_ptr<bufferstream> stream) const
{
stream->writeType<int32_t>(value.size());
auto pad = value.size() % 4;
for (uint i = 0; i < pad; i++)
stream->readType<uint8_t>();
}
void character::iStream(std::shared_ptr<bufferstream> stream)
{
value = stream->readType<int32_t>();
}
void character::oStream(std::shared_ptr<bufferstream> stream) const
{
stream->writeType(static_cast<int32_t>(value));
}
/**
* @brief array::setTypes
* @param types
*/
void array::setTypes(std::string types)
{
members = Message::createArguments(types);
}
std::string array::type() const
{
std::string ret = "[";
for (const auto &member: members)
ret += member->type();
ret += "]";
return ret;
}
size_t array::streamSize() const
{
size_t size = 0;
for (const auto &member: members)
size += member->streamSize();
return size;
}
void array::iStream(std::shared_ptr<bufferstream> stream)
{
for (auto & member: members)
member->iStream(stream);
}
void array::oStream(std::shared_ptr<bufferstream> stream) const
{
for (const auto &member: members)
member->oStream(stream);
}
} // namespace OSC

332
protocol/osc/argument.h Normal file
View File

@ -0,0 +1,332 @@
/*
osc/argument.h
Copyright (c) 2023 Kevin Matz (kevin.matz@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <bufferstream.h>
#include <vector>
namespace OSC {
/**
* @brief The Argument struct
*/
struct Argument
: public streamable
{
/**
* @brief Argument
* @param type
*/
Argument(char type) : type_(type) {};
virtual std::string type() const;
private:
char type_;
};
/**
* @brief The int32 class
*/
struct int32
: Argument
{
int32() : Argument('i') {};
virtual size_t streamSize() const override { return 4; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
int32_t value; //!< value of the integer
};
/**
* @brief The float32 class
*/
struct float32
: Argument
{
float32() : Argument('f') {};
virtual size_t streamSize() const override { return 4; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
float value; //!< value of the floating point number
};
/**
* @brief The string class
*/
struct string
: Argument
{
string() : Argument('s') {};
virtual size_t streamSize() const override;
virtual void iStream(std::shared_ptr<bufferstream>) override;
virtual void oStream(std::shared_ptr<bufferstream>) const override;
std::string value; //!< charecters of the string
};
/**
* @brief The blob class
*/
struct blob
: Argument
{
blob() : Argument('b') {};
virtual size_t streamSize() const override;
virtual void iStream(std::shared_ptr<bufferstream>) override;
virtual void oStream(std::shared_ptr<bufferstream>) const override;
std::vector<uint8_t> value; //!< blob data
};
/**
* @brief The int64 class
*/
struct int64
: Argument
{
int64() : Argument('h') {};
virtual size_t streamSize() const override { return 8; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
int64_t value; //!< value of the integer
};
/**
* @brief The timetag class
*/
struct timetag
: Argument
{
timetag() : Argument('t') {};
virtual size_t streamSize() const override { return 8; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
union {
uint64_t value;
struct {
uint32_t seconds; //!< the number of seconds since midnight on January 1, 1900
uint32_t fractional; //!< fractional parts of a second
};
};
};
/**
* @brief The timetag128 class
*
* Not described in OSC, the 128-bit NTPv4 format for times > Feb 7, 2036
*/
struct timetag128
: Argument
{
timetag128() : Argument('T') {};
virtual size_t streamSize() const override { return 16; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> seconds;
*stream >> fractional; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << seconds;
*stream << fractional; }
uint64_t seconds; //!< the number of seconds since midnight on January 1, 1900
uint64_t fractional; //!< fractional parts of a second
};
/**
* @brief The float64 class
*/
struct float64
: Argument
{
float64() : Argument('d') {};
virtual size_t streamSize() const override { return 8; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
double value; //!< value of the float
};
/**
* @brief The character class
*/
struct character
: Argument
{
character() : Argument('c') {};
virtual size_t streamSize() const override { return 4; }
virtual void iStream(std::shared_ptr<bufferstream>) override;
virtual void oStream(std::shared_ptr<bufferstream>) const override;
char value; //!< value of the argument
};
/**
* @brief The rgba class
*/
struct rgba
: Argument
{
rgba() : Argument('r') {};
virtual size_t streamSize() const override { return 4; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
union {
uint32_t value;
struct {
uint8_t red;
uint8_t blue;
uint8_t green;
uint8_t alpha;
};
};
};
/**
* @brief The midi class
*/
struct midi
: Argument
{
midi() : Argument('m') {};
virtual size_t streamSize() const override { return 4; }
virtual void iStream(std::shared_ptr<bufferstream> stream) override { *stream >> value; }
virtual void oStream(std::shared_ptr<bufferstream> stream) const override { *stream << value; }
union {
uint32_t value;
struct {
uint8_t port;
uint8_t status;
uint8_t data1;
uint8_t data2;
};
};
};
/**
* @brief The True class
*/
struct True
: Argument
{
True() : Argument('T') {};
virtual size_t streamSize() const override { return 0; }
virtual void iStream(std::shared_ptr<bufferstream>) override {};
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
const bool value = true; //!< value of the argument
};
/**
* @brief The False class
*/
struct False
: Argument
{
False() : Argument('F') {};
virtual size_t streamSize() const override { return 0; }
virtual void iStream(std::shared_ptr<bufferstream>) override {};
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
const bool value = false; //!< value of the argument
};
/**
* @brief The Null class
*/
struct Null
: Argument
{
Null() : Argument('N') {};
virtual size_t streamSize() const override { return 0; }
virtual void iStream(std::shared_ptr<bufferstream>) override {};
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
};
/**
* @brief The impulse class
*/
struct impulse
: Argument
{
impulse() : Argument('I') {};
virtual size_t streamSize() const override { return 0; }
virtual void iStream(std::shared_ptr<bufferstream>) override {};
virtual void oStream(std::shared_ptr<bufferstream>) const override {};
};
/**
* @brief The array class
*/
struct array
: Argument
{
array() : Argument('[') {};
void setTypes(std::string types);
virtual std::string type() const override;
virtual size_t streamSize() const override;
virtual void iStream(std::shared_ptr<bufferstream>) override;
virtual void oStream(std::shared_ptr<bufferstream>) const override;
std::vector<std::shared_ptr<Argument>> members; //!< components of the array
};
} // namespace OSC