2022-12-11 09:58:26 -05:00
|
|
|
/*
|
|
|
|
osc/bundle.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.
|
|
|
|
*/
|
|
|
|
|
2023-04-23 11:33:58 -04:00
|
|
|
|
2022-12-11 09:58:26 -05:00
|
|
|
#include "bundle.h"
|
|
|
|
|
|
|
|
namespace OSC {
|
|
|
|
|
2023-04-23 11:33:58 -04:00
|
|
|
size_t Bundle::streamSize() const
|
|
|
|
{
|
|
|
|
size_t size = 16; // "#bundle" + timetag
|
|
|
|
for (const auto &element: elements)
|
|
|
|
size += 4 + element->streamSize(); // element's length + element length
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-15 10:55:24 -04:00
|
|
|
void Bundle::iStream(std::shared_ptr<bufferstream> buffer)
|
2023-04-23 11:33:58 -04:00
|
|
|
{
|
2023-05-15 10:55:24 -04:00
|
|
|
auto element = [&](std::shared_ptr<Message> msg, uint32_t size) {
|
|
|
|
msg->iStream(buffer);
|
|
|
|
// The element, as read, must match the prescribed size.
|
|
|
|
if (!buffer->fail())
|
|
|
|
{
|
|
|
|
if (msg->streamSize() == size)
|
|
|
|
elements.push_back(msg);
|
|
|
|
else
|
|
|
|
buffer->setstate(std::ios::failbit);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An OSC Bundle consists of the OSC-string “#bundle”
|
2023-04-23 11:33:58 -04:00
|
|
|
std::string header;
|
2023-05-15 10:55:24 -04:00
|
|
|
Message::readString(buffer, header);
|
2023-04-23 11:33:58 -04:00
|
|
|
if (header.compare(address_pattern) != 0)
|
2023-05-15 10:55:24 -04:00
|
|
|
return buffer->setstate(std::ios::failbit);
|
2023-04-23 11:33:58 -04:00
|
|
|
|
2023-05-15 10:55:24 -04:00
|
|
|
/// ... followed by an OSC Time Tag
|
|
|
|
time_tag.iStream(buffer);
|
2023-04-23 11:33:58 -04:00
|
|
|
|
2023-05-15 10:55:24 -04:00
|
|
|
/// ... followed by zero or more OSC Bundle Elements.
|
|
|
|
while (buffer->available() && buffer->good())
|
2023-04-23 11:33:58 -04:00
|
|
|
{
|
2023-05-15 10:55:24 -04:00
|
|
|
/// > \cite Spec10 An OSC Bundle Element consists of its size and its contents.
|
|
|
|
|
|
|
|
/// > \cite Spec10 The size is an int32 representing the number of 8-bit bytes
|
|
|
|
/// > in the contents, and will always be a multiple of 4.
|
|
|
|
uint32_t size = buffer->readType<int32_t>();
|
|
|
|
if (size % 4 != 0)
|
|
|
|
return buffer->setstate(std::ios::failbit);
|
|
|
|
|
|
|
|
/// > \cite Spec10 The contents are either an OSC Message or an OSC Bundle.
|
|
|
|
switch (buffer->peek()) {
|
2023-04-23 11:33:58 -04:00
|
|
|
case '/':
|
2023-05-15 10:55:24 -04:00
|
|
|
element(std::make_shared<Message>(), size);
|
2023-04-23 11:33:58 -04:00
|
|
|
break;
|
|
|
|
case '#':
|
2023-05-15 10:55:24 -04:00
|
|
|
element(std::make_shared<Bundle>(), size);
|
2023-04-23 11:33:58 -04:00
|
|
|
break;
|
|
|
|
default:
|
2023-05-15 10:55:24 -04:00
|
|
|
buffer->setstate(std::ios::failbit);
|
2023-04-23 11:33:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-15 10:55:24 -04:00
|
|
|
void Bundle::oStream(std::shared_ptr<bufferstream> buffer) const
|
2022-12-11 09:58:26 -05:00
|
|
|
{
|
2023-05-15 10:55:24 -04:00
|
|
|
Message::writeString(buffer, address_pattern);
|
|
|
|
time_tag.oStream(buffer);
|
2023-04-23 11:33:58 -04:00
|
|
|
for (const auto &element: elements)
|
|
|
|
{
|
2023-05-15 10:55:24 -04:00
|
|
|
buffer->writeType<int32_t>(element->streamSize());
|
|
|
|
element->oStream(buffer);
|
2023-04-23 11:33:58 -04:00
|
|
|
}
|
2022-12-11 09:58:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace OSC
|