diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 443a9fe6..c5975b99 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -100,6 +100,8 @@ void NetworkClient::StartClient() port.tcp_client(port_ip, port_str); + client_active = true; + //Start the connection thread ConnectionThread = new std::thread(&NetworkClient::ConnectionThreadFunction, this); @@ -111,7 +113,18 @@ void NetworkClient::StartClient() void NetworkClient::StopClient() { + server_connected = false; + client_active = false; + shutdown(client_sock, SD_RECEIVE); + closesocket(client_sock); + ListenThread->join(); + ConnectionThread->join(); + + /*-------------------------------------------------*\ + | Client info has changed, call the callbacks | + \*-------------------------------------------------*/ + ClientInfoChanged(); } void NetworkClient::ConnectionThreadFunction() @@ -119,7 +132,7 @@ void NetworkClient::ConnectionThreadFunction() unsigned int requested_controllers; //This thread manages the connection to the server - while(1) + while(client_active == true) { if(server_connected == false) { @@ -243,7 +256,7 @@ void NetworkClient::ListenThreadFunction() { printf("Network client listener started\n"); //This thread handles messages received from the server - while(server_connected = true) + while(server_connected == true) { NetPacketHeader header; int bytes_read = 0; diff --git a/NetworkClient.h b/NetworkClient.h index 1727b56c..1e584caf 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -70,6 +70,7 @@ private: net_port port; char port_ip[20]; unsigned short port_num; + bool client_active; bool server_connected; bool server_initialized; unsigned int server_controller_count;