use callbacks to send the byte stream
This commit is contained in:
parent
0bd30d1151
commit
fdf801c962
@ -39,4 +39,34 @@ 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::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
|
} // namespace OSC
|
||||||
|
@ -23,6 +23,8 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <bufferstream.h>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
|
|
||||||
@ -40,10 +42,12 @@ public:
|
|||||||
explicit Sender();
|
explicit Sender();
|
||||||
virtual ~Sender();
|
virtual ~Sender();
|
||||||
|
|
||||||
/**
|
void send(std::shared_ptr<Message>) const;
|
||||||
* @brief send
|
std::shared_ptr<void> setSender(const std::function<void(std::shared_ptr<bufferstream>)>);
|
||||||
*/
|
|
||||||
virtual void send(std::shared_ptr<Message>) = 0;
|
|
||||||
|
private:
|
||||||
|
std::weak_ptr<const std::function<void(std::shared_ptr<bufferstream>)>> sender_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace OSC
|
} // namespace OSC
|
||||||
|
Loading…
Reference in New Issue
Block a user