1 #include "otsdaq/NetworkUtilities/TCPServer.h"
4 #include "otsdaq/NetworkUtilities/TCPTransceiverSocket.h"
10 TCPServer::TCPServer(
int serverPort,
unsigned int maxNumberOfClients) :
TCPServerBase(serverPort, maxNumberOfClients)
12 fReceiveTimeout.tv_sec = 0;
13 fReceiveTimeout.tv_usec = 0;
14 fSendTimeout.tv_sec = 0;
15 fSendTimeout.tv_usec = 0;
21 TCPServer::~TCPServer(
void) {}
36 std::cout << __PRETTY_FUNCTION__ <<
"Waiting for message for socket #: " << socket->getSocketId() << std::endl;
40 message = socket->receivePacket();
42 catch(
const std::exception& e)
44 std::cout << __PRETTY_FUNCTION__ <<
"Error: " << e.what() << std::endl;
45 std::cerr << __PRETTY_FUNCTION__ << e.what() <<
'\n';
46 TCPServerBase::closeClientSocket(socket->getSocketId());
47 interpretMessage(
"Error: " + std::string(e.what()));
54 std::string messageToClient = interpretMessage(message);
57 if(messageToClient !=
"")
61 socket->sendPacket(messageToClient);
69 std::cout << __PRETTY_FUNCTION__ <<
"Thread done for socket #: " << socket->getSocketId() << std::endl;
73 void TCPServer::acceptConnections()
81 clientSocket->setReceiveTimeout(fReceiveTimeout.tv_sec, fReceiveTimeout.tv_usec);
82 clientSocket->setSendTimeout(fSendTimeout.tv_sec, fSendTimeout.tv_usec);
83 std::thread thread(&TCPServer::connectClient,
this, clientSocket);
88 std::cout << __PRETTY_FUNCTION__ <<
"SHUTTING DOWN SOCKET" << std::endl;
93 fAcceptPromise.set_value(
true);
97 void TCPServer::setReceiveTimeout(
unsigned int timeoutSeconds,
unsigned int timeoutMicroseconds)
99 fReceiveTimeout.tv_sec = timeoutSeconds;
100 fReceiveTimeout.tv_usec = timeoutMicroseconds;
104 void TCPServer::setSendTimeout(
unsigned int timeoutSeconds,
unsigned int timeoutMicroseconds)
106 fSendTimeout.tv_sec = timeoutSeconds;
107 fSendTimeout.tv_usec = timeoutMicroseconds;