otsdaq  v2_05_02_indev
TCPTransmitterSocket.cc
1 #include "otsdaq/NetworkUtilities/TCPTransmitterSocket.h"
2 #include <string.h>
3 #include <sys/socket.h>
4 #include <unistd.h>
5 #include <stdexcept>
6 #include "otsdaq/NetworkUtilities/TCPPacket.h"
7 //#include <iostream>
8 
9 using namespace ots;
10 
11 //==============================================================================
12 TCPTransmitterSocket::TCPTransmitterSocket(int socketId) : TCPSocket(socketId) {}
13 
14 //==============================================================================
15 TCPTransmitterSocket::~TCPTransmitterSocket(void) {}
16 
17 //==============================================================================
18 void TCPTransmitterSocket::sendPacket(char const* buffer, std::size_t size) { send(TCPPacket::encode(buffer, size)); }
19 
20 //==============================================================================
21 void TCPTransmitterSocket::sendPacket(const std::string& buffer) { send(TCPPacket::encode(buffer)); }
22 
23 //==============================================================================
24 void TCPTransmitterSocket::send(char const* buffer, std::size_t size)
25 {
26  std::size_t sentBytes = ::send(getSocketId(), buffer, size, MSG_NOSIGNAL);
27  if(sentBytes == static_cast<std::size_t>(-1))
28  {
29  switch(errno)
30  {
31  // case EINVAL:
32  // case EBADF:
33  // case ECONNRESET:
34  // case ENXIO:
35  case EPIPE:
36  {
37  // Fatal error. Programming bug
38  throw std::runtime_error(std::string("Write: critical error: ") + strerror(errno));
39  }
40  // case EDQUOT:
41  // case EFBIG:
42  // case EIO:
43  // case ENETDOWN:
44  // case ENETUNREACH:
45  case ENOSPC:
46  {
47  // Resource acquisition failure or device error
48  throw std::runtime_error(std::string("Write: resource failure: ") + strerror(errno));
49  }
50  case EINTR:
51  // TODO: Check for user interrupt flags.
52  // Beyond the scope of this project
53  // so continue normal operations.
54  case EAGAIN:
55  {
56  // Temporary error.
57  throw std::runtime_error(std::string("Write: temporary error: ") + strerror(errno));
58  }
59  default:
60  {
61  throw std::runtime_error(std::string("Write: returned -1: ") + strerror(errno));
62  }
63  }
64  }
65 }
66 
67 //==============================================================================
68 void TCPTransmitterSocket::send(const std::string& buffer)
69 {
70  if(buffer.size() == 0)
71  {
72  std::cout << __PRETTY_FUNCTION__ << "I am sorry but I won't send an empty packet!" << std::endl;
73  return;
74  }
75  send(&buffer.at(0), buffer.size());
76 }
77 
78 //==============================================================================
79 void TCPTransmitterSocket::send(const std::vector<char>& buffer)
80 {
81  if(buffer.size() == 0)
82  {
83  std::cout << __PRETTY_FUNCTION__ << "I am sorry but I won't send an empty packet!" << std::endl;
84  return;
85  }
86  send(&buffer.at(0), buffer.size());
87 }
88 
89 //==============================================================================
90 void TCPTransmitterSocket::send(const std::vector<uint16_t>& buffer)
91 {
92  if(buffer.size() == 0)
93  {
94  std::cout << __PRETTY_FUNCTION__ << "I am sorry but I won't send an empty packet!" << std::endl;
95  return;
96  }
97  send((const char*)&buffer.at(0), buffer.size());
98 }
99 
100 //==============================================================================
101 void TCPTransmitterSocket::setSendTimeout(unsigned int timeoutSeconds, unsigned int timeoutMicroSeconds)
102 {
103  struct timeval tv;
104  tv.tv_sec = timeoutSeconds;
105  tv.tv_usec = timeoutMicroSeconds;
106  setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof tv);
107 }