diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-01 20:25:00 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-08-01 20:25:00 +0000 |
commit | 2c37441be01d58e1714a604645d329575ae95f82 (patch) | |
tree | 582da8190d178ab96ffadc5d63e0c1f5d16bcb73 | |
parent | 6e6c1d4b25e7c833b68da0a2aba6861879e08881 (diff) | |
download | manaserv-2c37441be01d58e1714a604645d329575ae95f82.tar.gz manaserv-2c37441be01d58e1714a604645d329575ae95f82.tar.bz2 manaserv-2c37441be01d58e1714a604645d329575ae95f82.tar.xz manaserv-2c37441be01d58e1714a604645d329575ae95f82.zip |
Some messing about towards making cleaner code.
-rw-r--r-- | ChangeLog | 12 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/accountclient.cpp | 64 | ||||
-rw-r--r-- | src/accountclient.h | 88 | ||||
-rw-r--r-- | src/accounthandler.cpp | 234 | ||||
-rw-r--r-- | src/accounthandler.h | 20 | ||||
-rw-r--r-- | src/client.cpp | 10 | ||||
-rw-r--r-- | src/main.cpp | 13 | ||||
-rw-r--r-- | src/messageout.cpp | 9 | ||||
-rw-r--r-- | src/messageout.h | 9 |
10 files changed, 283 insertions, 178 deletions
@@ -1,3 +1,15 @@ +2006-08-01 Bjørn Lindeijer <bjorn@lindeijer.nl> + + * src/accountclient.h, src/accountclient.cpp, src/Makefile.am: Defined + this class in its own module (extracted from accounthandler.cpp). + * src/messageout.h, src/messageout.cpp: Added constructor that takes + the message ID for convenience. + * src/accounthandler.cpp, src/accounthandler.h: Put handling of logins + in a separate method. I think it would be better if this was done for + all messages. + * src/client.cpp: Removed requesting character listing, as this comes + with the succesful register/login responses. + 2006-07-31 Bjørn Lindeijer <bjorn@lindeijer.nl> * src/client.cpp, src/netcomputer.cpp: Fixed registering (send version diff --git a/src/Makefile.am b/src/Makefile.am index 6fff83eb..298b3aa8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,6 +14,8 @@ tmwclient_SOURCES = client.cpp \ tmwserv_SOURCES = main.cpp \ configuration.h \ configuration.cpp \ + accountclient.h \ + accountclient.cpp \ accounthandler.h \ accounthandler.cpp \ chathandler.h \ diff --git a/src/accountclient.cpp b/src/accountclient.cpp new file mode 100644 index 00000000..1b1172e3 --- /dev/null +++ b/src/accountclient.cpp @@ -0,0 +1,64 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: accounthandler.cpp 2478 2006-07-27 21:04:04Z umperio $ + */ + +#include "accountclient.h" + +#include "account.h" +#include "accounthandler.h" + +AccountClient::AccountClient(AccountHandler *handler, ENetPeer *peer): + NetComputer(handler, peer), + mAccountPtr(NULL), + mCharacterPtr(NULL) +{ +} + +AccountClient::~AccountClient() +{ + unsetAccount(); +} + + +void AccountClient::setAccount(AccountPtr acc) +{ + unsetAccount(); + mAccountPtr = acc; +} + +void AccountClient::setCharacter(PlayerPtr ch) +{ + unsetCharacter(); + mCharacterPtr = ch; +} + +void AccountClient::unsetAccount() +{ + unsetCharacter(); + mAccountPtr = AccountPtr(NULL); +} + +void AccountClient::unsetCharacter() +{ + if (mCharacterPtr.get() == NULL) return; + mCharacterPtr = PlayerPtr(NULL); +} diff --git a/src/accountclient.h b/src/accountclient.h new file mode 100644 index 00000000..a73b773e --- /dev/null +++ b/src/accountclient.h @@ -0,0 +1,88 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id: accounthandler.cpp 2478 2006-07-27 21:04:04Z umperio $ + */ + +#ifndef _TMWSERV_ACCOUNTCLIENT_H_ +#define _TMWSERV_ACCOUNTCLIENT_H_ + +#include "netcomputer.h" + +#include <enet/enet.h> + +class AccountHandler; + +/** + * A connected computer that can have an account and character associated with + * it. + */ +class AccountClient: public NetComputer +{ + public: + /** + * Constructor. + */ + AccountClient(AccountHandler *accountHandler, ENetPeer *peer); + + /** + * Destructor. + */ + ~AccountClient(); + + /** + * Set the account associated with the connection + */ + void setAccount(AccountPtr acc); + + /** + * Unset the account associated with the connection + */ + void unsetAccount(); + + /** + * Get account associated with the connection. + */ + AccountPtr getAccount() { return mAccountPtr; } + + /** + * Set the selected character associated with connection. + */ + void setCharacter(PlayerPtr ch); + + /** + * Deselect the character associated with connection. + */ + void unsetCharacter(); + + /** + * Get character associated with the connection + */ + PlayerPtr getCharacter() { return mCharacterPtr; } + + private: + /** Account associated with connection */ + AccountPtr mAccountPtr; + + /** Selected character */ + PlayerPtr mCharacterPtr; +}; + +#endif diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index c9f5098e..4de1c2aa 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -24,6 +24,7 @@ #include "accounthandler.h" #include "account.h" +#include "accountclient.h" #include "chathandler.h" #include "configuration.h" #include "connectionhandler.h" @@ -37,93 +38,6 @@ #include "utils/logger.h" #include "utils/stringfilter.h" -class AccountClient: public NetComputer -{ - public: - /** - * Constructor. - */ - AccountClient(AccountHandler *, ENetPeer *); - - /** - * Destructor. - */ - ~AccountClient(); - - /** - * Set the account associated with the connection - */ - void setAccount(AccountPtr acc); - - /** - * Unset the account associated with the connection - */ - void unsetAccount(); - - /** - * Get account associated with the connection. - */ - AccountPtr getAccount() { return mAccountPtr; } - - /** - * Set the selected character associated with connection. - */ - void setCharacter(PlayerPtr ch); - - /** - * Deselect the character associated with connection. - */ - void unsetCharacter(); - - /** - * Get character associated with the connection - */ - PlayerPtr getCharacter() { return mCharacterPtr; } - - private: - /** Account associated with connection */ - AccountPtr mAccountPtr; - - /** Selected character */ - PlayerPtr mCharacterPtr; -}; - -AccountClient::AccountClient(AccountHandler *handler, ENetPeer *peer): - NetComputer(handler, peer), - mAccountPtr(NULL), - mCharacterPtr(NULL) -{ -} - -AccountClient::~AccountClient() -{ - unsetAccount(); -} - - -void AccountClient::setAccount(AccountPtr acc) -{ - unsetAccount(); - mAccountPtr = acc; -} - -void AccountClient::setCharacter(PlayerPtr ch) -{ - unsetCharacter(); - mCharacterPtr = ch; -} - -void AccountClient::unsetAccount() -{ - unsetCharacter(); - mAccountPtr = AccountPtr(NULL); -} - -void AccountClient::unsetCharacter() -{ - if (mCharacterPtr.get() == NULL) return; - mCharacterPtr = PlayerPtr(NULL); -} NetComputer *AccountHandler::computerConnected(ENetPeer *peer) { @@ -160,75 +74,7 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) switch (message.getId()) { case PAMSG_LOGIN: - { - unsigned long clientVersion = message.readLong(); - std::string username = message.readString(); - std::string password = message.readString(); - LOG_INFO(username << " is trying to login.", 1); - - result.writeShort(APMSG_LOGIN_RESPONSE); - - if (clientVersion < config.getValue("clientVersion", 0)) - { - LOG_INFO("Client has an unsufficient version number to login.", 1); - result.writeByte(LOGIN_INVALID_VERSION); - break; - } - if (stringFilter->findDoubleQuotes(username)) - { - result.writeByte(ERRMSG_INVALID_ARGUMENT); - LOG_INFO(username << ": has got double quotes in it.", 1); - break; - } - if (computer.getAccount().get() != NULL) { - LOG_INFO("Already logged in as " << computer.getAccount()->getName() - << ".", 1); - LOG_INFO("Please logout first.", 1); - result.writeByte(ERRMSG_FAILURE); - break; - } - if (getClientNumber() >= MAX_CLIENTS ) - { - // Too much clients logged in. - LOG_INFO("Client couldn't login. Already has " << MAX_CLIENTS - << " logged in.", 1); - result.writeByte(LOGIN_SERVER_FULL); - break; - } - - // see if the account exists - AccountPtr acc = store.getAccount(username); - - if (!acc.get() || acc->getPassword() != password) { - // account doesn't exist -- send error to client - LOG_INFO(username << ": Account does not exist or the password is invalid.", 1); - - result.writeByte(ERRMSG_INVALID_ARGUMENT); - break; - } - - LOG_INFO("Login OK by " << username, 1); - - // Associate account with connection - computer.setAccount(acc); - - result.writeByte(ERRMSG_OK); - - // Return information about available characters - Players &chars = computer.getAccount()->getCharacters(); - result.writeByte(chars.size()); - - LOG_INFO(username << "'s account has " << chars.size() << " character(s).", 1); - for (unsigned int i = 0; i < chars.size(); i++) - { - result.writeString(chars[i]->getName()); - result.writeByte(unsigned(short(chars[i]->getGender()))); - result.writeByte(chars[i]->getHairStyle()); - result.writeByte(chars[i]->getHairColor()); - result.writeByte(chars[i]->getLevel()); - result.writeShort(chars[i]->getMoney()); - } - } + handleLoginMessage(computer, message); break; case PAMSG_LOGOUT: @@ -789,3 +635,79 @@ void AccountHandler::processMessage(NetComputer *comp, MessageIn &message) // return result computer.send(result.getPacket()); } + +void +AccountHandler::handleLoginMessage(AccountClient &computer, MessageIn &msg) +{ + unsigned long clientVersion = msg.readLong(); + std::string username = msg.readString(); + std::string password = msg.readString(); + + LOG_INFO(username << " is trying to login.", 1); + + MessageOut reply(APMSG_LOGIN_RESPONSE); + + if (clientVersion < config.getValue("clientVersion", 0)) + { + LOG_INFO("Client has an insufficient version number to login.", 1); + reply.writeByte(LOGIN_INVALID_VERSION); + } + if (stringFilter->findDoubleQuotes(username)) + { + LOG_INFO(username << ": has got double quotes in it.", 1); + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + if (computer.getAccount().get() != NULL) { + LOG_INFO("Already logged in as " << computer.getAccount()->getName() + << ".", 1); + LOG_INFO("Please logout first.", 1); + reply.writeByte(ERRMSG_FAILURE); + } + if (getClientNumber() >= MAX_CLIENTS ) + { + LOG_INFO("Client couldn't login. Already has " << MAX_CLIENTS + << " logged in.", 1); + reply.writeByte(LOGIN_SERVER_FULL); + } + else + { + // Check if the account exists + Storage &store = Storage::instance("tmw"); + AccountPtr acc = store.getAccount(username); + + if (!acc.get() || acc->getPassword() != password) + { + LOG_INFO(username << ": Account does not exist or the password is " + "invalid.", 1); + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else + { + LOG_INFO("Login OK by " << username, 1); + + // Associate account with connection + computer.setAccount(acc); + + reply.writeByte(ERRMSG_OK); + + // Return information about available characters + Players &chars = computer.getAccount()->getCharacters(); + reply.writeByte(chars.size()); + + LOG_INFO(username << "'s account has " << chars.size() + << " character(s).", 1); + + for (unsigned int i = 0; i < chars.size(); i++) + { + reply.writeString(chars[i]->getName()); + reply.writeByte(unsigned(short(chars[i]->getGender()))); + reply.writeByte(chars[i]->getHairStyle()); + reply.writeByte(chars[i]->getHairColor()); + reply.writeByte(chars[i]->getLevel()); + reply.writeShort(chars[i]->getMoney()); + } + } + } + + computer.send(reply.getPacket()); +} diff --git a/src/accounthandler.h b/src/accounthandler.h index 8b6bb8d2..c4073589 100644 --- a/src/accounthandler.h +++ b/src/accounthandler.h @@ -26,11 +26,13 @@ #include "connectionhandler.h" +class AccountClient; + /** * 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 + * 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). */ @@ -40,9 +42,17 @@ class AccountHandler : public ConnectionHandler /** * Process account related messages. */ - void processMessage(NetComputer *computer, MessageIn &message); - NetComputer *computerConnected(ENetPeer *); - void computerDisconnected(NetComputer *); + void + processMessage(NetComputer *computer, MessageIn &message); + + NetComputer* + computerConnected(ENetPeer *peer); + + void + computerDisconnected(NetComputer *comp); + + void + handleLoginMessage(AccountClient &computer, MessageIn &msg); }; #endif diff --git a/src/client.cpp b/src/client.cpp index 82548c89..f1d73541 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -92,9 +92,9 @@ int main(int argc, char *argv[]) if (connected) { int answer = -1; std::cout << std::endl; - std::cout << "0) Quit 9) Character selection" << std::endl; + std::cout << "0) Quit 9) Select Character" << std::endl; std::cout << "1) Register 10) Delete Character" << std::endl; - std::cout << "2) Unregister 11) List Characters" << std::endl; + std::cout << "2) Unregister " << std::endl; std::cout << "3) Login 12) Move Character" << std::endl; std::cout << "4) Logout 13) Say around" << std::endl; std::cout << "5) Change Password 14) Equip Item" << std::endl; @@ -259,12 +259,6 @@ int main(int argc, char *argv[]) msg.writeByte(atoi(line)); } break; - case 11: - { - // List characters - msg.writeShort(PAMSG_CHAR_LIST); - } break; - case 12: { // Move character diff --git a/src/main.cpp b/src/main.cpp index 8f418ce4..28afc187 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -107,12 +107,11 @@ State *gameState; */ void initialize() { - -/** - * If the path values aren't defined, we set the default - * depending on the platform. - */ -// The config path + /* + * If the path values aren't defined, we set the default + * depending on the platform. + */ + // The config path #if defined CONFIG_FILE std::string configPath = CONFIG_FILE; #else @@ -127,7 +126,7 @@ void initialize() #endif // defined CONFIG_FILE -// The log path + // The log path #if defined LOG_FILE std::string logPath = LOG_FILE; #else diff --git a/src/messageout.cpp b/src/messageout.cpp index e8336694..639038a0 100644 --- a/src/messageout.cpp +++ b/src/messageout.cpp @@ -37,6 +37,15 @@ MessageOut::MessageOut(): { } +MessageOut::MessageOut(short id): + mPacket(0), + mData(0), + mDataSize(0), + mPos(0) +{ + writeShort(id); +} + MessageOut::~MessageOut() { if (mPacket) { diff --git a/src/messageout.h b/src/messageout.h index 245c3dca..41a6b983 100644 --- a/src/messageout.h +++ b/src/messageout.h @@ -40,6 +40,11 @@ class MessageOut MessageOut(); /** + * Constructor that takes a message ID. + */ + MessageOut(short id); + + /** * Destructor. */ ~MessageOut(); @@ -60,12 +65,12 @@ class MessageOut * a call to this method. */ const Packet *getPacket(); - + /** * Returns the content of the message. */ char *getData(); - + /** * Returns the length of the data. */ |