OpenLCP/protocol/osc/receiver.cpp

162 lines
4.2 KiB
C++
Raw Normal View History

2023-04-23 11:35:31 -04:00
/*
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"
2023-04-23 11:35:31 -04:00
#include <sstream>
2023-04-24 16:44:04 -04:00
#include <thread>
2023-04-23 11:35:31 -04:00
namespace OSC {
Receiver::Receiver()
2023-04-23 11:35:31 -04:00
: address_space_(new Method())
{
}
Receiver::~Receiver()
2023-04-23 11:35:31 -04:00
{
delete address_space_;
}
/**
* @brief Receiver::dispatch
2023-04-23 11:35:31 -04:00
* @param msg
*/
void Receiver::dispatch(const std::shared_ptr<Message> msg) const
2023-04-23 11:35:31 -04:00
{
switch (msg->address_pattern.at(0)) {
case '/':
{
2023-04-23 11:56:57 -04:00
std::list<std::string> patterns;
2023-04-23 11:35:31 -04:00
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
2023-04-23 11:35:31 -04:00
* @param msg
*/
void Receiver::scheduleBundle(const std::shared_ptr<Bundle> msg) const
2023-04-23 11:35:31 -04:00
{
2023-04-24 16:44:04 -04:00
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.
}
2023-04-23 11:35:31 -04:00
}
2023-04-25 10:10:16 -04:00
/**
* @brief Receiver::rxPacket
2023-04-25 10:25:54 -04:00
* @param buffer
2023-04-25 10:10:16 -04:00
*/
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
2023-04-25 10:25:54 -04:00
* @param buffer
2023-04-25 10:10:16 -04:00
*
* 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
2023-04-25 10:25:54 -04:00
* @param buffer
2023-04-25 10:10:16 -04:00
*/
void Receiver::rxPacketSLIP(std::shared_ptr<bufferstream> buffer) const
{
(void)buffer;
/// \todo Receive an SLIP framed message.
}
2023-04-23 11:35:31 -04:00
} // namespace OSC