4 #include "otsdaq/NetworkUtilities/TCPServerBase.h"
5 #include "otsdaq/NetworkUtilities/TCPTransmitterSocket.h"
16 TCPServerBase::TCPServerBase(
int serverPort,
unsigned int )
17 : fAccept(true), fAcceptFuture(fAcceptPromise.get_future())
20 if(::setsockopt(getSocketId(), SOL_SOCKET, SO_REUSEADDR, &opt,
sizeof(opt)) != 0)
23 throw std::runtime_error(std::string(
"Setsockopt: ") + strerror(errno));
26 struct sockaddr_in serverAddr;
27 bzero((
char*)&serverAddr,
sizeof(serverAddr));
28 serverAddr.sin_family = AF_INET;
29 serverAddr.sin_port = htons(serverPort);
30 serverAddr.sin_addr.s_addr = INADDR_ANY;
32 if(::bind(getSocketId(), (
struct sockaddr*)&serverAddr,
sizeof(serverAddr)) != 0)
35 throw std::runtime_error(std::string(
"Bind: ") + strerror(errno));
38 if(::listen(getSocketId(), fMaxConnectionBacklog) != 0)
41 throw std::runtime_error(std::string(
"Listen: ") + strerror(errno));
47 TCPServerBase::~TCPServerBase(
void)
49 std::cout << __PRETTY_FUNCTION__ <<
"Shutting down accept for socket: " << getSocketId() << std::endl;
51 while(fAcceptFuture.wait_for(std::chrono::milliseconds(100)) != std::future_status::ready)
52 std::cout << __PRETTY_FUNCTION__ <<
"Server accept still running" << std::endl;
53 std::cout << __PRETTY_FUNCTION__ <<
"Closing connected client sockets for socket: " << getSocketId() << std::endl;
55 std::cout << __PRETTY_FUNCTION__ <<
"Closed all sockets connected to server: " << getSocketId() << std::endl;
59 void TCPServerBase::startAccept(
void)
62 std::thread thread(&TCPServerBase::acceptConnections,
this);
68 int TCPServerBase::accept(
bool blocking)
70 std::cout << __PRETTY_FUNCTION__ <<
"Now server accept connections on socket: " << getSocketId() << std::endl;
71 if (getSocketId() == invalidSocketId)
73 throw std::logic_error(
"Accept called on a bad socket object (this object was moved)");
76 struct sockaddr_storage serverStorage;
77 socklen_t addr_size =
sizeof serverStorage;
78 int clientSocket = invalidSocketId;
81 clientSocket = ::accept(getSocketId(), (
struct sockaddr *)&serverStorage, &addr_size);
82 std::cout << __LINE__<<
"] " << __PRETTY_FUNCTION__ <<
"fAccept? " << fAccept << std::endl;
87 if (clientSocket == invalidSocketId)
89 std::cout << __PRETTY_FUNCTION__ <<
"New socket invalid?: " << clientSocket <<
" errno: " << errno << std::endl;
90 throw std::runtime_error(std::string(
"Accept: ") + strerror(errno));
92 std::cout << __PRETTY_FUNCTION__ <<
"Server just accepted a connection on socket: " << getSocketId() <<
" Client socket: " << clientSocket << std::endl;
97 constexpr
int sleepMSeconds = 5;
98 constexpr
int timeoutSeconds = 0;
99 constexpr
int timeoutUSeconds = 1000;
100 struct timeval timeout;
101 timeout.tv_sec = timeoutSeconds;
102 timeout.tv_usec = timeoutUSeconds;
109 FD_SET(getSocketId(), &fdSet);
110 select(getSocketId() + 1, &fdSet, 0, 0, &timeout);
112 if (FD_ISSET(getSocketId(), &fdSet))
114 struct sockaddr_in clientAddress;
115 socklen_t socketSize =
sizeof(clientAddress);
117 clientSocket = ::accept(getSocketId(), (
struct sockaddr *)&clientAddress, &socketSize);
118 if (clientSocket == invalidSocketId)
120 std::cout << __PRETTY_FUNCTION__ <<
"New socket invalid?: " << clientSocket <<
" errno: " << errno << std::endl;
121 throw std::runtime_error(std::string(
"Accept: ") + strerror(errno));
125 std::this_thread::sleep_for(std::chrono::milliseconds(sleepMSeconds));
132 void TCPServerBase::closeClientSockets(
void)
134 for(
auto& socket : fConnectedClients)
136 socket.second->sendClose();
137 delete socket.second;
139 fConnectedClients.clear();
143 void TCPServerBase::closeClientSocket(
int socket)
145 for(
auto it = fConnectedClients.begin(); it != fConnectedClients.end(); it++)
146 if(it->second->getSocketId() == socket)
148 it->second->sendClose();
150 fConnectedClients.erase(it--);
155 void TCPServerBase::broadcastPacket(
const char* message, std::size_t length)
157 broadcastPacket(std::string(message,length));
161 void TCPServerBase::broadcastPacket(
const std::string& message)
163 for(
auto it = fConnectedClients.begin(); it != fConnectedClients.end(); it++)
169 catch(
const std::exception& e)
171 std::cout << __PRETTY_FUNCTION__ <<
"Error: " << e.what() << std::endl;
173 fConnectedClients.erase(it--);
179 void TCPServerBase::broadcast(
const char* message, std::size_t length)
182 for (
auto it = fConnectedClients.begin(); it != fConnectedClients.end(); it++)
188 catch (
const std::exception &e)
192 fConnectedClients.erase(it--);
198 void TCPServerBase::broadcast(
const std::string& message)
200 for(
auto it = fConnectedClients.begin(); it != fConnectedClients.end(); it++)
206 catch(
const std::exception& e)
208 std::cout << __PRETTY_FUNCTION__ <<
"Error: " << e.what() << std::endl;
210 fConnectedClients.erase(it--);
216 void TCPServerBase::broadcast(
const std::vector<char>& message)
218 for(
auto it = fConnectedClients.begin(); it != fConnectedClients.end(); it++)
224 catch(
const std::exception& e)
226 std::cout << __PRETTY_FUNCTION__ <<
"Error: " << e.what() << std::endl;
228 fConnectedClients.erase(it--);
234 void TCPServerBase::broadcast(
const std::vector<uint16_t>& message)
236 for(
auto it = fConnectedClients.begin(); it != fConnectedClients.end(); it++)
242 catch(
const std::exception& e)
244 std::cout << __PRETTY_FUNCTION__ <<
"Error: " << e.what() << std::endl;
246 fConnectedClients.erase(it--);
252 void TCPServerBase::shutdownAccept()
255 shutdown(getSocketId(), SHUT_RD);