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