1 #include "otsdaq/NetworkUtilities/TCPTransmitterSocket.h"
3 #include <sys/socket.h>
6 #include "otsdaq/NetworkUtilities/TCPPacket.h"
12 TCPTransmitterSocket::TCPTransmitterSocket(
int socketId) :
TCPSocket(socketId) {}
15 TCPTransmitterSocket::~TCPTransmitterSocket(
void) {}
18 void TCPTransmitterSocket::sendPacket(
char const* buffer, std::size_t size) { send(TCPPacket::encode(buffer, size)); }
21 void TCPTransmitterSocket::sendPacket(
const std::string& buffer) { send(TCPPacket::encode(buffer)); }
24 void TCPTransmitterSocket::send(
char const* buffer, std::size_t size)
26 std::size_t sentBytes = ::send(getSocketId(), buffer, size, MSG_NOSIGNAL);
27 if(sentBytes == static_cast<std::size_t>(-1))
38 throw std::runtime_error(std::string(
"Write: critical error: ") + strerror(errno));
48 throw std::runtime_error(std::string(
"Write: resource failure: ") + strerror(errno));
57 throw std::runtime_error(std::string(
"Write: temporary error: ") + strerror(errno));
61 throw std::runtime_error(std::string(
"Write: returned -1: ") + strerror(errno));
68 void TCPTransmitterSocket::send(
const std::string& buffer)
70 if(buffer.size() == 0)
72 std::cout << __PRETTY_FUNCTION__ <<
"I am sorry but I won't send an empty packet!" << std::endl;
75 send(&buffer.at(0), buffer.size());
79 void TCPTransmitterSocket::send(
const std::vector<char>& buffer)
81 if(buffer.size() == 0)
83 std::cout << __PRETTY_FUNCTION__ <<
"I am sorry but I won't send an empty packet!" << std::endl;
86 send(&buffer.at(0), buffer.size());
90 void TCPTransmitterSocket::send(
const std::vector<uint16_t>& buffer)
92 if(buffer.size() == 0)
94 std::cout << __PRETTY_FUNCTION__ <<
"I am sorry but I won't send an empty packet!" << std::endl;
97 send((
const char*)&buffer.at(0), buffer.size());
101 void TCPTransmitterSocket::setSendTimeout(
unsigned int timeoutSeconds,
unsigned int timeoutMicroSeconds)
104 tv.tv_sec = timeoutSeconds;
105 tv.tv_usec = timeoutMicroSeconds;
106 setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, (
const char*)&tv,
sizeof tv);