89 lines
2.4 KiB
C++
89 lines
2.4 KiB
C++
/*
|
|
osc/client.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 "sender.h"
|
|
#include "bundle.h"
|
|
|
|
namespace OSC {
|
|
|
|
/**
|
|
* @brief Client::Client
|
|
*/
|
|
Sender::Sender()
|
|
{
|
|
|
|
}
|
|
|
|
Sender::~Sender()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Sender::send
|
|
* @param msg
|
|
*/
|
|
void Sender::send(std::shared_ptr<Message> msg) const
|
|
{
|
|
auto buffer = std::vector<uint8_t>(msg->streamSize());
|
|
auto stream = std::make_shared<bufferstream>(buffer.data(), buffer.size());
|
|
msg->oStream(stream);
|
|
if (auto sp = sender_.lock()) // the owner is still holding the token
|
|
(*sp)(stream);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Sender::send
|
|
* @param elements
|
|
* @param time
|
|
*/
|
|
void Sender::send(std::vector<std::shared_ptr<Message>> elements,
|
|
std::chrono::time_point<std::chrono::system_clock> time) const
|
|
{
|
|
auto msg = std::make_shared<Bundle>();
|
|
msg->time_tag.setTime(time);
|
|
msg->elements = elements;
|
|
send(msg);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Sender::setSender
|
|
* @param cb
|
|
* @return
|
|
*/
|
|
std::shared_ptr<void> Sender::setSender(const std::function<void(std::shared_ptr<bufferstream>)> cb)
|
|
{
|
|
// wrap the callback with a shared pointer
|
|
auto sp = std::make_shared<std::function<void(std::shared_ptr<bufferstream>)>>(std::move(cb));
|
|
// store sender function (as a weak pointer)
|
|
sender_ = sp;
|
|
// return token that caller must keep throughout it's scope
|
|
return sp;
|
|
}
|
|
|
|
} // namespace OSC
|