1
0
Fork 0

use a callback to send stream data

This commit is contained in:
Kevin Matz 2023-05-20 13:39:03 -04:00
parent e76d466bf6
commit eead2311ea
2 changed files with 28 additions and 2 deletions

View File

@ -46,14 +46,35 @@ Device::Device()
/**
* @brief Device::setSender
* @param sender
* @return
*/
std::shared_ptr<void> Device::setSender(const std::function<void(std::shared_ptr<bufferstream>,
ipAddress)> sender)
{
// wrap the callback with a shared pointer
auto sp = std::make_shared<std::function<void(std::shared_ptr<bufferstream>,
ipAddress)>>(std::move(sender));
// store sender function (as a weak pointer)
_sender = sp;
// return token that caller must keep throughout it's scope
return sp;
}
/**
* @brief Device::send
* @param packet
* @param address
*/
void Device::send(const std::shared_ptr<Packet> packet, const ipAddress &address) const
{
auto buffer = std::vector<uint8_t>(packet->streamSize());
auto stream = std::make_shared<bufferstream>(buffer.data(), buffer.size());
packet->oStream(stream);
if (auto sp = _sender.lock()) // the owner is still holding the token
(*sp)(stream, address);
}

View File

@ -24,6 +24,7 @@
#pragma once
#include "packet.h"
#include <functional>
#include <udp.h>
namespace ARTNET {
@ -40,13 +41,15 @@ public:
explicit Device();
// virtual void receive(ACN::PDU::Stream);
std::shared_ptr<void> setSender(const std::function<void(std::shared_ptr<bufferstream>,
ipAddress)>);
protected:
TalkToMe diagnostic_reporting_behavior; //!< behavior flags
Priority diagnostic_reporting_threshold; //!< lowest priority dignostic message to send
// void send(const uint32_t vector, const std::shared_ptr<packet_data> data,
// const ACN::SDT::UDP::ipAddress&);
void send(const std::shared_ptr<Packet> packet, const ipAddress &) const;
void rxArtPoll(std::shared_ptr<ArtPoll>);
virtual void rxArtPollReply(std::shared_ptr<ArtPollReply>);
virtual void rxArtDiagData(std::shared_ptr<ArtDiagData>);
@ -101,6 +104,8 @@ protected:
*/
virtual ipAddress broadcastIp() const = 0;
private:
std::weak_ptr<const std::function<void(std::shared_ptr<bufferstream>, ipAddress)>> _sender;
};
} // namespace ARTNET