message and bundle I/O
This commit is contained in:
parent
d51d3163f9
commit
877ab3ed4c
@ -22,16 +22,63 @@
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include "bundle.h"
|
#include "bundle.h"
|
||||||
|
|
||||||
namespace OSC {
|
namespace OSC {
|
||||||
|
|
||||||
/**
|
size_t Bundle::streamSize() const
|
||||||
* @brief Bundle::Bundle
|
|
||||||
*/
|
|
||||||
Bundle::Bundle()
|
|
||||||
{
|
{
|
||||||
|
size_t size = 16; // "#bundle" + timetag
|
||||||
|
for (const auto &element: elements)
|
||||||
|
size += 4 + element->streamSize(); // element's length + element length
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Bundle::iStream(std::shared_ptr<bufferstream> stream)
|
||||||
|
{
|
||||||
|
std::string header;
|
||||||
|
stream->readString(header);
|
||||||
|
if (header.compare(address_pattern) != 0)
|
||||||
|
return stream->setstate(std::ios::failbit);
|
||||||
|
|
||||||
|
time_tag.iStream(stream);
|
||||||
|
|
||||||
|
while (stream->available() && stream->good())
|
||||||
|
{
|
||||||
|
int32_t length;
|
||||||
|
*stream >> length;
|
||||||
|
switch (stream->peek()) {
|
||||||
|
case '/':
|
||||||
|
elements.emplace_back(std::make_shared<Message>());
|
||||||
|
break;
|
||||||
|
case '#':
|
||||||
|
elements.emplace_back(std::make_shared<Bundle>());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
stream->setstate(std::ios::failbit);
|
||||||
|
}
|
||||||
|
if (!stream->good())
|
||||||
|
break;
|
||||||
|
|
||||||
|
elements.back()->iStream(stream);
|
||||||
|
if ((uint)length != elements.back()->streamSize())
|
||||||
|
return stream->setstate(std::ios::failbit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Bundle::oStream(std::shared_ptr<bufferstream> stream) const
|
||||||
|
{
|
||||||
|
stream->writeString(address_pattern);
|
||||||
|
time_tag.oStream(stream);
|
||||||
|
|
||||||
|
for (const auto &element: elements)
|
||||||
|
{
|
||||||
|
stream->writeType<int32_t>(element->streamSize());
|
||||||
|
element->oStream(stream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace OSC
|
} // namespace OSC
|
||||||
|
@ -23,15 +23,35 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "argument.h"
|
||||||
|
#include <bufferstream.h>
|
||||||
|
#include <memory>
|
||||||
|
#include "message.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace OSC {
|
namespace OSC {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The Bundle class
|
* @brief The Bundle struct
|
||||||
|
*
|
||||||
|
* \cite Spec10 An OSC Bundle consists of the OSC-string “#bundle” followed by an OSC Time Tag,
|
||||||
|
* followed by zero or more OSC Bundle Elements.
|
||||||
|
*
|
||||||
|
* Inherits from Message as a Bundle may also be a Bundle Element.
|
||||||
*/
|
*/
|
||||||
class Bundle
|
struct Bundle
|
||||||
|
: public Message
|
||||||
{
|
{
|
||||||
public:
|
///@brief Bundle
|
||||||
explicit Bundle();
|
Bundle() { address_pattern = "#bundle"; }
|
||||||
|
|
||||||
|
virtual size_t streamSize() const override;
|
||||||
|
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
||||||
|
virtual void oStream(std::shared_ptr<bufferstream>) const override;
|
||||||
|
|
||||||
|
timetag time_tag; //!< delay excution of elements until this time
|
||||||
|
std::vector<std::shared_ptr<Message>> elements; //!< bundled elements, either messages or bundles
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace OSC
|
} // namespace OSC
|
||||||
|
@ -22,16 +22,173 @@
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
|
|
||||||
namespace OSC {
|
namespace OSC {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Message::Message
|
* @brief Message::type_tag
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
Message::Message()
|
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 nests = 1;
|
||||||
|
while (nests > 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
|
||||||
|
{
|
||||||
|
nests++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (types.at(close) == ']') // closed this level of nesting
|
||||||
|
{
|
||||||
|
nests--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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>();
|
||||||
|
|
||||||
|
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>();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
for (const auto &argument: arguments)
|
||||||
|
argument->oStream(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace OSC
|
} // namespace OSC
|
||||||
|
@ -23,15 +23,30 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <argument.h>
|
||||||
|
#include <bufferstream.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace OSC {
|
namespace OSC {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The Message class
|
* @brief The Message struct
|
||||||
|
*
|
||||||
|
* \cite Spec10 An OSC message consists of an OSC Address Pattern followed by an OSC
|
||||||
|
* Type Tag String followed by zero or more OSC Arguments.
|
||||||
*/
|
*/
|
||||||
class Message
|
struct Message
|
||||||
|
: streamable
|
||||||
{
|
{
|
||||||
public:
|
std::string address_pattern; //!<An OSC-string beginning with the character ‘/’.
|
||||||
explicit Message();
|
std::string type_tag() const;
|
||||||
|
std::vector<std::shared_ptr<Argument>> arguments; //!< zero or more Arguments.
|
||||||
|
|
||||||
|
static std::vector<std::shared_ptr<Argument>> createArguments(std::string);
|
||||||
|
|
||||||
|
virtual size_t streamSize() const override;
|
||||||
|
virtual void iStream(std::shared_ptr<bufferstream>) override;
|
||||||
|
virtual void oStream(std::shared_ptr<bufferstream>) const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace OSC
|
} // namespace OSC
|
||||||
|
Loading…
Reference in New Issue
Block a user