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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-04-25 09:03:46 -04:00
|
|
|
|
#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 {
|
|
|
|
|
|
2023-04-25 09:03:46 -04:00
|
|
|
|
Receiver::Receiver()
|
2023-04-23 11:35:31 -04:00
|
|
|
|
: address_space_(new Method())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 09:03:46 -04:00
|
|
|
|
Receiver::~Receiver()
|
2023-04-23 11:35:31 -04:00
|
|
|
|
{
|
|
|
|
|
delete address_space_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-05-16 10:09:14 -04:00
|
|
|
|
/**
|
|
|
|
|
* @brief Receiver::rxPacket
|
|
|
|
|
* @param buffer
|
|
|
|
|
*/
|
|
|
|
|
void Receiver::rxPacket(std::shared_ptr<bufferstream> buffer) const
|
|
|
|
|
{
|
|
|
|
|
auto packet = [&](std::shared_ptr<Message> msg)
|
|
|
|
|
{
|
|
|
|
|
msg->iStream(buffer);
|
|
|
|
|
if (!buffer->fail())
|
|
|
|
|
dispatch(msg);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
while (buffer->available() && buffer->good())
|
|
|
|
|
{
|
|
|
|
|
/// > \cite Spec10 The contents of an OSC packet must be either an OSC Message or
|
|
|
|
|
/// > an OSC Bundle. The first byte of the packet’s contents unambiguously
|
|
|
|
|
/// > distinguishes between these two alternatives.
|
|
|
|
|
switch (buffer->peek()) {
|
|
|
|
|
case '/':
|
|
|
|
|
packet(std::make_shared<Message>());
|
|
|
|
|
break;
|
|
|
|
|
case '#':
|
|
|
|
|
packet(std::make_shared<Bundle>());
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
buffer->setstate(std::ios::failbit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Receiver::rxPacketStream
|
|
|
|
|
* @param buffer
|
|
|
|
|
*
|
|
|
|
|
* This method is presented in \cite Spec10 Spec 1.0, but has been delared as legacy
|
|
|
|
|
* in favor of SLIP framming. \see rxPacketSLIP
|
|
|
|
|
*/
|
|
|
|
|
void Receiver::rxPacketStream(std::shared_ptr<bufferstream> buffer) const
|
|
|
|
|
{
|
|
|
|
|
/// > \cite Spec10 In a stream-based protocol such as TCP, the stream should begin with
|
|
|
|
|
/// > an int32 giving the size of the first packet, followed by the contents of the
|
|
|
|
|
/// > first packet, followed by the size of the second packet, etc.
|
|
|
|
|
while (buffer->good()) {
|
|
|
|
|
size_t size = buffer->readType<int32_t>();
|
|
|
|
|
auto msg = std::make_shared<Message>();
|
|
|
|
|
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.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-23 11:35:31 -04:00
|
|
|
|
/**
|
2023-04-25 09:03:46 -04:00
|
|
|
|
* @brief Receiver::dispatch
|
2023-04-23 11:35:31 -04:00
|
|
|
|
* @param msg
|
|
|
|
|
*/
|
2023-04-25 09:03:46 -04:00
|
|
|
|
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-05-16 10:09:14 -04:00
|
|
|
|
for (const auto &hit: findMethods(msg->address_pattern))
|
2023-05-16 09:52:18 -04:00
|
|
|
|
hit->trigger(msg);
|
2023-04-23 11:35:31 -04:00
|
|
|
|
break;
|
|
|
|
|
case '#':
|
|
|
|
|
scheduleBundle(std::static_pointer_cast<Bundle>(msg));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-05-16 10:09:14 -04:00
|
|
|
|
/**
|
|
|
|
|
* @brief Receiver::findMethods
|
2023-05-16 10:27:56 -04:00
|
|
|
|
* @param address
|
2023-05-16 10:09:14 -04:00
|
|
|
|
* @return
|
|
|
|
|
*/
|
2023-05-16 10:27:56 -04:00
|
|
|
|
std::vector<const Method *> Receiver::findMethods(std::string address) const
|
2023-05-16 10:09:14 -04:00
|
|
|
|
{
|
2023-05-16 10:27:56 -04:00
|
|
|
|
std::vector<const Method *> hits;
|
|
|
|
|
if (address.empty())
|
|
|
|
|
return hits;
|
|
|
|
|
|
|
|
|
|
std::istringstream stream(address);
|
|
|
|
|
if (address[0] == '/') // skip leading '/'
|
|
|
|
|
stream.seekg(1);
|
2023-05-16 10:09:14 -04:00
|
|
|
|
|
2023-05-16 10:27:56 -04:00
|
|
|
|
std::list<std::string> pattern;
|
2023-05-16 10:09:14 -04:00
|
|
|
|
for (std::string name; std::getline(stream, name, '/');)
|
2023-05-16 10:27:56 -04:00
|
|
|
|
pattern.push_back(name);
|
2023-05-16 10:09:14 -04:00
|
|
|
|
|
2023-05-16 10:27:56 -04:00
|
|
|
|
address_space_->matchAddress(hits, pattern);
|
2023-05-16 10:09:14 -04:00
|
|
|
|
return hits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-23 11:35:31 -04:00
|
|
|
|
/**
|
2023-04-25 09:03:46 -04:00
|
|
|
|
* @brief Receiver::scheduleBundle
|
2023-04-23 11:35:31 -04:00
|
|
|
|
* @param msg
|
|
|
|
|
*/
|
2023-04-25 09:03:46 -04:00
|
|
|
|
void Receiver::scheduleBundle(const std::shared_ptr<Bundle> msg) const
|
2023-04-23 11:35:31 -04:00
|
|
|
|
{
|
2023-05-16 09:50:14 -04:00
|
|
|
|
auto invoke = [this] (const std::shared_ptr<Bundle> msg) {
|
2023-04-24 16:44:04 -04:00
|
|
|
|
for (const auto &element: msg->elements)
|
|
|
|
|
dispatch(element);
|
|
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* > \cite Spec10 The OSC Bundle’s OSC Time Tag determines when the OSC Bundle’s 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.
|
|
|
|
|
*/
|
2023-05-16 09:50:14 -04:00
|
|
|
|
invoke(msg);
|
2023-04-24 16:44:04 -04:00
|
|
|
|
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);
|
2023-05-16 09:50:14 -04:00
|
|
|
|
invoke(msg);
|
2023-04-24 16:44:04 -04:00
|
|
|
|
});
|
|
|
|
|
thread.detach();
|
|
|
|
|
/// \test validate future OSC Bundle scheduling.
|
|
|
|
|
}
|
2023-04-23 11:35:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace OSC
|
|
|
|
|
|