From a4d1a5c05c869d1632feb9e933d4df5a61c5a66f Mon Sep 17 00:00:00 2001 From: Eugenio Favalli Date: Fri, 28 Apr 2006 14:17:10 +0000 Subject: Got rid of SDL threads, now using pthreads. --- src/netsession.cpp | 26 +++++++++++++++----------- src/netsession.h | 12 ++++++------ 2 files changed, 21 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/netsession.cpp b/src/netsession.cpp index 0a56d7c0..260a649f 100644 --- a/src/netsession.cpp +++ b/src/netsession.cpp @@ -23,8 +23,6 @@ #include "netsession.h" -#include - #include "connectionhandler.h" #include "utils/logger.h" @@ -34,11 +32,11 @@ * immediately passes control over to the connection handler instance that will * deal with incoming connections and data. */ -int startListenThread(void *data) +void *startListenThread(void *data) { ListenThreadData *ltd = (ListenThreadData*)data; ltd->handler->startListen(ltd); - return 0; + pthread_exit(NULL); } @@ -51,7 +49,7 @@ NetSession::~NetSession() // Stop listening to any ports } -void NetSession::startListen(ConnectionHandler *handler, Uint16 port) +void NetSession::startListen(ConnectionHandler *handler, enet_uint16 port) { // Here we will probably need the creation of a listening thread, which // will call connect/disconnect events on the given ConnectionHandler and @@ -81,19 +79,25 @@ void NetSession::startListen(ConnectionHandler *handler, Uint16 port) data->host = server; // Start the listening thread - data->thread = SDL_CreateThread(startListenThread, data); + int rc = pthread_create(&data->thread, NULL, + startListenThread, (void *)data); + if (rc) { + LOG_ERROR("pthread_create: " << rc, 0); + exit(4); + } + /*data->thread = SDL_CreateThread(startListenThread, data); if (data->thread == NULL) { LOG_ERROR("SDL_CreateThread: " << SDL_GetError(), 0); exit(4); - } + }*/ listeners[port] = data; } -void NetSession::stopListen(Uint16 port) +void NetSession::stopListen(enet_uint16 port) { - std::map::iterator threadDataI; + std::map::iterator threadDataI; threadDataI = listeners.find(port); if (threadDataI != listeners.end()) @@ -106,7 +110,7 @@ void NetSession::stopListen(Uint16 port) // Wait for listen thread to stop and close socket // Note: Somewhere in this process the ConnectionHandler should receive // disconnect notifications about all the connected clients. - SDL_WaitThread(data->thread, NULL); + //SDL_WaitThread(data->thread, NULL); enet_host_destroy(data->host); delete data; listeners.erase(threadDataI); @@ -117,7 +121,7 @@ void NetSession::stopListen(Uint16 port) } } -NetComputer *NetSession::connect(const std::string &host, Uint16 port) +NetComputer *NetSession::connect(const std::string &host, enet_uint16 port) { // Try to connect to given host:port, and return NetComputer objects that // can be used to send messages that way, or NULL when failing to connect. diff --git a/src/netsession.h b/src/netsession.h index e50cc6e7..fe893055 100644 --- a/src/netsession.h +++ b/src/netsession.h @@ -25,7 +25,7 @@ #define _TMWSERV_NETSESSION_H_ #include -#include +#include #include @@ -40,7 +40,7 @@ struct ListenThreadData { ENetAddress address; /**< Includes the port to listen to. */ ENetHost *host; /**< The host that listen for connections. */ - SDL_Thread *thread; /**< The thread, ignored by thread itself. */ + pthread_t thread; /**< The thread, ignored by thread itself. */ ConnectionHandler *handler; /**< Handler for events. */ bool running; /**< Wether to keep listening. */ }; @@ -70,26 +70,26 @@ class NetSession * that will handle listening for new connections and incoming data * over this port. The connection handler will need to be thread safe. */ - void startListen(ConnectionHandler *handler, Uint16 port); + void startListen(ConnectionHandler *handler, enet_uint16 port); /** * Stop listening for connections and disconnect any connected clients. * This is done by signalling the listening thread to stop running, and * closing the socket when it stopped. */ - void stopListen(Uint16 port); + void stopListen(unsigned short port); /** * Connect to another network session. */ - NetComputer *connect(const std::string &ip, Uint16 port); + NetComputer *connect(const std::string &ip, unsigned short port); private: /** * The list of ports we're listening to and their associated thread * data, including the connection handler and wether to keep listening. */ - std::map listeners; + std::map listeners; // Other information we need to keep: // -- cgit v1.2.3-70-g09d2