1 #include "otsdaq/NetworkUtilities/TCPReceiverSocket.h"
2 #include <sys/socket.h>
11 TCPReceiverSocket::TCPReceiverSocket(
int socketId) :
TCPSocket(socketId) { fPacket.reset(); }
14 TCPReceiverSocket::~TCPReceiverSocket(
void) {}
17 std::string TCPReceiverSocket::receivePacket()
19 std::string retVal =
"";
23 fPacket += receive<std::string>();
26 while(!fPacket.isEmpty() && !fPacket.decode(retVal));
31 std::size_t TCPReceiverSocket::receive(
char* buffer, std::size_t bufferSize,
int )
33 std::cout << __PRETTY_FUNCTION__ <<
"Receiving Message for socket: " << getSocketId() << std::endl;
34 if(getSocketId() == 0)
36 throw std::logic_error(
"Bad socket object (this object was moved)");
39 std::size_t dataRead = ::read(getSocketId(), buffer, bufferSize);
40 if(dataRead == static_cast<std::size_t>(-1))
42 std::stringstream error;
46 error <<
"Socket file descriptor " << getSocketId() <<
" is not a valid file descriptor or is not open for reading...Errno: " << errno;
49 error <<
"Buffer is outside your accessible address space...Errno: " << errno;
54 error <<
"Read critical error caused by a programming bug...Errno: " << errno;
55 throw std::domain_error(error.str());
61 error <<
"The call was interrupted by a signal before any data was "
81 error <<
"Read: returned -1...Errno: " << errno;
84 throw std::runtime_error(error.str());
86 else if(dataRead == static_cast<std::size_t>(0))
88 std::cout << __PRETTY_FUNCTION__ <<
"Connection closed!" << std::endl;
89 throw std::runtime_error(
"Connection closed");
97 void TCPReceiverSocket::setReceiveTimeout(
unsigned int timeoutSeconds,
unsigned int timeoutMicroSeconds)
100 tv.tv_sec = timeoutSeconds;
101 tv.tv_usec = timeoutMicroSeconds;
102 setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, (
const char*)&tv,
sizeof tv);