otsdaq  v2_05_02_indev
TCPServer.cc
1 #include "otsdaq/NetworkUtilities/TCPServer.h"
2 #include <errno.h> // errno
3 #include <string.h> // errno
4 #include "otsdaq/NetworkUtilities/TCPTransceiverSocket.h"
5 
6 #include <iostream>
7 using namespace ots;
8 
9 //==============================================================================
10 TCPServer::TCPServer(int serverPort, unsigned int maxNumberOfClients) : TCPServerBase(serverPort, maxNumberOfClients)
11 {
12  fReceiveTimeout.tv_sec = 0;
13  fReceiveTimeout.tv_usec = 0;
14  fSendTimeout.tv_sec = 0;
15  fSendTimeout.tv_usec = 0;
16  // fAcceptFuture = std::async(std::launch::async, &TCPServer::acceptConnections,
17  // this);
18 }
19 
20 //==============================================================================
21 TCPServer::~TCPServer(void) {}
22 
23 // void TCPServer::StartAcceptConnections()
24 // {
25 
26 // }
27 //==============================================================================
28 // time out or protection for this receive method?
29 // void TCPServer::connectClient(int fdClientSocket)
30 void TCPServer::connectClient(TCPTransceiverSocket* socket)
31 {
32  // std::cout << __PRETTY_FUNCTION__ << "Waiting 3 seconds" << std::endl;
33  // std::this_thread::sleep_for(std::chrono::milliseconds(3000));
34  while(1)
35  {
36  std::cout << __PRETTY_FUNCTION__ << "Waiting for message for socket #: " << socket->getSocketId() << std::endl;
37  std::string message;
38  try
39  {
40  message = socket->receivePacket();
41  }
42  catch(const std::exception& e)
43  {
44  std::cout << __PRETTY_FUNCTION__ << "Error: " << e.what() << std::endl; // Client connection must have closed
45  std::cerr << __PRETTY_FUNCTION__ << e.what() << '\n';
46  TCPServerBase::closeClientSocket(socket->getSocketId());
47  interpretMessage("Error: " + std::string(e.what()));
48  return; // the pointer to socket has been deleted in closeClientSocket
49  }
50 
51  // std::cout << __PRETTY_FUNCTION__
52  // //<< "Received message:-" << message << "-"
53  // << "Message Length=" << message.length() << " From socket #: " << socket->getSocketId() << std::endl;
54  std::string messageToClient = interpretMessage(message);
55 
56  // Send back something only if there is actually a message to be sent!
57  if(messageToClient != "")
58  {
59  // std::cout << __PRETTY_FUNCTION__ << "Sending back message:-" << messageToClient << "-(nbytes=" << messageToClient.length() << ") to socket #: "
60  // << socket->getSocketId() << std::endl;
61  socket->sendPacket(messageToClient);
62  }
63  // else
64  // std::cout << __PRETTY_FUNCTION__ << "Not sending anything back to socket #: " << socket->getSocketId() << std::endl;
65 
66  // std::cout << __PRETTY_FUNCTION__ << "After message sent now checking for more... socket #: " << socket->getSocketId() << std::endl;
67  }
68 
69  std::cout << __PRETTY_FUNCTION__ << "Thread done for socket #: " << socket->getSocketId() << std::endl;
70 }
71 
72 //==============================================================================
73 void TCPServer::acceptConnections()
74 {
75  // std::pair<std::unordered_map<int, TCPTransceiverSocket>::iterator, bool> element;
76  while(true)
77  {
78  try
79  {
80  TCPTransceiverSocket* clientSocket = acceptClient<TCPTransceiverSocket>();
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);
84  thread.detach();
85  }
86  catch(int e)
87  {
88  std::cout << __PRETTY_FUNCTION__ << "SHUTTING DOWN SOCKET" << std::endl;
89  if(e == E_SHUTDOWN)
90  break;
91  }
92  }
93  fAcceptPromise.set_value(true);
94 }
95 
96 //==============================================================================
97 void TCPServer::setReceiveTimeout(unsigned int timeoutSeconds, unsigned int timeoutMicroseconds)
98 {
99  fReceiveTimeout.tv_sec = timeoutSeconds;
100  fReceiveTimeout.tv_usec = timeoutMicroseconds;
101 }
102 
103 //==============================================================================
104 void TCPServer::setSendTimeout(unsigned int timeoutSeconds, unsigned int timeoutMicroseconds)
105 {
106  fSendTimeout.tv_sec = timeoutSeconds;
107  fSendTimeout.tv_usec = timeoutMicroseconds;
108 }