Squashed 'libs/CommService/' content from commit 7ccc0fc
git-subtree-dir: libs/CommService git-subtree-split: 7ccc0fce88bbc5969df060058cf0fb57abe3bcf9
This commit is contained in:
116
include/CommService/CommService.hpp
Normal file
116
include/CommService/CommService.hpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#ifndef COMMSERVICE
|
||||
#define COMMSERVICE
|
||||
|
||||
#include <CommService/Message.hpp>
|
||||
#include <CommService/ThreadsafeQueue.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <sys/select.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
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<LocalUDPClients> 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<bool> stopReceiveThread;
|
||||
std::atomic<bool> receiving;
|
||||
|
||||
|
||||
/// show if the service is connected or not
|
||||
std::atomic<bool> connected;
|
||||
|
||||
std::mutex mutex;
|
||||
|
||||
std::shared_ptr<ThreadsafeQueue<Message>> 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<unsigned char> *m);
|
||||
|
||||
void start();
|
||||
|
||||
void stop();
|
||||
|
||||
|
||||
std::shared_ptr<ThreadsafeQueue<Message>> getReceivedMessageQueue();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user