diff options
author | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-09-23 14:08:21 +0000 |
---|---|---|
committer | Guillaume Melquiond <guillaume.melquiond@gmail.com> | 2007-09-23 14:08:21 +0000 |
commit | 0d00b4e7f49a0373d350353e62e08cdd903628e9 (patch) | |
tree | 6e2d50e6f19b08288c1463119c3d72508c77d8ff | |
parent | 5618f4890df5eb9ea91eb73a7a7dd7df745863a6 (diff) | |
download | manaserv-0d00b4e7f49a0373d350353e62e08cdd903628e9.tar.gz manaserv-0d00b4e7f49a0373d350353e62e08cdd903628e9.tar.bz2 manaserv-0d00b4e7f49a0373d350353e62e08cdd903628e9.tar.xz manaserv-0d00b4e7f49a0373d350353e62e08cdd903628e9.zip |
Simplified interface of the connection handler used for clients of the account server.
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | src/account-server/accounthandler.cpp | 257 | ||||
-rw-r--r-- | src/account-server/accounthandler.hpp | 133 | ||||
-rw-r--r-- | src/account-server/main-account.cpp | 12 | ||||
-rw-r--r-- | src/account-server/serverhandler.cpp | 3 |
5 files changed, 180 insertions, 232 deletions
@@ -14,6 +14,13 @@ * src/scripting/lua.cpp, src/game-server/testing.cpp, src/game-server/command.cpp, src/game-server/gamehandler.cpp: Taken insertion failures into account. + * src/account-server/accounthandler.hpp, + src/account-server/accounthandler.cpp: Simplified interface of + connection handler. Moved all the implementation details outside the + header file. + * src/account-server/serverhandler.cpp, + src/account-server/main-account.cpp: Updated to new interface of + connection handler. 2007-09-22 Guillaume Melquiond <guillaume.melquiond@gmail.com> diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp index 2a046de6..1e46d111 100644 --- a/src/account-server/accounthandler.cpp +++ b/src/account-server/accounthandler.cpp @@ -43,16 +43,76 @@ #include "utils/tokencollector.hpp" #include "utils/tokendispenser.hpp" +class AccountHandler : public ConnectionHandler +{ + public: + /** + * Constructor. + */ + AccountHandler(); + + /** + * Called by the token collector in order to associate a client to its + * account ID. + */ + void tokenMatched(AccountClient *computer, int accountID); + + /** + * Called by the token collector when a client was not acknowledged for + * some time and should be disconnected. + */ + void deletePendingClient(AccountClient *computer); + + /** + * Called by the token collector. + */ + void deletePendingConnect(int) {} + + /** + * Token collector for connecting a client coming from a game server + * without having to provide username and password a second time. + */ + TokenCollector< AccountHandler, AccountClient *, int > mTokenCollector; + + protected: + /** + * Processes account related messages. + */ + void processMessage(NetComputer *computer, MessageIn &message); + + NetComputer *computerConnected(ENetPeer *peer); + + void computerDisconnected(NetComputer *comp); +}; + +static AccountHandler *accountHandler; + AccountHandler::AccountHandler(): mTokenCollector(this) { } -bool -AccountHandler::startListen(enet_uint16 port) +bool AccountClientHandler::initialize(int port) { + accountHandler = new AccountHandler; LOG_INFO("Account handler started:"); - return ConnectionHandler::startListen(port); + return accountHandler->startListen(port); +} + +void AccountClientHandler::deinitialize() +{ + accountHandler->stopListen(); + delete accountHandler; +} + +void AccountClientHandler::process() +{ + accountHandler->process(50); +} + +void AccountClientHandler::prepareReconnect(std::string const &token, int id) +{ + accountHandler->mTokenCollector.addPendingConnect(token, id); } NetComputer* @@ -73,83 +133,7 @@ AccountHandler::computerDisconnected(NetComputer *comp) delete computer; // ~AccountClient unsets the account } -/** - * Generic interface convention for getting a message and sending it to the - * correct subroutines. Account handler takes care of determining the - * current step in the account process, be it creation, setup, or login. - */ -void -AccountHandler::processMessage(NetComputer *comp, MessageIn &message) -{ - AccountClient &computer = *static_cast< AccountClient * >(comp); - - switch (message.getId()) - { - case PAMSG_LOGIN: - LOG_DEBUG("Received msg ... PAMSG_LOGIN"); - handleLoginMessage(computer, message); - break; - - case PAMSG_LOGOUT: - LOG_DEBUG("Received msg ... PAMSG_LOGOUT"); - handleLogoutMessage(computer); - break; - - case PAMSG_RECONNECT: - LOG_DEBUG("Received msg ... PAMSG_RECONNECT"); - handleReconnectMessage(computer, message); - break; - - case PAMSG_REGISTER: - LOG_DEBUG("Received msg ... PAMSG_REGISTER"); - handleRegisterMessage(computer, message); - break; - - case PAMSG_UNREGISTER: - LOG_DEBUG("Received msg ... PAMSG_UNREGISTER"); - handleUnregisterMessage(computer, message); - break; - - case PAMSG_EMAIL_CHANGE: - LOG_DEBUG("Received msg ... PAMSG_EMAIL_CHANGE"); - handleEmailChangeMessage(computer, message); - break; - - case PAMSG_EMAIL_GET: - LOG_DEBUG("Received msg ... PAMSG_EMAIL_GET"); - handleEmailGetMessage(computer); - break; - - case PAMSG_PASSWORD_CHANGE: - LOG_DEBUG("Received msg ... PAMSG_PASSWORD_CHANGE"); - handlePasswordChangeMessage(computer, message); - break; - - case PAMSG_CHAR_CREATE: - LOG_DEBUG("Received msg ... PAMSG_CHAR_CREATE"); - handleCharacterCreateMessage(computer, message); - break; - - case PAMSG_CHAR_SELECT: - LOG_DEBUG("Received msg ... PAMSG_CHAR_SELECT"); - handleCharacterSelectMessage(computer, message); - break; - - case PAMSG_CHAR_DELETE: - LOG_DEBUG("Received msg ... PAMSG_CHAR_DELETE"); - handleCharacterDeleteMessage(computer, message); - break; - - default: - LOG_WARN("AccountHandler::processMessage, Invalid message type " - << message.getId()); - MessageOut result(XXMSG_INVALID); - computer.send(result); - break; - } -} - -void AccountHandler::sendCharacterData(AccountClient &computer, int slot, Character const &ch) +static void sendCharacterData(AccountClient &computer, int slot, Character const &ch) { MessageOut charInfo(APMSG_CHAR_INFO); charInfo.writeByte(slot); @@ -168,8 +152,7 @@ void AccountHandler::sendCharacterData(AccountClient &computer, int slot, Charac computer.send(charInfo); } -void -AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg) +static void handleLoginMessage(AccountClient &computer, MessageIn &msg) { MessageOut reply(APMSG_LOGIN_RESPONSE); @@ -199,7 +182,7 @@ AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg) return; } - if (getClientNumber() >= MAX_CLIENTS ) + if (accountHandler->getClientNumber() >= MAX_CLIENTS ) { reply.writeByte(ERRMSG_SERVER_FULL); computer.send(reply); @@ -242,8 +225,7 @@ AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg) } } -void -AccountHandler::handleLogoutMessage(AccountClient &computer) +static void handleLogoutMessage(AccountClient &computer) { MessageOut reply(APMSG_LOGOUT_RESPONSE); @@ -260,30 +242,28 @@ AccountHandler::handleLogoutMessage(AccountClient &computer) else if (computer.status == CLIENT_QUEUED) { // Delete it from the pendingClient list - mTokenCollector.deletePendingClient(&computer); + accountHandler->mTokenCollector.deletePendingClient(&computer); computer.status = CLIENT_LOGIN; reply.writeByte(ERRMSG_OK); } computer.send(reply); } -void AccountHandler:: -handleReconnectMessage(AccountClient &computer, MessageIn &msg) +static void handleReconnectMessage(AccountClient &computer, MessageIn &msg) { if (computer.status != CLIENT_LOGIN) { LOG_DEBUG("Account tried to reconnect, but was already logged in " - << "or queued."); + "or queued."); return; } std::string magic_token = msg.readString(MAGIC_TOKEN_LENGTH); computer.status = CLIENT_QUEUED; // Before the addPendingClient - mTokenCollector.addPendingClient(magic_token, &computer); + accountHandler->mTokenCollector.addPendingClient(magic_token, &computer); } -void -AccountHandler::handleRegisterMessage(AccountClient &computer, MessageIn &msg) +static void handleRegisterMessage(AccountClient &computer, MessageIn &msg) { int clientVersion = msg.readLong(); std::string username = msg.readString(); @@ -356,9 +336,7 @@ AccountHandler::handleRegisterMessage(AccountClient &computer, MessageIn &msg) computer.send(reply); } -void -AccountHandler::handleUnregisterMessage(AccountClient &computer, - MessageIn &msg) +static void handleUnregisterMessage(AccountClient &computer, MessageIn &msg) { LOG_DEBUG("AccountHandler::handleUnregisterMessage"); std::string username = msg.readString(); @@ -400,8 +378,7 @@ AccountHandler::handleUnregisterMessage(AccountClient &computer, computer.send(reply); } -void AccountHandler:: -handleEmailChangeMessage(AccountClient &computer, MessageIn &msg) +static void handleEmailChangeMessage(AccountClient &computer, MessageIn &msg) { MessageOut reply(APMSG_EMAIL_CHANGE_RESPONSE); @@ -435,8 +412,7 @@ handleEmailChangeMessage(AccountClient &computer, MessageIn &msg) computer.send(reply); } -void AccountHandler:: -handleEmailGetMessage(AccountClient &computer) +static void handleEmailGetMessage(AccountClient &computer) { MessageOut reply(APMSG_EMAIL_GET_RESPONSE); @@ -454,9 +430,7 @@ handleEmailGetMessage(AccountClient &computer) computer.send(reply); } -void -AccountHandler::handlePasswordChangeMessage(AccountClient &computer, - MessageIn &msg) +static void handlePasswordChangeMessage(AccountClient &computer, MessageIn &msg) { std::string oldPassword = msg.readString(); std::string newPassword = msg.readString(); @@ -490,9 +464,7 @@ AccountHandler::handlePasswordChangeMessage(AccountClient &computer, computer.send(reply); } -void -AccountHandler::handleCharacterCreateMessage(AccountClient &computer, - MessageIn &msg) +static void handleCharacterCreateMessage(AccountClient &computer, MessageIn &msg) { std::string name = msg.readString(); int hairStyle = msg.readByte(); @@ -612,8 +584,7 @@ AccountHandler::handleCharacterCreateMessage(AccountClient &computer, computer.send(reply); } -void AccountHandler:: -handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg) +static void handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg) { MessageOut reply(APMSG_CHAR_SELECT_RESPONSE); @@ -671,8 +642,7 @@ handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg) computer.send(reply); } -void AccountHandler:: -handleCharacterDeleteMessage(AccountClient &computer, MessageIn &msg) +static void handleCharacterDeleteMessage(AccountClient &computer, MessageIn &msg) { MessageOut reply(APMSG_CHAR_DELETE_RESPONSE); @@ -737,7 +707,72 @@ AccountHandler::deletePendingClient(AccountClient* computer) // The computer will be deleted when the disconnect event is processed } -void AccountHandler::deletePendingConnect(int) +void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) { - // No resources to free. + AccountClient &computer = *static_cast< AccountClient * >(comp); + + switch (message.getId()) + { + case PAMSG_LOGIN: + LOG_DEBUG("Received msg ... PAMSG_LOGIN"); + handleLoginMessage(computer, message); + break; + + case PAMSG_LOGOUT: + LOG_DEBUG("Received msg ... PAMSG_LOGOUT"); + handleLogoutMessage(computer); + break; + + case PAMSG_RECONNECT: + LOG_DEBUG("Received msg ... PAMSG_RECONNECT"); + handleReconnectMessage(computer, message); + break; + + case PAMSG_REGISTER: + LOG_DEBUG("Received msg ... PAMSG_REGISTER"); + handleRegisterMessage(computer, message); + break; + + case PAMSG_UNREGISTER: + LOG_DEBUG("Received msg ... PAMSG_UNREGISTER"); + handleUnregisterMessage(computer, message); + break; + + case PAMSG_EMAIL_CHANGE: + LOG_DEBUG("Received msg ... PAMSG_EMAIL_CHANGE"); + handleEmailChangeMessage(computer, message); + break; + + case PAMSG_EMAIL_GET: + LOG_DEBUG("Received msg ... PAMSG_EMAIL_GET"); + handleEmailGetMessage(computer); + break; + + case PAMSG_PASSWORD_CHANGE: + LOG_DEBUG("Received msg ... PAMSG_PASSWORD_CHANGE"); + handlePasswordChangeMessage(computer, message); + break; + + case PAMSG_CHAR_CREATE: + LOG_DEBUG("Received msg ... PAMSG_CHAR_CREATE"); + handleCharacterCreateMessage(computer, message); + break; + + case PAMSG_CHAR_SELECT: + LOG_DEBUG("Received msg ... PAMSG_CHAR_SELECT"); + handleCharacterSelectMessage(computer, message); + break; + + case PAMSG_CHAR_DELETE: + LOG_DEBUG("Received msg ... PAMSG_CHAR_DELETE"); + handleCharacterDeleteMessage(computer, message); + break; + + default: + LOG_WARN("AccountHandler::processMessage, Invalid message type " + << message.getId()); + MessageOut result(XXMSG_INVALID); + computer.send(result); + break; + } } diff --git a/src/account-server/accounthandler.hpp b/src/account-server/accounthandler.hpp index 9762ca6d..ce38af01 100644 --- a/src/account-server/accounthandler.hpp +++ b/src/account-server/accounthandler.hpp @@ -24,118 +24,29 @@ #ifndef _TMWSERV_ACCOUNTHANDLER_H_ #define _TMWSERV_ACCOUNTHANDLER_H_ -#include "net/connectionhandler.hpp" -#include "utils/tokencollector.hpp" +#include <string> -class AccountClient; -class Character; - -/** - * Manages the data stored in user accounts and provides a reliable interface - * for working with an account. The account handler class can be used as a link - * to a working account handle, and can be assigned to a user persistently as - * an interface between the computer and account. (Messages from the user can - * be traced to this account through the NetComputer structure, then processed - * here with the persistent stored data). - */ -class AccountHandler : public ConnectionHandler +namespace AccountClientHandler { - public: - /** - * Constructor - */ - AccountHandler(); - - /** - * Start the handler - */ - bool - startListen(enet_uint16 port); - - /** - * Combines a client with it's account. - * (Needed for TokenCollector) - */ - void - tokenMatched(AccountClient *computer, int accountID); - - /** - * Deletes a pending client's data. - * (Needed for TokenCollector) - */ - void - deletePendingClient(AccountClient* computer); - - /** - * Deletes a pending connection's data. - * (Needed for TokenCollector) - */ - void - deletePendingConnect(int accountID); - - /** - * TokenCollector, used to login a client without the client having to - * send username and password a second time. - */ - TokenCollector<AccountHandler, AccountClient*, int> - mTokenCollector; - - protected: - /** - * Process account related messages. - */ - void - processMessage(NetComputer *computer, MessageIn &message); - - NetComputer* - computerConnected(ENetPeer *peer); - - void - computerDisconnected(NetComputer *comp); - - private: - - void sendCharacterData(AccountClient &, int, Character const &); - - void - handleLoginMessage(AccountClient &computer, MessageIn &msg); - - void - handleLogoutMessage(AccountClient &computer); - - void - handleReconnectMessage(AccountClient &computer, MessageIn &msg); - - void - handleRegisterMessage(AccountClient &computer, MessageIn &msg); - - void - handleUnregisterMessage(AccountClient &computer, MessageIn &msg); - - void - handleEmailChangeMessage(AccountClient &computer, MessageIn &msg); - - void - handleEmailGetMessage(AccountClient &computer); - - void - handlePasswordChangeMessage(AccountClient &computer, MessageIn &msg); - - void - handleCharacterCreateMessage(AccountClient &computer, MessageIn &msg); - - void - handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg); - - void - handleCharacterDeleteMessage(AccountClient &computer, MessageIn &msg); - - /** - * Send guild join for each guild the player belongs to. - */ - void handleGuildJoining(AccountClient &computer, Character *character); -}; - -extern AccountHandler * accountHandler; + /** + * Creates a connection handler and starts listening on given port. + */ + bool initialize(int port); + + /** + * Stops listening to messages and destroys the connection handler. + */ + void deinitialize(); + + /** + * Prepares a connection for a client coming from a game server. + */ + void prepareReconnect(std::string const &token, int accountID); + + /** + * Processes messages received by the connection handler. + */ + void process(); +} #endif diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp index e684cbad..30eec4db 100644 --- a/src/account-server/main-account.cpp +++ b/src/account-server/main-account.cpp @@ -59,9 +59,6 @@ utils::StringFilter *stringFilter; /**< Slang's Filter */ /** Database handler. */ DALStorage *storage; -/** Account message handler */ -AccountHandler *accountHandler; - /** Communications (chat) message handler */ ChatHandler *chatHandler; @@ -164,7 +161,6 @@ static void initialize() // --- Initialize the global handlers // FIXME: Make the global handlers global vars or part of a bigger // singleton or a local variable in the event-loop - accountHandler = new AccountHandler; chatHandler = new ChatHandler; serverHandler = new ServerHandler; @@ -191,13 +187,14 @@ static void deinitialize() // Write configuration file Configuration::deinitialize(); + AccountClientHandler::deinitialize(); + // Quit ENet enet_deinitialize(); // Destroy message handlers delete serverHandler; delete chatHandler; - delete accountHandler; // Destroy Managers delete chatChannelManager; @@ -309,7 +306,7 @@ int main(int argc, char *argv[]) initialize(); int port = Configuration::getValue("accountServerPort", DEFAULT_SERVER_PORT); - if (!accountHandler->startListen(port) || + if (!AccountClientHandler::initialize(port) || !serverHandler->startListen(port + 1) || !chatHandler->startListen(port + 2)) { @@ -321,7 +318,7 @@ int main(int argc, char *argv[]) utils::Timer statTimer(10000); while (running) { - accountHandler->process(50); + AccountClientHandler::process(); chatHandler->process(50); serverHandler->process(50); if (statTimer.poll()) dumpStatistics(); @@ -330,6 +327,5 @@ int main(int argc, char *argv[]) LOG_INFO("Received: Quit signal, closing down..."); serverHandler->stopListen(); chatHandler->stopListen(); - accountHandler->stopListen(); deinitialize(); } diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp index 39e63c6e..d5d9d5a9 100644 --- a/src/account-server/serverhandler.cpp +++ b/src/account-server/serverhandler.cpp @@ -212,8 +212,7 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) if (Character *ptr = storage->getCharacter(id, NULL)) { int accountID = ptr->getAccountID(); - accountHandler-> - mTokenCollector.addPendingConnect(magic_token, accountID); + AccountClientHandler::prepareReconnect(magic_token, accountID); delete ptr; } else |