/* 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 #include 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 msg) const { switch (msg->address_pattern.at(0)) { case '/': { std::list patterns; std::istringstream strm(msg->address_pattern); strm.seekg(1); // skip leading '/' for (std::string name; std::getline(strm, name, '/');) patterns.push_back(name); address_space_->dispatch(patterns, msg); } break; case '#': scheduleBundle(std::static_pointer_cast(msg)); break; default: break; } } /** * @brief Receiver::scheduleBundle * @param msg */ void Receiver::scheduleBundle(const std::shared_ptr msg) const { auto invoke = [&] { 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. */ 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 buffer) const { auto packet = [&](std::shared_ptr 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()); break; case '#': packet(std::make_shared()); 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 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(); auto msg = std::make_shared(); 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 buffer) const { (void)buffer; /// \todo Receive an SLIP framed message. } } // namespace OSC