diff --git a/NetworkClient.cpp b/NetworkClient.cpp index c16c0e20..9f274e63 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -13,29 +13,6 @@ static void Sleep(unsigned int milliseconds) } #endif -//Thread functions have different types in Windows and Linux -#ifdef WIN32 -#define THREAD static void -#define THREADRETURN -#else -#define THREAD static void* -#define THREADRETURN return(NULL); -#endif - -THREAD connection_thread(void *param) -{ - NetworkClient* client = static_cast(param); - client->ConnectionThread(); - THREADRETURN -} - -THREAD listen_thread(void *param) -{ - NetworkClient* client = static_cast(param); - client->ListenThread(); - THREADRETURN -} - NetworkClient::NetworkClient(std::vector& control) : controllers(control) { unsigned int requested_controllers; @@ -47,14 +24,10 @@ NetworkClient::NetworkClient(std::vector& control) : controller requested_controllers = 0; //Start the connection thread -#ifdef WIN32 - _beginthread(connection_thread, 0, this); - _beginthread(listen_thread, 0, this); -#else - pthread_t thread; - pthread_create(&thread, NULL, &connection_thread, this); - pthread_create(&thread, NULL, &listen_thread, this); -#endif + ConnectionThread = new std::thread(&NetworkClient::ConnectionThreadFunction, this); + + //Start the listener thread + ListenThread = new std::thread(&NetworkClient::ListenThreadFunction, this); //Wait for server to connect while(!server_connected) @@ -97,7 +70,7 @@ NetworkClient::NetworkClient(std::vector& control) : controller } } -void NetworkClient::ConnectionThread() +void NetworkClient::ConnectionThreadFunction() { //This thread manages the connection to the server while(1) @@ -117,7 +90,7 @@ void NetworkClient::ConnectionThread() } } -void NetworkClient::ListenThread() +void NetworkClient::ListenThreadFunction() { printf("Network client listener started\n"); //This thread handles messages received from the server diff --git a/NetworkClient.h b/NetworkClient.h index a0f62538..fadc2f33 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -2,6 +2,8 @@ #include "NetworkProtocol.h" #include "net_port.h" +#include + #pragma once class NetworkClient @@ -9,8 +11,8 @@ class NetworkClient public: NetworkClient(std::vector& control); - void ConnectionThread(); - void ListenThread(); + void ConnectionThreadFunction(); + void ListenThreadFunction(); void ProcessReply_ControllerCount(unsigned int data_size, char * data); void ProcessReply_ControllerData(unsigned int data_size, char * data, unsigned int dev_idx); @@ -36,4 +38,7 @@ private: net_port port; bool server_connected; unsigned int server_controller_count; -}; \ No newline at end of file + + std::thread * ConnectionThread; + std::thread * ListenThread; +};