diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 670f0d24..8e015bbd 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -1,23 +1,6 @@ #include "NetworkServer.h" #include -//Include thread libraries for Windows or Linux -#ifdef WIN32 -#include -#else -#include "pthread.h" -#include "unistd.h" -#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 - #ifdef WIN32 #include #else @@ -29,42 +12,16 @@ static void Sleep(unsigned int milliseconds) } #endif -typedef struct listen_thread_param_type -{ - NetworkServer * this_ptr; - SOCKET * sock_ptr; -}; - -THREAD connection_thread(void *param) -{ - NetworkServer* server = static_cast(param); - server->ConnectionThread(); - THREADRETURN -} - -THREAD listen_thread(void *param) -{ - NetworkServer* server = static_cast(param)->this_ptr; - SOCKET* sock = static_cast(param)->sock_ptr; - server->ListenThread(sock); - THREADRETURN -} - NetworkServer::NetworkServer(std::vector& control) : controllers(control) { //Start a TCP server and launch threads port.tcp_server("1337"); //Start the connection thread -#ifdef WIN32 - _beginthread(connection_thread, 0, this); -#else - pthread_t thread; - pthread_create(&thread, NULL, &connection_thread, this); -#endif + ConnectionThread = new std::thread(&NetworkServer::ConnectionThreadFunction, this); } -void NetworkServer::ConnectionThread() +void NetworkServer::ConnectionThreadFunction() { //This thread handles client connections @@ -72,24 +29,15 @@ void NetworkServer::ConnectionThread() while(1) { SOCKET * client_sock = port.tcp_server_listen(); - //Start a listener thread for the new client socket -#ifdef WIN32 - listen_thread_param_type new_thread_param; - new_thread_param.sock_ptr = client_sock; - new_thread_param.this_ptr = this; - _beginthread(listen_thread, 0, &new_thread_param); -#else - pthread_t thread; - listen_thread_param_type new_thread_param; - new_thread_param.sock_ptr = client_sock; - new_thread_param.this_ptr = this; - pthread_create(&thread, NULL, &listen_thread, &new_thread_param); -#endif + //Start a listener thread for the new client socket + std::thread * NewListenThread = new std::thread(&NetworkServer::ListenThreadFunction, this, client_sock); + + ListenThreads.push_back(NewListenThread); } } -void NetworkServer::ListenThread(SOCKET * client_sock) +void NetworkServer::ListenThreadFunction(SOCKET * client_sock) { printf("Network server started\n"); //This thread handles messages received from clients diff --git a/NetworkServer.h b/NetworkServer.h index ad0b9390..cd286514 100644 --- a/NetworkServer.h +++ b/NetworkServer.h @@ -2,6 +2,8 @@ #include "NetworkProtocol.h" #include "net_port.h" +#include + #pragma once class NetworkServer @@ -9,15 +11,18 @@ class NetworkServer public: NetworkServer(std::vector& control); - void ConnectionThread(); - void ListenThread(SOCKET * client_sock); + void ConnectionThreadFunction(); + void ListenThreadFunction(SOCKET * client_sock); void SendReply_ControllerCount(SOCKET * client_sock); void SendReply_ControllerData(SOCKET * client_sock, unsigned int dev_idx); protected: - std::vector& controllers; + std::vector& controllers; + + std::vector ListenThreads; + std::thread * ConnectionThread; private: net_port port; -}; \ No newline at end of file +};