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 | |
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)
-rw-r--r-- | .cvsignore | 13 | ||||
-rw-r--r-- | src/.cvsignore | 2 | ||||
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/connectionhandler.cpp | 6 | ||||
-rw-r--r-- | src/connectionhandler.h | 1 | ||||
-rw-r--r-- | src/debug.h | 2 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/messagehandler.cpp | 45 | ||||
-rw-r--r-- | src/messagehandler.h | 24 | ||||
-rw-r--r-- | src/messagein.h | 1 | ||||
-rw-r--r-- | src/messageout.cpp | 2 | ||||
-rw-r--r-- | src/netcomputer.h | 3 | ||||
-rw-r--r-- | src/netsession.cpp | 63 | ||||
-rw-r--r-- | src/netsession.h | 32 |
14 files changed, 146 insertions, 58 deletions
diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 00000000..9f17923d --- /dev/null +++ b/.cvsignore @@ -0,0 +1,13 @@ +Makefile +Makefile.in +aclocal.m4 +autom4te.cache +config.h +config.h.in +config.log +config.status +configure +depcomp +install-sh +missing +stamp-h1 diff --git a/src/.cvsignore b/src/.cvsignore new file mode 100644 index 00000000..282522db --- /dev/null +++ b/src/.cvsignore @@ -0,0 +1,2 @@ +Makefile +Makefile.in diff --git a/src/Makefile.am b/src/Makefile.am index 94ff110e..5915f280 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,8 @@ bin_PROGRAMS = tmwserv tmwserv_SOURCES = main.cpp \ connectionhandler.h \ connectionhandler.cpp \ + debug.h \ + debug.cpp \ messagehandler.h \ messagehandler.cpp \ messagein.h \ @@ -19,7 +21,7 @@ tmwserv_SOURCES = main.cpp \ INCLUDES= $(all_includes) # the library search path. -tmwserv_LDFLAGS = $(all_libraries) $(LIBSDL_RPATH) `pkg-config --libs libxml-2.0` -tmwserv_CXXFLAGS = -g -Wall $(LIBSDL_CFLAGS) `pkg-config --cflags libxml-2.0` -fno-inline -tmwserv_LDADD = $(LIBSDL_LIBS) -lphysfs +tmwserv_LDFLAGS = $(all_libraries) -lSDL_net `sdl-config --libs` `pkg-config --libs libxml-2.0` -lphysfs +tmwserv_CXXFLAGS = -g -Wall `sdl-config --cflags` `pkg-config --cflags libxml-2.0` -fno-inline +#tmwserv_LDADD = $(LIBSDL_LIBS) -lphysfs tmwserv_TARGET = ../tmwserv diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index fba5d7dc..c843140c 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -23,8 +23,12 @@ #include "connectionhandler.h" +ConnectionHandler::ConnectionHandler() +{ +} -ConnectionHandler::registerHandler(unsigned int msgId, MessageHandler *handler) +void ConnectionHandler::registerHandler( + unsigned int msgId, MessageHandler *handler) { handlers[msgId] = handler; } diff --git a/src/connectionhandler.h b/src/connectionhandler.h index d8bdba91..bc5f0965 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -24,6 +24,7 @@ #ifndef _TMW_SERVER_CONNECTIONHANDLER_ #define _TMW_SERVER_CONNECTIONHANDLER_ +#include "messagehandler.h" #include "netcomputer.h" #include "packet.h" #include <map> diff --git a/src/debug.h b/src/debug.h index 754fe277..b024935e 100644 --- a/src/debug.h +++ b/src/debug.h @@ -39,7 +39,7 @@ extern void debugCatch(int result); #define TMW_SUCCESS 1 // the function completed successfully // ACCOUNT -#define TMW_ACCOUNTERROR_NOEXIST 101 +#define TMW_ACCOUNTERROR_NOEXIST 100 #define TMW_ACCOUNTERROR_BANNED 101 #define TMW_ACCOUNTERROR_ALREADYASSIGNED 102 #define TMW_ACCOUNTERROR_CHARNOTFOUND 103 diff --git a/src/main.cpp b/src/main.cpp index b90e0286..e7db308d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,7 +64,7 @@ void initialize() } // Initialize world timer at 10 times per second - worldTimerID = SDL_AddTimer(100, world_tick, NULL); + worldTimerID = SDL_AddTimer(100, worldTick, NULL); } /** diff --git a/src/messagehandler.cpp b/src/messagehandler.cpp index cc79d45e..52187076 100644 --- a/src/messagehandler.cpp +++ b/src/messagehandler.cpp @@ -24,45 +24,31 @@ #include "messagehandler.h" #include "debug.h" -/* recieveMessage - * This function recieves a message, then sends it to the appropriate handler - * sub-routine for processing. - * Preconditions: valid parameters, queue initialized, etc. - * Postconditions: message successfully processed. - */ -void MessageHandler::receiveMessage(NetComputer *computer, MessageIn &message) +/* +void AccountHandler::receiveMessage(NetComputer *computer, MessageIn &message) { int result = 0; // determine message type - /* switch(message.type) - * { - * case: TYPE_LOGIN - * result = loginMessage(computer, message); - * break; - * } - */ - - debugCatch(result); -} + switch(message.type) + { + case TYPE_LOGIN: + result = loginMessage(computer, message); + break; + } + debugCatch(result); +} +*/ -/* loginMessage - * Accepts a login message and interprets it, assigning the proper login - * Preconditions: The requested handle is not logged in already. - * The requested handle exists. - * The requested handle is not banned or restricted. - * The character profile is valid - * Postconditions: the player recieves access through a character in the world. - * Return Value: SUCCESS if the player was successfully assigned the requested char - * ERROR on early termination of the routine. - */ int MessageHandler::loginMessage(NetComputer *computer, MessageIn &message) { // Get the handle (account) the player is requesting // RETURN TMW_ACCOUNTERROR_NOEXIST if: requested does not handle exist - // RETURN TMW_ACCOUNTERROR_BANNED if: the handle status is HANDLE_STATUS_BANNED - // RETURN TMW_ACCOUNTERROR_ALREADYASSIGNED if: the handle is already assigned + // RETURN TMW_ACCOUNTERROR_BANNED if: the handle status is + // HANDLE_STATUS_BANNED + // RETURN TMW_ACCOUNTERROR_ALREADYASSIGNED if: the handle is already + // assigned // Get the character within that handle that the player is requesting // RETURN TMW_ACCOUNTERROR_CHARNOTFOUND if: character not found @@ -71,4 +57,5 @@ int MessageHandler::loginMessage(NetComputer *computer, MessageIn &message) // RETURN TMW_ACCOUNTERROR_ASSIGNFAILED if: assignment not successful // return TMW_SUCCESS -- successful exit + return TMW_SUCCESS; } diff --git a/src/messagehandler.h b/src/messagehandler.h index 3d9e8a50..16f5251e 100644 --- a/src/messagehandler.h +++ b/src/messagehandler.h @@ -39,15 +39,25 @@ class MessageHandler /** * Called when a message is received with a message ID that corresponds * to an ID this message handler registered to handle. - * - * Note: A MessageIn is used here which hasn't been defined yet, but - * I imagine that Packet instances would be very simple, and wrapped - * by both a MessageIn and a MessageOut class that would implement - * methods to convenient parse and build packets transparently. */ - void receiveMessage(NetComputer *computer, MessageIn &message); + virtual void receiveMessage( + NetComputer *computer, MessageIn &message) = 0; - void loginMessage(NetComputer *computer, MessageIn &message); + // To be moved to "AccountHandler" + /** + * Accepts a login message and interprets it, assigning the proper + * login + * Preconditions: The requested handle is not logged in already. + * The requested handle exists. + * The requested handle is not banned or restricted. + * The character profile is valid + * Postconditions: The player recieves access through a character in + * the world. + * Return Value: SUCCESS if the player was successfully assigned the + * requested char, ERROR on early termination of the + * routine. + */ + int loginMessage(NetComputer *computer, MessageIn &message); }; #endif diff --git a/src/messagein.h b/src/messagein.h index 92a0a22c..bb12ad05 100644 --- a/src/messagein.h +++ b/src/messagein.h @@ -25,6 +25,7 @@ #define _TMW_SERVER_MESSAGEIN_ #include "packet.h" +#include <string> /** * A helper class to diff --git a/src/messageout.cpp b/src/messageout.cpp index 7542b693..d9393667 100644 --- a/src/messageout.cpp +++ b/src/messageout.cpp @@ -24,7 +24,7 @@ #include "messageout.h" MessageOut::MessageOut(): - packet(NULL) + packet(0) { } diff --git a/src/netcomputer.h b/src/netcomputer.h index be730941..02948207 100644 --- a/src/netcomputer.h +++ b/src/netcomputer.h @@ -24,6 +24,9 @@ #ifndef _TMW_SERVER_NETCOMPUTER_ #define _TMW_SERVER_NETCOMPUTER_ +#include "packet.h" +#include <string> + // Forward declaration class NetSession; 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; } diff --git a/src/netsession.h b/src/netsession.h index c0c92b2a..b313e2fc 100644 --- a/src/netsession.h +++ b/src/netsession.h @@ -27,6 +27,22 @@ #include "netcomputer.h" #include "connectionhandler.h" #include <SDL.h> +#include <SDL_thread.h> +#include <SDL_net.h> +#include <map> + +/** + * Data communicated to a new listen thread. The <code>running</code> member is + * set to <code>false</code> to tell the thread to stop listening. + */ +struct ListenThreadData +{ + IPaddress address; /**< Includes the port to listen to. */ + TCPsocket socket; /**< The socket that's been opened. */ + SDL_Thread *thread; /**< The thread, ignored by thread itself. */ + ConnectionHandler *handler; /**< Handler for events. */ + bool running; /**< Wether to keep listening. */ +}; /** * This class represents a network session. It implements listening for @@ -48,11 +64,17 @@ class NetSession /** * Start listening for connections and notify the given connection * handler about events. + * + * This method opens a socket on the given port and starts a new thread + * 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); /** * 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); @@ -62,10 +84,14 @@ class NetSession NetComputer *connect(const std::string &ip, Uint16 port); private: - // This class probably needs to keep information about: + /** + * 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; + + // Other information we need to keep: // - // - The list of ports we're listening to and their associated - // connection handlers. // - The list of clients that connected and their associated net // computers. // - The list of servers we connected to and their associated net |