#ifndef COMMSERVICE #define COMMSERVICE #include #include #include #include #include #include #include #include #include #include #include #include #include namespace CommService { class LocalUDPClients{ public: unsigned short port; std::uint16_t id; }; class CommService { private: /// device id std::uint16_t deviceID_; /// participant MajorType deviceMajorType majorType_; /// minor Type std::string minorType_; /// the IP Address to send messages to std::string dstIp; /// the local source IP Address std::string srcIp; /// the udp port on which to broadcast and receive messages unsigned short broadcastPort = 0; /// the port we are listing on for messages, should normally be broadcastPort, but... unsigned short localPort = 0; /// the network socket to work on int inetSocket = -1; /// vector of local client ports we forward messages to std::vector clients; /// mutex for exclusive access to the clients vector std::mutex mutexClients; /// variable for holding the receive thread identifier std::thread receiveThread; /// variable indicating if the receiveThread should be stopped std::atomic stopReceiveThread; std::atomic receiving; /// show if the service is connected or not std::atomic connected; std::mutex mutex; std::shared_ptr> ReceivingMessageQueue; /// attribute identifying this service as a gateway and all packets should be forwarded bool gateway = false; void setGateway(bool); public: CommService(std::string dst,std::string src, std::uint16_t id, deviceMajorType majorType, std::string minorType, unsigned short dstPort ): dstIp(dst),srcIp(src),majorType_(majorType),minorType_(minorType),broadcastPort(dstPort),stopReceiveThread(false),deviceID_(id){} ~CommService(); void connect(); void disconnect(); void publish(Message m); void receiveThreadFunc(); void asyncReceive(); void processMsg(std::vector *m); void start(); void stop(); std::shared_ptr> getReceivedMessageQueue(); }; }; #endif