85 lines
2.4 KiB
C++
85 lines
2.4 KiB
C++
/*
|
|
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.
|
|
*/
|
|
|
|
|
|
#include "bundle.h"
|
|
|
|
namespace OSC {
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
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
|