Network shutdown optimization
This commit is contained in:
parent
2c952a54d2
commit
d612b8b8de
2 changed files with 56 additions and 7 deletions
|
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include "NetworkClient.h"
|
#include "NetworkClient.h"
|
||||||
|
#include "LogManager.h"
|
||||||
#include "RGBController_Network.h"
|
#include "RGBController_Network.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
@ -163,6 +164,7 @@ void NetworkClient::StartClient()
|
||||||
|
|
||||||
void NetworkClient::StopClient()
|
void NetworkClient::StopClient()
|
||||||
{
|
{
|
||||||
|
LOG_TRACE("[NetworkClient]: StopClient started");
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| Disconnect the server and set it as inactive |
|
| Disconnect the server and set it as inactive |
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
|
|
@ -181,6 +183,8 @@ void NetworkClient::StopClient()
|
||||||
client_active = false;
|
client_active = false;
|
||||||
server_connected = false;
|
server_connected = false;
|
||||||
|
|
||||||
|
LOG_TRACE("[NetworkClient]: Socket shut down");
|
||||||
|
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| Close the listen thread |
|
| Close the listen thread |
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
|
|
@ -191,26 +195,35 @@ void NetworkClient::StopClient()
|
||||||
ListenThread = nullptr;
|
ListenThread = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG_TRACE("[NetworkClient]: ListenThread stopped");
|
||||||
|
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| Close the connection thread |
|
| Close the connection thread |
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
if(ConnectionThread)
|
if(ConnectionThread)
|
||||||
{
|
{
|
||||||
|
connection_cv.notify_all();
|
||||||
ConnectionThread->join();
|
ConnectionThread->join();
|
||||||
delete ConnectionThread;
|
delete ConnectionThread;
|
||||||
ConnectionThread = nullptr;
|
ConnectionThread = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LOG_TRACE("[NetworkClient]: ConnectionThread stopped");
|
||||||
|
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| Client info has changed, call the callbacks |
|
| Client info has changed, call the callbacks |
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
ClientInfoChanged();
|
ClientInfoChanged();
|
||||||
|
|
||||||
|
LOG_TRACE("[NetworkClient]: ClientInfoChange notified, StopClient finished");
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkClient::ConnectionThreadFunction()
|
void NetworkClient::ConnectionThreadFunction()
|
||||||
{
|
{
|
||||||
unsigned int requested_controllers;
|
unsigned int requested_controllers;
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(connection_mutex);
|
||||||
|
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| This thread manages the connection to the server |
|
| This thread manages the connection to the server |
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
|
|
@ -229,6 +242,7 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
if(port.tcp_client_connect() == true)
|
if(port.tcp_client_connect() == true)
|
||||||
{
|
{
|
||||||
client_sock = port.sock;
|
client_sock = port.sock;
|
||||||
|
LOG_TRACE("[NetworkClient]: Connected to server [%s]", port_ip.c_str());
|
||||||
printf( "Connected to server\n" );
|
printf( "Connected to server\n" );
|
||||||
|
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
|
|
@ -253,11 +267,16 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
LOG_TRACE("[NetworkClient]: Connection attempt failed");
|
||||||
printf( "Connection attempt failed\n" );
|
printf( "Connection attempt failed\n" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(server_initialized == false && server_connected == true)
|
/*-----------------------------------------------------------*\
|
||||||
|
| TIP: we double-check client_active as it could have changed |
|
||||||
|
\*-----------------------------------------------------------*/
|
||||||
|
|
||||||
|
if(client_active && server_initialized == false && server_connected == true)
|
||||||
{
|
{
|
||||||
unsigned int timeout_counter = 0;
|
unsigned int timeout_counter = 0;
|
||||||
requested_controllers = 0;
|
requested_controllers = 0;
|
||||||
|
|
@ -268,7 +287,11 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| Wait for server to connect |
|
| Wait for server to connect |
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
std::this_thread::sleep_for(100ms);
|
connection_cv.wait_for(lock, 100ms);
|
||||||
|
if(!client_active)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
/*---------------------------------------------------------*\
|
/*---------------------------------------------------------*\
|
||||||
| Request protocol version |
|
| Request protocol version |
|
||||||
|
|
@ -280,7 +303,11 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
while(!server_protocol_version_received)
|
while(!server_protocol_version_received)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(5ms);
|
connection_cv.wait_for(lock, 5ms);
|
||||||
|
if(!client_active)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
timeout_counter++;
|
timeout_counter++;
|
||||||
|
|
||||||
|
|
@ -311,7 +338,11 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
while(!server_controller_count_received)
|
while(!server_controller_count_received)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(5ms);
|
connection_cv.wait_for(lock, 5ms);
|
||||||
|
if(!client_active)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Client: Received controller count from server: %d\r\n", server_controller_count);
|
printf("Client: Received controller count from server: %d\r\n", server_controller_count);
|
||||||
|
|
@ -331,7 +362,11 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
\*---------------------------------------------------------*/
|
\*---------------------------------------------------------*/
|
||||||
while(controller_data_received == false)
|
while(controller_data_received == false)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(5ms);
|
connection_cv.wait_for(lock, 5ms);
|
||||||
|
if(!client_active)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
requested_controllers++;
|
requested_controllers++;
|
||||||
|
|
@ -358,8 +393,18 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
ClientInfoChanged();
|
ClientInfoChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::this_thread::sleep_for(1s);
|
/*---------------------------------------------------------*\
|
||||||
|
| Wait 1 sec or until the thread is requested to stop |
|
||||||
|
\*---------------------------------------------------------*/
|
||||||
|
|
||||||
|
LOG_TRACE("[NetworkClient]: Waiting 1s before the next iteration");
|
||||||
|
connection_cv.wait_for(lock, 1s);
|
||||||
}
|
}
|
||||||
|
/*---------------------------------------------------------*\
|
||||||
|
| lock is unlocked automatically |
|
||||||
|
\*---------------------------------------------------------*/
|
||||||
|
|
||||||
|
LOG_TRACE("[NetworkClient]: ConnectionThread exited");
|
||||||
}
|
}
|
||||||
|
|
||||||
int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags)
|
int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags)
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <condition_variable>
|
||||||
#include "RGBController.h"
|
#include "RGBController.h"
|
||||||
#include "NetworkProtocol.h"
|
#include "NetworkProtocol.h"
|
||||||
#include "net_port.h"
|
#include "net_port.h"
|
||||||
|
|
@ -93,7 +94,7 @@ private:
|
||||||
net_port port;
|
net_port port;
|
||||||
std::string port_ip;
|
std::string port_ip;
|
||||||
unsigned short port_num;
|
unsigned short port_num;
|
||||||
bool client_active;
|
std::atomic<bool> client_active;
|
||||||
bool controller_data_received;
|
bool controller_data_received;
|
||||||
bool server_connected;
|
bool server_connected;
|
||||||
bool server_initialized;
|
bool server_initialized;
|
||||||
|
|
@ -104,6 +105,9 @@ private:
|
||||||
bool change_in_progress;
|
bool change_in_progress;
|
||||||
std::mutex send_in_progress;
|
std::mutex send_in_progress;
|
||||||
|
|
||||||
|
std::mutex connection_mutex;
|
||||||
|
std::condition_variable connection_cv;
|
||||||
|
|
||||||
std::thread * ConnectionThread;
|
std::thread * ConnectionThread;
|
||||||
std::thread * ListenThread;
|
std::thread * ListenThread;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue