#ifndef __COMMUNICATION_H_ #define __COMMUNICATION_H_ #include "communication/include/icommunication.h" #include "communication/include/command.h" #include "communication/include/network.h" #include "communication/include/packet.h" #include "communication/message/callback/include/messagecallback.h" #include "communication/message/include/serialnumbermessage.h" #include "communication/include/packetizer.h" #include "communication/include/encoder.h" #include "communication/include/decoder.h" #include #include #include #include #include #include namespace icsneo { class Communication { public: Communication( std::unique_ptr com, std::shared_ptr p, std::unique_ptr e, std::unique_ptr md) : packetizer(p), encoder(std::move(e)), decoder(std::move(md)), impl(std::move(com)) {} virtual ~Communication() { close(); } bool open(); bool close(); virtual void spawnThreads(); virtual void joinThreads(); bool rawWrite(const std::vector& bytes) { return impl->write(bytes); } virtual bool sendPacket(std::vector& bytes); virtual bool sendCommand(Command cmd, bool boolean) { return sendCommand(cmd, std::vector({ (uint8_t)boolean })); } virtual bool sendCommand(Command cmd, std::vector arguments = {}); bool getSettingsSync(std::vector& data, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); std::shared_ptr getSerialNumberSync(std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); int addMessageCallback(const MessageCallback& cb); bool removeMessageCallback(int id); std::shared_ptr waitForMessageSync(MessageFilter f = MessageFilter(), std::chrono::milliseconds timeout = std::chrono::milliseconds(50)) { return waitForMessageSync(std::make_shared(f), timeout); } std::shared_ptr waitForMessageSync(std::shared_ptr f, std::chrono::milliseconds timeout = std::chrono::milliseconds(50)); std::shared_ptr packetizer; // Ownership is shared with the encoder std::unique_ptr encoder; std::unique_ptr decoder; protected: std::unique_ptr impl; static int messageCallbackIDCounter; std::map messageCallbacks; std::atomic closing{false}; private: bool isOpen = false; std::thread readTaskThread; void readTask(); }; } #endif