diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-21 15:24:49 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-03-21 15:24:49 +0000 |
commit | 4a20e71c66c0b7bbdd73ab26b3ca8c0d7f4c8b64 (patch) | |
tree | 67fc6704e4735cbc6f21d368dba349e9aed768eb /src/netsession.cpp | |
parent | 28f6096586bf4e3465140f48f1eae54f11fef88d (diff) | |
download | manaserv-4a20e71c66c0b7bbdd73ab26b3ca8c0d7f4c8b64.tar.gz manaserv-4a20e71c66c0b7bbdd73ab26b3ca8c0d7f4c8b64.tar.bz2 manaserv-4a20e71c66c0b7bbdd73ab26b3ca8c0d7f4c8b64.tar.xz manaserv-4a20e71c66c0b7bbdd73ab26b3ca8c0d7f4c8b64.zip |
More complete implementation of startListen and stopListen, told CVS to ignore
some files and compile fixes (it actually compiles now, just don't expect it to
do anything useful)
Diffstat (limited to 'src/netsession.cpp')
-rw-r--r-- | src/netsession.cpp | 63 |
1 files changed, 51 insertions, 12 deletions
diff --git a/src/netsession.cpp b/src/netsession.cpp index 603ea7f7..3ad8da1a 100644 --- a/src/netsession.cpp +++ b/src/netsession.cpp @@ -22,7 +22,14 @@ */ #include "netsession.h" -#include <SDL_net.h> + + +int startListenThread(void *data) +{ + // So who will do the actual listening now? + return 0; +} + NetSession::NetSession() { @@ -39,23 +46,51 @@ void NetSession::startListen(ConnectionHandler *handler, Uint16 port) // will call connect/disconnect events on the given ConnectionHandler and // will cut incoming data into Packets and send them there too. - IPaddress address; - address.host = INADDR_ANY; - address.port = port; + ListenThreadData *data = new ListenThreadData; + + data->address.host = INADDR_ANY; + data->address.port = port; + data->handler = handler; + data->running = true; + data->socket = SDLNet_TCP_Open(&data->address); + + if (!data->socket) { + printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); + exit(3); + } + + data->thread = SDL_CreateThread(startListenThread, data); - TCPsocket tcpsock = SDLNet_TCP_Open(address); - if (!tcpsock) { - printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); - exit(3); + if (data->thread == NULL) { + printf("SDL_CreateThread: %s\n", SDL_GetError()); + exit(5); } + + listeners[port] = data; } void NetSession::stopListen(Uint16 port) { - // Here all open connections on this port will be closed, and listening - // thread is stopped. + std::map<Uint16, ListenThreadData*>::iterator threadDataI; + threadDataI = listeners.find(port); + + if (threadDataI != listeners.end()) + { + ListenThreadData *data = (*threadDataI).second; - // void SDLNet_TCP_Close(TCPsocket sock); + // Tell listen thread to stop running + data->running = false; + + // 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); + SDLNet_TCP_Close(data->socket); + } + else + { + printf("NetSession::stopListen() not listening to port %d!\n", port); + } } NetComputer *NetSession::connect(const std::string &host, Uint16 port) @@ -69,14 +104,18 @@ NetComputer *NetSession::connect(const std::string &host, Uint16 port) if (!SDLNet_ResolveHost(&address, host.c_str(), port)) { - TCPsocket tcpsock = SDLNet_TCP_Open(IPaddress *ip); + TCPsocket tcpsock = SDLNet_TCP_Open(&address); if (!tcpsock) { printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError()); exit(3); } + + // return computer; } else { printf("SDLNet_ResolveHost: Could not resolve %s\n", host.c_str()); exit(4); } + + return NULL; } |