1
0
Fork 0
OpenLCP/protocol/osc/message.cpp

195 lines
5.9 KiB
C++

/*
osc/message.cpp
Copyright (c) 2022 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 <cassert>
#include "message.h"
namespace OSC {
/**
* @brief Message::type_tag
* @return
*/
std::string Message::type_tag() const
{
std::string types = ",";
for (const auto & argument: arguments)
types += argument->type();
return types;
}
/**
* @brief Message::createArguments
* @param types
* @return
*/
std::vector<std::shared_ptr<Argument>> Message::createArguments(std::string types)
{
auto args = std::vector<std::shared_ptr<Argument>>();
for (std::string::size_type pos = 0; pos < types.size(); pos++)
{
switch (types.at(pos)) {
case 'i':
args.emplace_back(std::make_shared<int32>());
break;
case 'f':
args.emplace_back(std::make_shared<float32>());
break;
case 's':
case 'S':
args.emplace_back(std::make_shared<string>());
break;
case 'b':
args.emplace_back(std::make_shared<blob>());
break;
case 'h':
args.emplace_back(std::make_shared<int64>());
break;
case 't':
args.emplace_back(std::make_shared<timetag>());
break;
case 'd':
args.emplace_back(std::make_shared<float64>());
break;
case 'c':
args.emplace_back(std::make_shared<character>());
break;
case 'r':
args.emplace_back(std::make_shared<rgba>());
break;
case 'm':
args.emplace_back(std::make_shared<midi>());
break;
case 'T':
args.emplace_back(std::make_shared<True>());
break;
case 'F':
args.emplace_back(std::make_shared<False>());
break;
case 'N':
args.emplace_back(std::make_shared<Null>());
break;
case 'I':
args.emplace_back(std::make_shared<impulse>());
break;
case '[':
{
args.emplace_back(std::make_shared<array>());
auto arg = std::static_pointer_cast<array>(args.back());
std::string::size_type close = pos;
int depth = 1;
while (depth > 0)
{
close = types.find_first_of("[]", close);
if (close == std::string::npos) // no closing brace, use all the remaining types
{
close = types.size()-1;
break;
}
if (types.at(close) == '[') // nested braces, keep going
{
depth++;
continue;
}
if (types.at(close) == ']') // closed this level of nesting
{
depth--;
}
}
arg->setTypes(types.substr(pos+1, close-(pos+1)));
pos = close;
}
break;
default:
continue;
}
}
return args;
}
size_t Message::streamSize() const
{
size_t address_size = address_pattern.size(); // character count
address_size += 1; // null terminated
address_size += address_size % 4 ? 4 - (address_size % 4) : 0; // padding bytes
assert(0 == address_size % 4); // 32 bit aligned
size_t type_size = type_tag().size(); // character count
type_size += 1; // null terminated
type_size += type_size % 4 ? 4 - (type_size % 4) : 0; // padding
assert(0 == type_size % 4); // 32 bit aligned
size_t argument_size = 0;
for (const auto & argument: arguments)
argument_size += argument->streamSize(); // length of all arguments
assert(0 == argument_size % 4); // 32 bit aligned
return address_size + type_size + argument_size;
}
void Message::iStream(std::shared_ptr<bufferstream> stream)
{
stream->readString(address_pattern);
if (address_pattern.front() != '/')
return stream->setstate(std::ios::failbit);
for (uint i = 0; i < (address_pattern.size() + 1) % 4; i++)
stream->readType<uint8_t>(); // 32 bit aligned padding
std::string type_string;
stream->readString(type_string);
if (type_string.front() != ',')
return stream->setstate(std::ios::failbit);
for (uint i = 0; i < (type_string.size() + 1) % 4; i++)
stream->readType<uint8_t>(); // 32 bit aligned padding
arguments = createArguments(type_string);
for (auto &argument: arguments)
argument->iStream(stream);
}
void Message::oStream(std::shared_ptr<bufferstream> stream) const
{
stream->writeString(address_pattern);
auto address_pad = (address_pattern.size() + 1) % 4;
address_pad = address_pad ? 4 - address_pad : 0;
for (uint i = 0; i < address_pad; i++)
stream->writeType<uint8_t>(0x00); // 32 bit aligned padding
std::string type_string = type_tag();
stream->writeString(type_string);
auto type_pad = (type_string.size() + 1) % 4;
type_pad = type_pad ? 4 - type_pad : 0;
for (uint i = 0; i < type_pad; i++)
stream->writeType<uint8_t>(0x00); // 32 bit aligned padding
for (const auto &argument: arguments)
argument->oStream(stream);
}
} // namespace OSC