MotorControlModuleSDFM_TMS3.../Projects/EFC_Application/UMLibrary/communication/service/BufferedCommunicationLink.hh

97 lines
2.4 KiB
C++

/*
* BufferedCommunicationLink.hh
*
* Created on: 23 àïð. 2020 ã.
* Author: LeonidTitov
*/
#ifndef UMLIBRARY_COMMUNICATION_SERVICE_BUFFEREDCOMMUNICATIONLINK_HH_
#define UMLIBRARY_COMMUNICATION_SERVICE_BUFFEREDCOMMUNICATIONLINK_HH_
#include "../../driver/ISlipCommunication.hh"
#include "IMessageLink.hh"
#include "IMessageHandler.hh"
#include "ICommunicationLink.hh"
#include "../../systemic/IProcess.hh"
#include <cstddef>
#include <stdint.h>
#include <memory_resource>
#include <map>
#include "../../systemic/Timer.hh"
namespace communication { namespace service {
class BufferedCommunicationLink : public ICommunicationLink, public systemic::IProcess {
public:
BufferedCommunicationLink( driver::ISlipCommunication & isc, std::pmr::memory_resource * memory, systemic::time_t timeout = 15 );
bool registerMessageHandler( IMessageHandler * handler, std::size_t id );
float getPacketCounter() const;
void process();
private:
systemic::Timer series_timeout;
systemic::time_t timeout_after_send;
uint16_t free_counter;
typedef unsigned short PacketId;
driver::ISlipCommunication & communication;
struct Series : public IMessageSeriesLink {
ostream & sendData( std::size_t size );
bool data_sent;
PacketId message_id;
IMessageSeriesHandler * series_handler;
std::size_t message_remain;
driver::ISlipCommunication & communication;
bool charge( PacketId id, std::size_t num_messages, IMessageSeriesHandler * handler );
void update();
Series( driver::ISlipCommunication & com );
} series;
struct MessageLink : public IMessageLink {
istream & getData();
ostream & createMessage( std::size_t size );
std::size_t transmitBuffCapacity() const;
std::size_t receiveBuffCapacity() const;
bool createMessageSeries( std::size_t num_messages, IMessageSeriesHandler * handler );
PacketId message_id;
bool message_answered;
MessageLink( PacketId id, driver::ISlipCommunication & com, Series & ser );
~MessageLink() noexcept;
driver::ISlipCommunication & communication;
Series & series;
};
typedef std::map< PacketId, IMessageHandler *, std::less<PacketId>, std::pmr::polymorphic_allocator< std::pair< const PacketId, IMessageHandler *> > > Subscribers;
Subscribers subscribers;
};
}}
#endif /* UMLIBRARY_COMMUNICATION_SERVICE_BUFFEREDCOMMUNICATIONLINK_HH_ */