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

162 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
osc/server.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 "receiver.h"
#include <sstream>
#include <thread>
namespace OSC {
Receiver::Receiver()
: address_space_(new Method())
{
}
Receiver::~Receiver()
{
delete address_space_;
}
/**
* @brief Receiver::dispatch
* @param msg
*/
void Receiver::dispatch(const std::shared_ptr<Message> msg) const
{
switch (msg->address_pattern.at(0)) {
case '/':
{
std::list<std::string> patterns;
std::istringstream strm(msg->address_pattern);
strm.seekg(1); // skip leading '/'
for (std::string name; std::getline(strm, name, '/');)
patterns.emplace_back(std::string(name));
address_space_->dispatch(patterns, msg);
}
break;
case '#':
scheduleBundle(std::static_pointer_cast<Bundle>(msg));
break;
default:
break;
}
}
/**
* @brief Receiver::scheduleBundle
* @param msg
*/
void Receiver::scheduleBundle(const std::shared_ptr<Bundle> msg) const
{
auto invoke = [&] {
for (const auto &element: msg->elements)
dispatch(element);
};
/**
* > \cite Spec10 The OSC Bundles OSC Time Tag determines when the OSC Bundles OSC Messages
* > corresponding OSC Methods should be invoked.
*/
auto time = msg->time_tag.time();
if (time <= std::chrono::system_clock::now())
/**
* > \cite Spec10 If the time represented by the OSC Time Tag is before or equal to the
* > current time, the OSC Server should invoke the methods immediately.
*
* \warning The 64-bit \cite NTPv4 NTP timestamps rollover in 2036. After which all OSC
* Bundles will appear to be in the past and will always be invoked immediately.
*/
invoke();
else
{
/**
* > \cite Spec10 Otherwise the OSC Time Tag represents a time in the future, and the OSC
* > server must store the OSC Bundle until the specified time and then invoke the
* > appropriate OSC Methods.
*/
std::thread thread([&] {
std::this_thread::sleep_until(time);
invoke();
});
thread.detach();
/// \test validate future OSC Bundle scheduling.
}
}
/**
* @brief Receiver::rxPacket
* @param buffer
*/
void Receiver::rxPacket(std::shared_ptr<bufferstream> buffer) const
{
while (buffer->good()) {
auto msg = std::make_shared<Message>();
msg->iStream(buffer);
if (!buffer->fail())
dispatch(msg);
}
}
/**
* @brief Receiver::rxPacketTCP
* @param buffer
*
* This method is presented in \cite Spec10 Spec 1.0, but has been delared as legacy
* in favor SLIP framming.
*
* \note It may be necessary to later move the length check into the TCP receiver.
*/
void Receiver::rxPacketTCP(std::shared_ptr<bufferstream> buffer) const
{
while (buffer->good()) {
uint32_t size;
auto msg = std::make_shared<Message>();
*buffer >> size;
msg->iStream(buffer);
if (size != msg->streamSize())
buffer->setstate(std::ios::failbit);
if (!buffer->fail())
dispatch(msg);
}
}
/**
* @brief Receiver::rxPacketSLIP
* @param buffer
*/
void Receiver::rxPacketSLIP(std::shared_ptr<bufferstream> buffer) const
{
(void)buffer;
/// \todo Receive an SLIP framed message.
}
} // namespace OSC