Clean up comment formatting in NetworkServer.cpp

This commit is contained in:
Adam Honse 2024-03-25 17:50:32 -05:00
parent 94684a0a78
commit 9215ae0514

View file

@ -75,9 +75,9 @@ void NetworkServer::ClientInfoChanged()
{ {
ClientInfoChangeMutex.lock(); ClientInfoChangeMutex.lock();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Client info has changed, call the callbacks | | Client info has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < ClientInfoChangeCallbacks.size(); callback_idx++) for(unsigned int callback_idx = 0; callback_idx < ClientInfoChangeCallbacks.size(); callback_idx++)
{ {
ClientInfoChangeCallbacks[callback_idx](ClientInfoChangeCallbackArgs[callback_idx]); ClientInfoChangeCallbacks[callback_idx](ClientInfoChangeCallbackArgs[callback_idx]);
@ -88,10 +88,10 @@ void NetworkServer::ClientInfoChanged()
void NetworkServer::DeviceListChanged() void NetworkServer::DeviceListChanged()
{ {
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Indicate to the clients that the controller list | | Indicate to the clients that the controller list has |
| has changed | | changed |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
for(unsigned int client_idx = 0; client_idx < ServerClients.size(); client_idx++) for(unsigned int client_idx = 0; client_idx < ServerClients.size(); client_idx++)
{ {
SendRequest_DeviceListChanged(ServerClients[client_idx]->client_sock); SendRequest_DeviceListChanged(ServerClients[client_idx]->client_sock);
@ -102,9 +102,9 @@ void NetworkServer::ServerListeningChanged()
{ {
ServerListeningChangeMutex.lock(); ServerListeningChangeMutex.lock();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Server state has changed, call the callbacks | | Server state has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
for(unsigned int callback_idx = 0; callback_idx < ServerListeningChangeCallbacks.size(); callback_idx++) for(unsigned int callback_idx = 0; callback_idx < ServerListeningChangeCallbacks.size(); callback_idx++)
{ {
ServerListeningChangeCallbacks[callback_idx](ServerListeningChangeCallbackArgs[callback_idx]); ServerListeningChangeCallbacks[callback_idx](ServerListeningChangeCallbackArgs[callback_idx]);
@ -230,15 +230,18 @@ void NetworkServer::StartServer()
{ {
int err; int err;
struct addrinfo hints, *res, *result; struct addrinfo hints, *res, *result;
//Start a TCP server and launch threads
/*---------------------------------------------------------*\
| Start a TCP server and launch threads |
\*---------------------------------------------------------*/
char port_str[6]; char port_str[6];
snprintf(port_str, 6, "%d", port_num); snprintf(port_str, 6, "%d", port_num);
socket_count = 0; socket_count = 0;
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Windows requires WSAStartup before using sockets | | Windows requires WSAStartup before using sockets |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
#ifdef WIN32 #ifdef WIN32
if(WSAStartup(MAKEWORD(2, 2), &wsa) != NO_ERROR) if(WSAStartup(MAKEWORD(2, 2), &wsa) != NO_ERROR)
{ {
@ -260,9 +263,9 @@ void NetworkServer::StartServer()
return; return;
} }
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Create a server socket for each address returned. | | Create a server socket for each address returned. |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
for(res = result; res && socket_count < MAXSOCK; res = res->ai_next) for(res = result; res && socket_count < MAXSOCK; res = res->ai_next)
{ {
server_sock[socket_count] = socket(res->ai_family, res->ai_socktype, res->ai_protocol); server_sock[socket_count] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
@ -274,9 +277,9 @@ void NetworkServer::StartServer()
return; return;
} }
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Bind the server socket | | Bind the server socket |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
if(bind(server_sock[socket_count], res->ai_addr, res->ai_addrlen) == SOCKET_ERROR) if(bind(server_sock[socket_count], res->ai_addr, res->ai_addrlen) == SOCKET_ERROR)
{ {
if(errno == EADDRINUSE) if(errno == EADDRINUSE)
@ -301,8 +304,10 @@ void NetworkServer::StartServer()
} }
else else
{ {
// could be a linux specific error /*---------------------------------------------------------*\
// https://man7.org/linux/man-pages/man2/bind.2.html | errno could be a Linux specific error, see: |
| https://man7.org/linux/man-pages/man2/bind.2.html |
\*---------------------------------------------------------*/
printf("Error: Could not bind network socket, error code:%d\n", errno); printf("Error: Could not bind network socket, error code:%d\n", errno);
} }
@ -310,9 +315,9 @@ void NetworkServer::StartServer()
return; return;
} }
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Set socket options - no delay | | Set socket options - no delay |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
setsockopt(server_sock[socket_count], IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); setsockopt(server_sock[socket_count], IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
socket_count += 1; socket_count += 1;
@ -321,9 +326,9 @@ void NetworkServer::StartServer()
freeaddrinfo(result); freeaddrinfo(result);
server_online = true; server_online = true;
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Start the connection thread | | Start the connection thread |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
for(int curr_socket = 0; curr_socket < socket_count; curr_socket++) for(int curr_socket = 0; curr_socket < socket_count; curr_socket++)
{ {
ConnectionThread[curr_socket] = new std::thread(&NetworkServer::ConnectionThreadFunction, this, curr_socket); ConnectionThread[curr_socket] = new std::thread(&NetworkServer::ConnectionThreadFunction, this, curr_socket);
@ -364,29 +369,31 @@ void NetworkServer::StopServer()
socket_count = 0; socket_count = 0;
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Client info has changed, call the callbacks | | Client info has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
ClientInfoChanged(); ClientInfoChanged();
} }
void NetworkServer::ConnectionThreadFunction(int socket_idx) void NetworkServer::ConnectionThreadFunction(int socket_idx)
{ {
//This thread handles client connections /*---------------------------------------------------------*\
| This thread handles client connections |
\*---------------------------------------------------------*/
printf("Network connection thread started on port %hu\n", GetPort()); printf("Network connection thread started on port %hu\n", GetPort());
while(server_online == true) while(server_online == true)
{ {
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Create new socket for client connection | | Create new socket for client connection |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
NetworkClientInfo * client_info = new NetworkClientInfo(); NetworkClientInfo * client_info = new NetworkClientInfo();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Listen for incoming client connection on the | | Listen for incoming client connection on the server |
| server socket. This call blocks until a | | socket. This call blocks until a connection is |
| connection is established | | established |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
if(listen(server_sock[socket_idx], 10) < 0) if(listen(server_sock[socket_idx], 10) < 0)
{ {
printf("Connection thread closed\r\n"); printf("Connection thread closed\r\n");
@ -398,9 +405,9 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
server_listening = true; server_listening = true;
ServerListeningChanged(); ServerListeningChanged();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Accept the client connection | | Accept the client connection |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
client_info->client_sock = accept_select(server_sock[socket_idx]); client_info->client_sock = accept_select(server_sock[socket_idx]);
if(client_info->client_sock < 0) if(client_info->client_sock < 0)
@ -414,17 +421,17 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
return; return;
} }
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Get the new client socket and store it in the | | Get the new client socket and store it in the clients |
| clients vector | | vector |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
u_long arg = 0; u_long arg = 0;
ioctlsocket(client_info->client_sock, FIONBIO, &arg); ioctlsocket(client_info->client_sock, FIONBIO, &arg);
setsockopt(client_info->client_sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)); setsockopt(client_info->client_sock, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Discover the remote hosts IP | | Discover the remote hosts IP |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
struct sockaddr_storage tmp_addr; struct sockaddr_storage tmp_addr;
char ipstr[INET6_ADDRSTRLEN]; char ipstr[INET6_ADDRSTRLEN];
socklen_t len; socklen_t len;
@ -444,19 +451,23 @@ void NetworkServer::ConnectionThreadFunction(int socket_idx)
client_info->client_ip = ipstr; client_info->client_ip = ipstr;
} }
/* We need to lock before the thread could possibly finish */ /*---------------------------------------------------------*\
| We need to lock before the thread could possibly finish |
\*---------------------------------------------------------*/
ServerClientsMutex.lock(); ServerClientsMutex.lock();
//Start a listener thread for the new client socket /*---------------------------------------------------------*\
| Start a listener thread for the new client socket |
\*---------------------------------------------------------*/
client_info->client_listen_thread = new std::thread(&NetworkServer::ListenThreadFunction, this, client_info); client_info->client_listen_thread = new std::thread(&NetworkServer::ListenThreadFunction, this, client_info);
client_info->client_listen_thread->detach(); client_info->client_listen_thread->detach();
ServerClients.push_back(client_info); ServerClients.push_back(client_info);
ServerClientsMutex.unlock(); ServerClientsMutex.unlock();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Client info has changed, call the callbacks | | Client info has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
ClientInfoChanged(); ClientInfoChanged();
} }
@ -476,8 +487,8 @@ int NetworkServer::accept_select(int sockfd)
timeout.tv_sec = TCP_TIMEOUT_SECONDS; timeout.tv_sec = TCP_TIMEOUT_SECONDS;
timeout.tv_usec = 0; timeout.tv_usec = 0;
FD_ZERO(&set); /* clear the set */ FD_ZERO(&set);
FD_SET(sockfd, &set); /* add our file descriptor to the set */ FD_SET(sockfd, &set);
int rv = select(sockfd + 1, &set, NULL, NULL, &timeout); int rv = select(sockfd + 1, &set, NULL, NULL, &timeout);
@ -491,7 +502,6 @@ int NetworkServer::accept_select(int sockfd)
} }
else else
{ {
// socket has something to read
return(accept(sockfd, NULL, NULL)); return(accept(sockfd, NULL, NULL));
} }
} }
@ -507,8 +517,8 @@ int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags)
timeout.tv_sec = TCP_TIMEOUT_SECONDS; timeout.tv_sec = TCP_TIMEOUT_SECONDS;
timeout.tv_usec = 0; timeout.tv_usec = 0;
FD_ZERO(&set); /* clear the set */ FD_ZERO(&set);
FD_SET(s, &set); /* add our file descriptor to the set */ FD_SET(s, &set);
int rv = select(s + 1, &set, NULL, NULL, &timeout); int rv = select(s + 1, &set, NULL, NULL, &timeout);
@ -522,7 +532,6 @@ int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags)
} }
else else
{ {
// socket has something to read
return(recv(s, buf, len, flags)); return(recv(s, buf, len, flags));
} }
} }
@ -533,14 +542,19 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
SOCKET client_sock = client_info->client_sock; SOCKET client_sock = client_info->client_sock;
printf("Network server started\n"); printf("Network server started\n");
//This thread handles messages received from clients
/*---------------------------------------------------------*\
| This thread handles messages received from clients |
\*---------------------------------------------------------*/
while(server_online == true) while(server_online == true)
{ {
NetPacketHeader header; NetPacketHeader header;
int bytes_read = 0; int bytes_read = 0;
char * data = NULL; char * data = NULL;
//Read first byte of magic /*---------------------------------------------------------*\
| Read first byte of magic |
\*---------------------------------------------------------*/
bytes_read = recv_select(client_sock, &header.pkt_magic[0], 1, 0); bytes_read = recv_select(client_sock, &header.pkt_magic[0], 1, 0);
if(bytes_read <= 0) if(bytes_read <= 0)
@ -548,13 +562,17 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
goto listen_done; goto listen_done;
} }
//Test first character of magic - 'O' /*---------------------------------------------------------*\
| Test first character of magic - 'O' |
\*---------------------------------------------------------*/
if(header.pkt_magic[0] != 'O') if(header.pkt_magic[0] != 'O')
{ {
continue; continue;
} }
//Read second byte of magic /*---------------------------------------------------------*\
| Read second byte of magic |
\*---------------------------------------------------------*/
bytes_read = recv_select(client_sock, &header.pkt_magic[1], 1, 0); bytes_read = recv_select(client_sock, &header.pkt_magic[1], 1, 0);
if(bytes_read <= 0) if(bytes_read <= 0)
@ -562,13 +580,17 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
goto listen_done; goto listen_done;
} }
//Test second character of magic - 'R' /*---------------------------------------------------------*\
| Test second character of magic - 'R' |
\*---------------------------------------------------------*/
if(header.pkt_magic[1] != 'R') if(header.pkt_magic[1] != 'R')
{ {
continue; continue;
} }
//Read third byte of magic /*---------------------------------------------------------*\
| Read third byte of magic |
\*---------------------------------------------------------*/
bytes_read = recv_select(client_sock, &header.pkt_magic[2], 1, 0); bytes_read = recv_select(client_sock, &header.pkt_magic[2], 1, 0);
if(bytes_read <= 0) if(bytes_read <= 0)
@ -576,13 +598,17 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
goto listen_done; goto listen_done;
} }
//Test third character of magic - 'G' /*---------------------------------------------------------*\
| Test third character of magic - 'G' |
\*---------------------------------------------------------*/
if(header.pkt_magic[2] != 'G') if(header.pkt_magic[2] != 'G')
{ {
continue; continue;
} }
//Read fourth byte of magic /*---------------------------------------------------------*\
| Read fourth byte of magic |
\*---------------------------------------------------------*/
bytes_read = recv_select(client_sock, &header.pkt_magic[3], 1, 0); bytes_read = recv_select(client_sock, &header.pkt_magic[3], 1, 0);
if(bytes_read <= 0) if(bytes_read <= 0)
@ -590,13 +616,18 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
goto listen_done; goto listen_done;
} }
//Test fourth character of magic - 'B' /*---------------------------------------------------------*\
| Test fourth character of magic - 'B' |
\*---------------------------------------------------------*/
if(header.pkt_magic[3] != 'B') if(header.pkt_magic[3] != 'B')
{ {
continue; continue;
} }
//If we get to this point, the magic is correct. Read the rest of the header /*---------------------------------------------------------*\
| If we get to this point, the magic is correct. Read the |
| rest of the header |
\*---------------------------------------------------------*/
bytes_read = 0; bytes_read = 0;
do do
{ {
@ -613,7 +644,9 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
} while(bytes_read != sizeof(header) - sizeof(header.pkt_magic)); } while(bytes_read != sizeof(header) - sizeof(header.pkt_magic));
//Header received, now receive the data /*---------------------------------------------------------*\
| Header received, now receive the data |
\*---------------------------------------------------------*/
if(header.pkt_size > 0) if(header.pkt_size > 0)
{ {
bytes_read = 0; bytes_read = 0;
@ -635,7 +668,10 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
} while ((unsigned int)bytes_read < header.pkt_size); } while ((unsigned int)bytes_read < header.pkt_size);
} }
//Entire request received, select functionality based on request ID /*---------------------------------------------------------*\
| Entire request received, select functionality based on |
| request ID |
\*---------------------------------------------------------*/
switch(header.pkt_id) switch(header.pkt_id)
{ {
case NET_PACKET_ID_REQUEST_CONTROLLER_COUNT: case NET_PACKET_ID_REQUEST_CONTROLLER_COUNT:
@ -869,9 +905,9 @@ listen_done:
ServerClientsMutex.unlock(); ServerClientsMutex.unlock();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Client info has changed, call the callbacks | | Client info has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
ClientInfoChanged(); ClientInfoChanged();
} }
@ -900,9 +936,9 @@ void NetworkServer::ProcessRequest_ClientProtocolVersion(SOCKET client_sock, uns
} }
ServerClientsMutex.unlock(); ServerClientsMutex.unlock();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Client info has changed, call the callbacks | | Client info has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
ClientInfoChanged(); ClientInfoChanged();
} }
@ -919,9 +955,9 @@ void NetworkServer::ProcessRequest_ClientString(SOCKET client_sock, unsigned int
} }
ServerClientsMutex.unlock(); ServerClientsMutex.unlock();
/*-------------------------------------------------*\ /*---------------------------------------------------------*\
| Client info has changed, call the callbacks | | Client info has changed, call the callbacks |
\*-------------------------------------------------*/ \*---------------------------------------------------------*/
ClientInfoChanged(); ClientInfoChanged();
} }