summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEugenio Favalli <elvenprogrammer@gmail.com>2006-04-28 14:17:10 +0000
committerEugenio Favalli <elvenprogrammer@gmail.com>2006-04-28 14:17:10 +0000
commita4d1a5c05c869d1632feb9e933d4df5a61c5a66f (patch)
tree8713906788ebd9d169d7629bc87e800fd37adbd4 /src
parent184aae8cb1fb072b40d63f69c569d92ad7ed2eef (diff)
downloadmanaserv-a4d1a5c05c869d1632feb9e933d4df5a61c5a66f.tar.gz
manaserv-a4d1a5c05c869d1632feb9e933d4df5a61c5a66f.tar.bz2
manaserv-a4d1a5c05c869d1632feb9e933d4df5a61c5a66f.tar.xz
manaserv-a4d1a5c05c869d1632feb9e933d4df5a61c5a66f.zip
Got rid of SDL threads, now using pthreads.
Diffstat (limited to 'src')
-rw-r--r--src/netsession.cpp26
-rw-r--r--src/netsession.h12
2 files changed, 21 insertions, 17 deletions
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 <SDL.h>
-
#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<Uint16, ListenThreadData*>::iterator threadDataI;
+ std::map<enet_uint16, ListenThreadData*>::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 <map>
-#include <SDL_thread.h>
+#include <pthread.h>
#include <enet/enet.h>
@@ -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<Uint16, ListenThreadData*> listeners;
+ std::map<unsigned short, ListenThreadData*> listeners;
// Other information we need to keep:
//