$treeview $search $mathjax $extrastylesheet
otsdaq
v2_03_00
$projectbrief
|
$projectbrief
|
$searchbox |
00001 #ifndef _ots_TCPSocket_h_ 00002 #define _ots_TCPSocket_h_ 00003 00004 #include <netinet/in.h> 00005 #include <sys/types.h> 00006 #include <mutex> 00007 #include <string> 00008 #include <vector> 00009 00010 #include "artdaq/DAQdata/TCPConnect.hh" 00011 #include "artdaq/DAQdata/TCP_listen_fd.hh" 00012 00013 #define OTS_MAGIC 0x5F4F54534441515F /* _OTSDAQ_ */ 00014 00015 namespace ots 00016 { 00017 class TCPSocket 00018 { 00019 public: 00020 TCPSocket(const std::string& senderHost, 00021 unsigned int senderPort, 00022 int receiveBufferSize = 0x10000); 00023 TCPSocket(unsigned int listenPort, int sendBufferSize = 0x10000); 00024 virtual ~TCPSocket(void); 00025 00026 void connect(double tmo_s = 10.0); 00027 00028 int send(const uint8_t* data, size_t size); 00029 int send(const std::string& buffer); 00030 int send(const std::vector<uint32_t>& buffer); 00031 int send(const std::vector<uint16_t>& buffer); 00032 00033 int receive(uint8_t* buffer, 00034 unsigned int timeoutSeconds, 00035 unsigned int timeoutUSeconds); 00036 int receive(std::string& buffer, 00037 unsigned int timeoutSeconds = 1, 00038 unsigned int timeoutUSeconds = 0); 00039 int receive(std::vector<uint32_t>& buffer, 00040 unsigned int timeoutSeconds = 1, 00041 unsigned int timeoutUSeconds = 0); 00042 00043 protected: 00044 TCPSocket(void); 00045 00046 std::string host_; 00047 unsigned int port_; 00048 00049 int TCPSocketNumber_; 00050 int SendSocket_; 00051 bool isSender_; 00052 int bufferSize_; 00053 size_t chunkSize_; 00054 00055 mutable std::mutex socketMutex_; 00056 00057 private: 00058 struct MagicPacket 00059 { 00060 uint64_t ots_magic; 00061 unsigned int dest_port; 00062 }; 00063 MagicPacket makeMagicPacket(unsigned int port) 00064 { 00065 MagicPacket m; 00066 m.ots_magic = OTS_MAGIC; 00067 m.dest_port = port; 00068 return m; 00069 } 00070 bool checkMagicPacket(MagicPacket magic) 00071 { 00072 return magic.ots_magic == OTS_MAGIC && magic.dest_port == port_; 00073 } 00074 }; 00075 00076 } // namespace ots 00077 00078 #endif