From c548f6899f0d188ef3a5a3d7abbb3b32e9addaa6 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Sat, 20 May 2006 09:26:47 +0000 Subject: Changed ConnectionHandler and NetComputer into low-level base classes; they are overloaded by client-aware classes. Forced statement syntax for loggers. --- src/accounthandler.cpp | 141 +++++++++++++++++++++++---------------------- src/accounthandler.h | 5 +- src/chathandler.cpp | 38 ++++++------ src/chathandler.h | 13 +++-- src/configuration.cpp | 4 +- src/connectionhandler.cpp | 137 +++++++++++++++---------------------------- src/connectionhandler.h | 84 ++++++++++++++++----------- src/dalstorage.cpp | 20 +++---- src/gamehandler.cpp | 3 +- src/main.cpp | 32 +++++----- src/messagehandler.cpp | 2 +- src/netcomputer.cpp | 29 ++++++---- src/netcomputer.h | 31 ++++++++-- src/skill.cpp | 2 +- src/state.cpp | 4 +- src/state.h | 2 +- src/utils/logger.h | 24 ++++---- src/utils/stringfilter.cpp | 4 +- 18 files changed, 288 insertions(+), 287 deletions(-) (limited to 'src') diff --git a/src/accounthandler.cpp b/src/accounthandler.cpp index 389bed13..d1f9263a 100644 --- a/src/accounthandler.cpp +++ b/src/accounthandler.cpp @@ -44,8 +44,9 @@ using tmwserv::Storage; * correct subroutines. Account handler takes care of determining the * current step in the account process, be it creation, setup, or login. */ -void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) +void AccountHandler::receiveMessage(NetComputer &comp, MessageIn &message) { + ClientComputer &computer = static_cast< ClientComputer & >(comp); Storage &store = Storage::instance("tmw"); @@ -67,26 +68,26 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) std::string clientVersion = message.readString(); std::string username = message.readString(); std::string password = message.readString(); - LOG_INFO(username << " is trying to login.", 1) + LOG_INFO(username << " is trying to login.", 1); result.writeShort(SMSG_LOGIN_RESPONSE); if (clientVersion < config.getValue("clientVersion", "0.0.0")) { - LOG_INFO("Client has an unsufficient version number to login.", 1) + LOG_INFO("Client has an unsufficient version number to login.", 1); result.writeByte(LOGIN_INVALID_VERSION); break; } if (stringFilter->findDoubleQuotes(username)) { result.writeByte(LOGIN_INVALID_USERNAME); - LOG_INFO(username << ": has got double quotes in it.", 1) + 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) + << ".", 1); + LOG_INFO("Please logout first.", 1); result.writeByte(LOGIN_ALREADY_LOGGED); break; } @@ -94,7 +95,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { // Too much clients logged in. LOG_INFO("Client couldn't login. Already has " << MAX_CLIENTS - << " logged in.", 1) + << " logged in.", 1); result.writeByte(LOGIN_SERVER_FULL); break; } @@ -104,16 +105,16 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (!acc.get()) { // account doesn't exist -- send error to client - LOG_INFO(username << ": Account does not exist.", 1) + LOG_INFO(username << ": Account does not exist.", 1); result.writeByte(LOGIN_INVALID_USERNAME); } else if (acc->getPassword() != password) { // bad password -- send error to client - LOG_INFO("Bad password for " << username, 1) + LOG_INFO("Bad password for " << username, 1); result.writeByte(LOGIN_INVALID_PASSWORD); } else { - LOG_INFO("Login OK by " << username, 1) + LOG_INFO("Login OK by " << username, 1); // Associate account with connection computer.setAccount(acc); @@ -124,7 +125,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) tmwserv::Beings &chars = computer.getAccount()->getCharacters(); result.writeByte(chars.size()); - LOG_INFO(username << "'s account has " << chars.size() << " character(s).", 1) + LOG_INFO(username << "'s account has " << chars.size() << " character(s).", 1); std::string charNames = ""; for (unsigned int i = 0; i < chars.size(); i++) { @@ -133,7 +134,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) charNames += chars[i]->getName(); } charNames += "."; - LOG_INFO(charNames.c_str(), 1) + LOG_INFO(charNames.c_str(), 1); } } break; @@ -144,7 +145,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if ( computer.getAccount().get() == NULL ) { - LOG_INFO("Can't logout. Not even logged in.", 1) + LOG_INFO("Can't logout. Not even logged in.", 1); result.writeByte(LOGOUT_UNSUCCESSFULL); } else @@ -152,14 +153,14 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) std::string username = computer.getAccount()->getName(); if ( username == "" ) { - LOG_INFO("Account without name ? Logged out anyway...", 1) + LOG_INFO("Account without name ? Logged out anyway...", 1); // computer.unsetCharacter(); Done by unsetAccount(); computer.unsetAccount(); result.writeByte(LOGOUT_UNSUCCESSFULL); } else { - LOG_INFO(computer.getAccount()->getName() << " logs out.", 1) + LOG_INFO(computer.getAccount()->getName() << " logs out.", 1); // computer.unsetCharacter(); Done by unsetAccount(); computer.unsetAccount(); result.writeByte(LOGOUT_OK); @@ -178,7 +179,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (clientVersion < config.getValue("clientVersion", "0.0.0")) { - LOG_INFO("Client has an unsufficient version number to login.", 1) + LOG_INFO("Client has an unsufficient version number to login.", 1); result.writeByte(REGISTER_INVALID_VERSION); break; } @@ -187,52 +188,52 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (!stringFilter->filterContent(username)) { result.writeByte(REGISTER_INVALID_USERNAME); - LOG_INFO(username << ": has got bad words in it.", 1) + LOG_INFO(username << ": has got bad words in it.", 1); break; } // Checking if there are double quotes in it. if (stringFilter->findDoubleQuotes(username)) { result.writeByte(REGISTER_INVALID_USERNAME); - LOG_INFO(username << ": has got double quotes in it.", 1) + LOG_INFO(username << ": has got double quotes in it.", 1); break; } // Checking conditions for having a good account. - LOG_INFO(username << " is trying to register.", 1) + LOG_INFO(username << " is trying to register.", 1); // see if the account exists tmwserv::AccountPtr accPtr = store.getAccount(username); if ( accPtr.get() ) // Account already exists. { result.writeByte(REGISTER_EXISTS_USERNAME); - LOG_INFO(username << ": Username already exists.", 1) + LOG_INFO(username << ": Username already exists.", 1); } else if ((username.length() < MIN_LOGIN_LENGTH) || (username.length() > MAX_LOGIN_LENGTH)) // Username length { result.writeByte(REGISTER_INVALID_USERNAME); - LOG_INFO(username << ": Username too short or too long.", 1) + LOG_INFO(username << ": Username too short or too long.", 1); } else if ((password.length() < MIN_PASSWORD_LENGTH) || (password.length() > MAX_PASSWORD_LENGTH)) { result.writeByte(REGISTER_INVALID_PASSWORD); - LOG_INFO(email << ": Password too short or too long.", 1) + LOG_INFO(email << ": Password too short or too long.", 1); } else if (!stringFilter->isEmailValid(email)) { result.writeByte(REGISTER_INVALID_EMAIL); - LOG_INFO(email << ": Email Invalid, only a@b.c format is accepted.", 1) + LOG_INFO(email << ": Email Invalid, only a@b.c format is accepted.", 1); } else if (stringFilter->findDoubleQuotes(email)) { result.writeByte(REGISTER_INVALID_EMAIL); - LOG_INFO(email << ": has got double quotes in it.", 1) + LOG_INFO(email << ": has got double quotes in it.", 1); break; } else if (store.getSameEmailNumber(email) > 0) // Search if Email already exists. { result.writeByte(REGISTER_EXISTS_EMAIL); - LOG_INFO(email << ": Email already exists.", 1) + LOG_INFO(email << ": Email already exists.", 1); } else { @@ -242,7 +243,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) result.writeByte(REGISTER_OK); store.flush(); // flush changes - LOG_INFO(username << ": Account registered.", 1) + LOG_INFO(username << ": Account registered.", 1); } } break; @@ -251,14 +252,14 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { std::string username = message.readString(); std::string password = message.readString(); - LOG_INFO(username << " wants to be deleted from our accounts.", 1) + LOG_INFO(username << " wants to be deleted from our accounts.", 1); result.writeShort(SMSG_UNREGISTER_RESPONSE); if (stringFilter->findDoubleQuotes(username)) { result.writeByte(UNREGISTER_INVALID_USERNAME); - LOG_INFO(username << ": has got double quotes in it.", 1) + LOG_INFO(username << ": has got double quotes in it.", 1); break; } @@ -267,12 +268,12 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (!accPtr.get()) { // account doesn't exist -- send error to client - LOG_INFO(username << ": Account doesn't exist anyway.", 1) + LOG_INFO(username << ": Account doesn't exist anyway.", 1); result.writeByte(UNREGISTER_INVALID_USERNAME); } else if (accPtr->getPassword() != password) { // bad password -- send error to client - LOG_INFO("Won't delete it : Bad password for " << username << ".", 1) + LOG_INFO("Won't delete it : Bad password for " << username << ".", 1); result.writeByte(UNREGISTER_INVALID_PASSWORD); } else { @@ -288,7 +289,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) } } // delete account and associated characters - LOG_INFO("Farewell " << username << " ...", 1) + LOG_INFO("Farewell " << username << " ...", 1); store.delAccount(username); store.flush(); result.writeByte(UNREGISTER_OK); @@ -302,7 +303,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (computer.getAccount().get() == NULL) { result.writeByte(EMAILCHG_NOLOGIN); - LOG_INFO("Not logged in. Can't change your Account's Email.", 1) + LOG_INFO("Not logged in. Can't change your Account's Email.", 1); break; } @@ -311,24 +312,24 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { result.writeByte(EMAILCHG_INVALID); LOG_INFO(email << ": Invalid format, cannot change Email for " << - computer.getAccount()->getName(), 1) + computer.getAccount()->getName(), 1); } else if (stringFilter->findDoubleQuotes(email)) { result.writeByte(EMAILCHG_INVALID); - LOG_INFO(email << ": has got double quotes in it.", 1) + LOG_INFO(email << ": has got double quotes in it.", 1); } else if (store.getSameEmailNumber(email) > 1) // Search if Email already exists, { // Except for the one already that is to result.writeByte(EMAILCHG_EXISTS_EMAIL); // be changed. - LOG_INFO(email << ": New Email already exists.", 1) + LOG_INFO(email << ": New Email already exists.", 1); } else { computer.getAccount()->setEmail(email); result.writeByte(EMAILCHG_OK); LOG_INFO(computer.getAccount()->getName() << ": Email changed to: " << - email, 1) + email, 1); } } break; @@ -338,7 +339,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) result.writeShort(SMSG_EMAIL_GET_RESPONSE); if (computer.getAccount().get() == NULL) { result.writeByte(EMAILGET_NOLOGIN); - LOG_INFO("Not logged in. Can't get your Account's current Email.", 1) + LOG_INFO("Not logged in. Can't get your Account's current Email.", 1); break; } else @@ -356,7 +357,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (computer.getAccount().get() == NULL) { result.writeByte(PASSCHG_NOLOGIN); - LOG_INFO("Not logged in. Can't change your Account's Password.", 1) + LOG_INFO("Not logged in. Can't change your Account's Password.", 1); break; } std::string oldPassword = message.readString(); @@ -367,31 +368,31 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { result.writeByte(PASSCHG_INVALID); LOG_INFO(computer.getAccount()->getName() << - ": New password too long or too short.", 1) + ": New password too long or too short.", 1); } else if (stringFilter->findDoubleQuotes(password1)) { result.writeByte(PASSCHG_INVALID); - LOG_INFO(password1 << ": has got double quotes in it.", 1) + LOG_INFO(password1 << ": has got double quotes in it.", 1); } else if ( password1 != password2 ) { result.writeByte(PASSCHG_MISMATCH); LOG_INFO(computer.getAccount()->getName() << - ": New password mismatched confirmation password.", 1) + ": New password mismatched confirmation password.", 1); } else if ( oldPassword != computer.getAccount()->getPassword() ) { result.writeByte(PASSCHG_MISMATCH); LOG_INFO(computer.getAccount()->getName() << - ": Old password is wrong.", 1) + ": Old password is wrong.", 1); } else { computer.getAccount()->setPassword(password1); result.writeByte(PASSCHG_OK); LOG_INFO(computer.getAccount()->getName() << - ": The password was changed.", 1) + ": The password was changed.", 1); } } break; @@ -402,7 +403,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (computer.getAccount().get() == NULL) { result.writeByte(CREATE_NOLOGIN); - LOG_INFO("Not logged in. Can't create a Character.", 1) + LOG_INFO("Not logged in. Can't create a Character.", 1); break; } @@ -412,7 +413,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { result.writeByte(CREATE_TOO_MUCH_CHARACTERS); LOG_INFO("Already has " << MAX_OF_CHARACTERS - << " characters. Can't create another Character.", 1) + << " characters. Can't create another Character.", 1); break; } @@ -421,35 +422,35 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (!stringFilter->filterContent(name)) { result.writeByte(CREATE_INVALID_NAME); - LOG_INFO(name << ": Character has got bad words in it.", 1) + LOG_INFO(name << ": Character has got bad words in it.", 1); break; } // Checking if the Name has got double quotes. if (stringFilter->findDoubleQuotes(name)) { result.writeByte(CREATE_INVALID_NAME); - LOG_INFO(name << ": has got double quotes in it.", 1) + LOG_INFO(name << ": has got double quotes in it.", 1); break; } // Check if the character's name already exists if (store.doesCharacterNameExists(name)) { result.writeByte(CREATE_EXISTS_NAME); - LOG_INFO(name << ": Character's name already exists.", 1) + LOG_INFO(name << ": Character's name already exists.", 1); break; } // Check for character's name length if ((name.length() < MIN_CHARACTER_LENGTH) || (name.length() > MAX_CHARACTER_LENGTH)) { result.writeByte(CREATE_INVALID_NAME); - LOG_INFO(name << ": Character's name too short or too long.", 1) + LOG_INFO(name << ": Character's name too short or too long.", 1); break; } char hairStyle = message.readByte(); if ((hairStyle < 0) || (hairStyle > (signed)MAX_HAIRSTYLE_VALUE)) { result.writeByte(CREATE_INVALID_HAIRSTYLE); - LOG_INFO(name << ": Character's hair Style is invalid.", 1) + LOG_INFO(name << ": Character's hair Style is invalid.", 1); break; } @@ -457,14 +458,14 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if ((hairColor < 0) || (hairColor > (signed)MAX_HAIRCOLOR_VALUE)) { result.writeByte(CREATE_INVALID_HAIRCOLOR); - LOG_INFO(name << ": Character's hair Color is invalid.", 1) + LOG_INFO(name << ": Character's hair Color is invalid.", 1); break; } Genders gender = (Genders)message.readByte(); if ((gender < 0) || (gender > (signed)MAX_GENDER_VALUE)) { result.writeByte(CREATE_INVALID_GENDER); - LOG_INFO(name << ": Character's gender is invalid.", 1) + LOG_INFO(name << ": Character's gender is invalid.", 1); break; } // LATER_ON: Add race, face and maybe special attributes. @@ -522,25 +523,25 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if ( totalStats > POINTS_TO_DISTRIBUTES_AT_LVL1 ) { result.writeByte(CREATE_RAW_STATS_TOO_HIGH); - LOG_INFO(name << ": Character's stats are too high to be of level 1.", 1) + LOG_INFO(name << ": Character's stats are too high to be of level 1.", 1); break; } if ( totalStats < POINTS_TO_DISTRIBUTES_AT_LVL1 ) { result.writeByte(CREATE_RAW_STATS_TOO_LOW); - LOG_INFO(name << ": Character's stats are too low to be of level 1.", 1) + LOG_INFO(name << ": Character's stats are too low to be of level 1.", 1); break; } if ( (highestStat - lowestStat) > (signed)MAX_DIFF_BETWEEN_STATS ) { result.writeByte(CREATE_RAW_STATS_INVALID_DIFF); - LOG_INFO(name << ": Character's stats difference is too high to be accepted.", 1) + LOG_INFO(name << ": Character's stats difference is too high to be accepted.", 1); break; } if ( !validNonZeroRawStats ) { result.writeByte(CREATE_RAW_STATS_EQUAL_TO_ZERO); - LOG_INFO(name << ": One stat is equal to zero.", 1) + LOG_INFO(name << ": One stat is equal to zero.", 1); break; } @@ -555,7 +556,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) computer.getAccount()->addCharacter(newCharacter); LOG_INFO("Character " << name << " was created for " - << computer.getAccount()->getName() << "'s account.", 1) + << computer.getAccount()->getName() << "'s account.", 1); store.flush(); // flush changes result.writeByte(CREATE_OK); @@ -569,7 +570,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (computer.getAccount().get() == NULL) { result.writeByte(SELECT_NOLOGIN); - LOG_INFO("Not logged in. Can't select a Character.", 1) + LOG_INFO("Not logged in. Can't select a Character.", 1); break; // not logged in } @@ -579,14 +580,14 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (chars.size() == 0 ) { result.writeByte(SELECT_NO_CHARACTERS); - LOG_INFO("Character Selection : Yet no characters created.", 1) + LOG_INFO("Character Selection : Yet no characters created.", 1); break; } // Character ID = 0 to Number of Characters - 1. if (charNum >= chars.size()) { // invalid char selection result.writeByte(SELECT_INVALID); - LOG_INFO("Character Selection : Selection out of ID range.", 1) + LOG_INFO("Character Selection : Selection out of ID range.", 1); break; } @@ -614,7 +615,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (computer.getAccount().get() == NULL) { result.writeByte(DELETE_NOLOGIN); - LOG_INFO("Not logged in. Can't delete a Character.", 1) + LOG_INFO("Not logged in. Can't delete a Character.", 1); break; // not logged in } @@ -625,14 +626,14 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) { result.writeByte(DELETE_NO_MORE_CHARACTERS); LOG_INFO("Character Deletion : No characters in " << computer.getAccount()->getName() - << "'s account.", 1) + << "'s account.", 1); break; } // Character ID = 0 to Number of Characters - 1. if (charNum >= chars.size()) { // invalid char selection result.writeByte(DELETE_INVALID_ID); - LOG_INFO("Character Deletion : Selection out of ID range.", 1) + LOG_INFO("Character Deletion : Selection out of ID range.", 1); break; } @@ -650,7 +651,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) std::string deletedCharacter = chars[charNum].get()->getName(); computer.getAccount()->delCharacter(deletedCharacter); store.flush(); - LOG_INFO(deletedCharacter << ": Character deleted...", 1) + LOG_INFO(deletedCharacter << ": Character deleted...", 1); result.writeByte(DELETE_OK); } @@ -663,7 +664,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (computer.getAccount().get() == NULL) { result.writeByte(CHAR_LIST_NOLOGIN); - LOG_INFO("Not logged in. Can't list characters.", 1) + LOG_INFO("Not logged in. Can't list characters.", 1); break; // not logged in } @@ -673,7 +674,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) result.writeByte(chars.size()); LOG_INFO(computer.getAccount()->getName() << "'s account has " - << chars.size() << " character(s).", 1) + << chars.size() << " character(s).", 1); std::string charStats = ""; std::string mapName = ""; for (unsigned int i = 0; i < chars.size(); i++) @@ -699,12 +700,12 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) result.writeShort(chars[i]->getY()); } charStats += "."; - LOG_INFO(charStats.c_str(), 1) + LOG_INFO(charStats.c_str(), 1); } break; default: - LOG_WARN("Invalid message type", 0) + LOG_WARN("Invalid message type", 0); result.writeShort(SMSG_LOGIN_RESPONSE); result.writeByte(LOGIN_UNKNOWN); break; @@ -727,7 +728,7 @@ void AccountHandler::receiveMessage(NetComputer &computer, MessageIn &message) * requested char, ERROR on early termination of the * routine. */ -int AccountHandler::loginMessage(NetComputer &computer, MessageIn &message) +int AccountHandler::loginMessage(ClientComputer &computer, MessageIn &message) { // Get the handle (account) the player is requesting // RETURN TMW_ACCOUNTERROR_NOEXIST if: requested does not handle exist @@ -756,7 +757,7 @@ int AccountHandler::loginMessage(NetComputer &computer, MessageIn &message) * routine. */ int -AccountHandler::assignAccount(NetComputer &computer, tmwserv::Account *account) +AccountHandler::assignAccount(ClientComputer &computer, tmwserv::Account *account) { // RETURN TMW_ACCOUNTERROR_ASSIGNFAILED if: the account was accessed before // being initalized. diff --git a/src/accounthandler.h b/src/accounthandler.h index 62d6e46f..7c8f8b96 100644 --- a/src/accounthandler.h +++ b/src/accounthandler.h @@ -28,6 +28,7 @@ class MessageIn; class NetComputer; +class ClientComputer; namespace tmwserv { class Account; @@ -53,12 +54,12 @@ class AccountHandler : public MessageHandler /** * Handles the login message. */ - int loginMessage(NetComputer &computer, MessageIn &message); + int loginMessage(ClientComputer &computer, MessageIn &message); /** * Account assignment. */ - int assignAccount(NetComputer &computer, tmwserv::Account *account); + int assignAccount(ClientComputer &computer, tmwserv::Account *account); }; #endif diff --git a/src/chathandler.cpp b/src/chathandler.cpp index 05dc046f..a9ad9f91 100644 --- a/src/chathandler.cpp +++ b/src/chathandler.cpp @@ -32,12 +32,14 @@ #include "utils/logger.h" #include "utils/stringfilter.h" -void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) +void ChatHandler::receiveMessage(NetComputer &comp, MessageIn &message) { + ClientComputer &computer = static_cast< ClientComputer & >(comp); + // If not logged in... if (computer.getAccount().get() == NULL) { - LOG_INFO("Not logged in, can't chat...", 2) + LOG_INFO("Not logged in, can't chat...", 2); MessageOut result; result.writeShort(SMSG_CHAT); result.writeByte(CHAT_NOLOGIN); @@ -53,7 +55,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) result.writeShort(SMSG_CHAT); result.writeByte(CHAT_NO_CHARACTER_SELECTED); computer.send(result.getPacket()); - LOG_INFO("No character selected. Can't chat...", 2) + LOG_INFO("No character selected. Can't chat...", 2); return; // character not selected } } @@ -68,7 +70,7 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) if (stringFilter->filterContent(text)) { short channel = message.readShort(); - LOG_INFO("Say: (Channel " << channel << "): " << text, 2) + LOG_INFO("Say: (Channel " << channel << "): " << text, 2); if ( channel == 0 ) // Let's say that is the default channel for now. { if ( text.substr(0, 1) == "@" || text.substr(0, 1) == "#" || text.substr(0, 1) == "/" ) @@ -371,21 +373,21 @@ void ChatHandler::receiveMessage(NetComputer &computer, MessageIn &message) break; default: - LOG_INFO("Chat: Invalid message type", 2) + LOG_INFO("Chat: Invalid message type", 2); break; } } -void ChatHandler::handleCommand(NetComputer &computer, const std::string& command) +void ChatHandler::handleCommand(ClientComputer &computer, const std::string& command) { - LOG_INFO("Chat: Received unhandled command: " << command, 2) + LOG_INFO("Chat: Received unhandled command: " << command, 2); MessageOut result; result.writeShort(SMSG_CHAT); result.writeByte(CHATCMD_UNHANDLED_COMMAND); computer.send(result.getPacket()); } -void ChatHandler::warnPlayerAboutBadWords(NetComputer &computer) +void ChatHandler::warnPlayerAboutBadWords(ClientComputer &computer) { // We could later count if the player is really often unpolite. MessageOut result; @@ -393,16 +395,16 @@ void ChatHandler::warnPlayerAboutBadWords(NetComputer &computer) result.writeByte(CHAT_USING_BAD_WORDS); // The Channel computer.send(result.getPacket()); - LOG_INFO(computer.getCharacter()->getName() << " says bad words.", 2) + LOG_INFO(computer.getCharacter()->getName() << " says bad words.", 2); } -void ChatHandler::announce(NetComputer &computer, const std::string& text) +void ChatHandler::announce(ClientComputer &computer, const std::string& text) { MessageOut result; if ( computer.getAccount()->getLevel() == (AccountLevels)AL_ADMIN || computer.getAccount()->getLevel() == (AccountLevels)AL_GM ) { - LOG_INFO("ANNOUNCE: " << text, 0) + LOG_INFO("ANNOUNCE: " << text, 0); // Send it to every beings. result.writeShort(SMSG_ANNOUNCEMENT); result.writeString(text); @@ -414,14 +416,14 @@ void ChatHandler::announce(NetComputer &computer, const std::string& text) result.writeByte(CHATCMD_UNSUFFICIENT_RIGHTS); computer.send(result.getPacket()); LOG_INFO(computer.getCharacter()->getName() << - " couldn't make an announcement due to insufficient rights.", 2) + " couldn't make an announcement due to insufficient rights.", 2); } } -void ChatHandler::sayAround(NetComputer &computer, const std::string& text) +void ChatHandler::sayAround(ClientComputer &computer, const std::string& text) { MessageOut result; - LOG_INFO( computer.getCharacter()->getName() << " says: " << text, 2) + LOG_INFO( computer.getCharacter()->getName() << " says: " << text, 2); // Send it to every beings around result.writeShort(SMSG_CHAT); result.writeShort(0); // The default channel @@ -432,11 +434,11 @@ void ChatHandler::sayAround(NetComputer &computer, const std::string& text) connectionHandler->sendAround(computer.getCharacter(), result); } -void ChatHandler::sayToPlayer(NetComputer &computer, const std::string& playerName, const std::string& text) +void ChatHandler::sayToPlayer(ClientComputer &computer, const std::string& playerName, const std::string& text) { MessageOut result; LOG_INFO( computer.getCharacter()->getName() << " says to " << playerName - << ": " << text, 2) + << ": " << text, 2); // Send it to the being if the being exists result.writeShort(SMSG_PRIVMSG); std::string say = computer.getCharacter()->getName(); @@ -446,11 +448,11 @@ void ChatHandler::sayToPlayer(NetComputer &computer, const std::string& playerNa connectionHandler->sendTo(playerName, result); } -void ChatHandler::sayInChannel(NetComputer &computer, short channel, const std::string& text) +void ChatHandler::sayInChannel(ClientComputer &computer, short channel, const std::string& text) { MessageOut result; LOG_INFO( computer.getCharacter()->getName() << " says in channel " << channel - << ": " << text, 2) + << ": " << text, 2); // Send it to every beings in channel result.writeShort(SMSG_CHAT_CNL); result.writeShort(channel); diff --git a/src/chathandler.h b/src/chathandler.h index 6336c890..d7273528 100644 --- a/src/chathandler.h +++ b/src/chathandler.h @@ -30,6 +30,7 @@ class MessageIn; class NetComputer; +class ClientComputer; /** * Manages all chat related @@ -46,34 +47,34 @@ class ChatHandler : public MessageHandler /** * Deals with command messages */ - void handleCommand(NetComputer &computer, const std::string& command); + void handleCommand(ClientComputer &computer, const std::string& command); /** * Tells the player to be more polite. */ - void warnPlayerAboutBadWords(NetComputer &computer); + void warnPlayerAboutBadWords(ClientComputer &computer); /** * Announce a message to every being in the default channel. */ - void announce(NetComputer &computer, const std::string& text); + void announce(ClientComputer &computer, const std::string& text); /** * Display a message to every player around one's player * in the default channel. * The tile area has been set to 10 for now. */ - void sayAround(NetComputer &computer, const std::string& text); + void sayAround(ClientComputer &computer, const std::string& text); /** * Say something private to a player. */ - void sayToPlayer(NetComputer &computer, const std::string& playerName, const std::string& text); + void sayToPlayer(ClientComputer &computer, const std::string& playerName, const std::string& text); /** * Say something in a specific channel. */ - void sayInChannel(NetComputer &computer, short channel, const std::string& text); + void sayInChannel(ClientComputer &computer, short channel, const std::string& text); }; #endif diff --git a/src/configuration.cpp b/src/configuration.cpp index fd2bca41..c2a84b23 100644 --- a/src/configuration.cpp +++ b/src/configuration.cpp @@ -61,7 +61,7 @@ void Configuration::init(const std::string &filename) xmlNodePtr node = xmlDocGetRootElement(doc); if (!node || !xmlStrEqual(node->name, BAD_CAST "configuration")) { - LOG_WARN("Warning: No configuration file (" << filename.c_str() << ")", 0) + LOG_WARN("Warning: No configuration file (" << filename.c_str() << ")", 0); return; } @@ -126,7 +126,7 @@ void Configuration::write() void Configuration::setValue(const std::string &key, std::string value) { - LOG_DEBUG("Configuration::setValue(" << key << ", " << value << ")", 2) + LOG_DEBUG("Configuration::setValue(" << key << ", " << value << ")", 2); options[key] = value; diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp index 1da0b830..bbb3e9d4 100644 --- a/src/connectionhandler.cpp +++ b/src/connectionhandler.cpp @@ -35,42 +35,6 @@ #include "script.h" #endif -/** - * TEMPORARY - * Split a string into a std::vector delimiting elements by 'split'. This - * function could be used for ASCII message handling (as we do not have - * a working client yet, using ASCII allows tools like Netcat to be used - * to test server functionality). - * - * This function may seem unoptimized, except it is this way to allow for - * thread safety. - */ -std::vector -stringSplit(const std::string &str, - const std::string &split) -{ - std::vector result; // temporary result - unsigned int i; - unsigned int last = 0; - - // iterate through string - for (i = 0; i < str.length(); i++) - { - if (str.compare(i, split.length(), split.c_str(), split.length()) == 0) - { - result.push_back(str.substr(last, i - last)); - last = i + 1; - } - } - - // add remainder of string - if (last < str.length()) { - result.push_back(str.substr(last, str.length())); - } - - return result; -} - /** * Convert a IP4 address into its string representation */ @@ -142,9 +106,8 @@ ConnectionHandler::process() LOG_INFO("A new client connected from " << ip4ToString(event.peer->address.host) << ":" << event.peer->address.port, 0); - NetComputer *comp = new NetComputer(this, event.peer); + NetComputer *comp = computerConnected(event.peer); clients.push_back(comp); - computerConnected(comp); /*LOG_INFO(ltd->host->peerCount << " client(s) connected", 0);*/ @@ -207,7 +170,6 @@ ConnectionHandler::process() << " disconected.", 0);*/ // Reset the peer's client information. computerDisconnected(comp); - delete comp; clients.erase(std::find(clients.begin(), clients.end(), comp)); event.peer->data = NULL; } break; @@ -217,40 +179,28 @@ ConnectionHandler::process() } } -void ConnectionHandler::computerConnected(NetComputer *comp) -{ - LOG_INFO("A client connected!", 0) -} - -void ConnectionHandler::computerDisconnected(NetComputer *comp) -{ - LOG_INFO("A client disconnected!", 0) -} - void ConnectionHandler::registerHandler( unsigned int msgId, MessageHandler *handler) { handlers[msgId] = handler; } -void ConnectionHandler::sendTo(tmwserv::BeingPtr beingPtr, MessageOut &msg) +void ClientConnectionHandler::sendTo(tmwserv::BeingPtr beingPtr, MessageOut &msg) { - for (NetComputers::iterator i = clients.begin(); - i != clients.end(); - i++) { - if ((*i)->getCharacter().get() == beingPtr.get()) { + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); + i != i_end; ++i) { + if (static_cast< ClientComputer * >(*i)->getCharacter().get() == beingPtr.get()) { (*i)->send(msg.getPacket()); break; } } } -void ConnectionHandler::sendTo(std::string name, MessageOut &msg) +void ClientConnectionHandler::sendTo(std::string name, MessageOut &msg) { - for (NetComputers::iterator i = clients.begin(); - i != clients.end(); - i++) { - if ((*i)->getCharacter().get()->getName() == name) { + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); + i != i_end; ++i) { + if (static_cast< ClientComputer * >(*i)->getCharacter()->getName() == name) { (*i)->send(msg.getPacket()); break; } @@ -259,24 +209,20 @@ void ConnectionHandler::sendTo(std::string name, MessageOut &msg) void ConnectionHandler::sendToEveryone(MessageOut &msg) { - for (NetComputers::iterator i = clients.begin(); - i != clients.end(); - i++) - { - (*i)->send(msg.getPacket()); - break; + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); + i != i_end; ++i) { + (*i)->send(msg.getPacket()); } } -void ConnectionHandler::sendAround(tmwserv::BeingPtr beingPtr, MessageOut &msg) +void ClientConnectionHandler::sendAround(tmwserv::BeingPtr beingPtr, MessageOut &msg) { unsigned speakerMapId = beingPtr->getMapId(); std::pair speakerXY = beingPtr->getXY(); for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); - i != i_end; - ++i) { + i != i_end; ++i) { // See if the other being is near enough, then send the message - tmwserv::Being const *listener = (*i)->getCharacter().get(); + tmwserv::Being const *listener = static_cast< ClientComputer * >(*i)->getCharacter().get(); if (listener->getMapId() != speakerMapId) continue; std::pair listenerXY = listener->getXY(); if (abs(listenerXY.first - speakerXY.first ) > (int)AROUND_AREA_IN_TILES) continue; @@ -285,17 +231,16 @@ void ConnectionHandler::sendAround(tmwserv::BeingPtr beingPtr, MessageOut &msg) } } -void ConnectionHandler::sendInChannel(short channelId, MessageOut &msg) +void ClientConnectionHandler::sendInChannel(short channelId, MessageOut &msg) { - for (NetComputers::iterator i = clients.begin(); i != clients.end();i++) - { + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); + i != i_end; ++i) { const std::vector beingList = chatChannelManager->getUserListInChannel(channelId); // If the being is in the channel, send it - for (std::vector::const_iterator j = beingList.begin(); - j != beingList.end(); j++) - { - if ((*i)->getCharacter().get() == (*j).get() ) + for (std::vector::const_iterator j = beingList.begin(), j_end = beingList.end(); + j != j_end; ++j) { + if (static_cast< ClientComputer * >(*i)->getCharacter().get() == j->get()) { (*i)->send(msg.getPacket()); } @@ -308,7 +253,7 @@ unsigned int ConnectionHandler::getClientNumber() return clients.size(); } -void ConnectionHandler::makeUsersLeaveChannel(const short channelId) +void ClientConnectionHandler::makeUsersLeaveChannel(const short channelId) { MessageOut result; result.writeShort(SMSG_QUIT_CHANNEL_RESPONSE); @@ -316,13 +261,12 @@ void ConnectionHandler::makeUsersLeaveChannel(const short channelId) const std::vector beingList = chatChannelManager->getUserListInChannel(channelId); - for (NetComputers::iterator i = clients.begin(); i != clients.end();i++) - { + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); + i != i_end; ++i) { // If the being is in the channel, send it the 'leave now' packet - for (std::vector::const_iterator j = beingList.begin(); - j != beingList.end(); j++) - { - if ((*i)->getCharacter().get() == (*j).get() ) + for (std::vector::const_iterator j = beingList.begin(), j_end = beingList.end(); + j != j_end; ++j) { + if (static_cast< ClientComputer * >(*i)->getCharacter().get() == j->get()) { (*i)->send(result.getPacket()); } @@ -330,9 +274,9 @@ void ConnectionHandler::makeUsersLeaveChannel(const short channelId) } } -void ConnectionHandler::warnUsersAboutPlayerEventInChat(const short channelId, - const std::string& userName, - const char eventId) +void ClientConnectionHandler::warnUsersAboutPlayerEventInChat(const short channelId, + const std::string& userName, + const char eventId) { MessageOut result; result.writeShort(SMSG_UPDATE_CHANNEL_RESPONSE); @@ -341,16 +285,25 @@ void ConnectionHandler::warnUsersAboutPlayerEventInChat(const short channelId, const std::vector beingList = chatChannelManager->getUserListInChannel(channelId); - for (NetComputers::iterator i = clients.begin(); i != clients.end();i++) - { + for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); + i != i_end; ++i) { // If the being is in the channel, send it the 'eventId' packet - for (std::vector::const_iterator j = beingList.begin(); - j != beingList.end(); j++) - { - if ((*i)->getCharacter().get() == (*j).get() ) + for (std::vector::const_iterator j = beingList.begin(), j_end = beingList.end(); + j != j_end; ++j) { + if (static_cast< ClientComputer * >(*i)->getCharacter().get() == j->get() ) { (*i)->send(result.getPacket()); } } } } + +NetComputer *ClientConnectionHandler::computerConnected(ENetPeer *peer) { + LOG_INFO("A client connected!", 0); + return new ClientComputer(this, peer); +} + +void ClientConnectionHandler::computerDisconnected(NetComputer *comp) { + delete comp; + LOG_INFO("A client disconnected!", 0); +} diff --git a/src/connectionhandler.h b/src/connectionhandler.h index d56fc09d..1b4e8036 100644 --- a/src/connectionhandler.h +++ b/src/connectionhandler.h @@ -59,6 +59,7 @@ class ClientData class ConnectionHandler { public: + virtual ~ConnectionHandler() {} /** * Open the server socket. @@ -77,33 +78,66 @@ class ConnectionHandler void process(); /** - * Called when a computer connects to a network session. + * Called when a computer sends a packet to the network session. */ - void computerConnected(NetComputer *computer); + //void receivePacket(NetComputer *computer, Packet *packet); /** - * Called when a computer reconnects to a network session. + * Registers a message handler to handle a certain message type. */ - //void computerReconnected(NetComputer *computer); + void registerHandler(unsigned int msgId, MessageHandler *handler); /** - * Called when a computer disconnects from a network session. - * - * Note: After returning from this method the NetComputer - * reference is no longer guaranteed to be valid. + * Send packet to every client, used for announcements. */ - void computerDisconnected(NetComputer *computer); + void sendToEveryone(MessageOut &); /** - * Called when a computer sends a packet to the network session. + * Return the number of connected clients. */ - //void receivePacket(NetComputer *computer, Packet *packet); + unsigned int getClientNumber(); + + private: + ENetAddress address; /**< Includes the port to listen to. */ + ENetHost *host; /**< The host that listen for connections. */ + + typedef std::map< unsigned int, MessageHandler * > HandlerMap; + HandlerMap handlers; + protected: /** - * Registers a message handler to handle a certain message type. + * Called when a computer connects to the server. Initialize + * an object derived of NetComputer. */ - void registerHandler(unsigned int msgId, MessageHandler *handler); + virtual NetComputer *computerConnected(ENetPeer *) = 0; + + /** + * Called when a computer reconnects to the server. + */ + //virtual NetComputer *computerReconnected(ENetPeer *) = 0; + + /** + * Called when a computer disconnects from the server. + * + * Note: After returning from this method the NetComputer + * reference is no longer guaranteed to be valid. + */ + virtual void computerDisconnected(NetComputer *) = 0; + typedef std::list NetComputers; + /** + * A list of pointers to the client structures created by + * computerConnected. + */ + NetComputers clients; +}; + +/** + * Temporary placeholder until the connection handlers have been split. + */ +class ClientConnectionHandler: public ConnectionHandler +{ + public: /** * Send packet to client with matching BeingPtr */ @@ -114,11 +148,6 @@ class ConnectionHandler */ void sendTo(std::string name, MessageOut &); - /** - * Send packet to every client, used for announcements. - */ - void sendToEveryone(MessageOut &); - /** * Send packet to every client around the client on screen. */ @@ -129,11 +158,6 @@ class ConnectionHandler */ void sendInChannel(short channelId, MessageOut &); - /** - * Return the number of connected clients. - */ - unsigned int getClientNumber(); - /** * tells a list of user they're leaving a channel. */ @@ -146,17 +170,11 @@ class ConnectionHandler const std::string& userName, const char eventId); - private: - ENetAddress address; /**< Includes the port to listen to. */ - ENetHost *host; /**< The host that listen for connections. */ - - typedef std::map< unsigned int, MessageHandler * > HandlerMap; - HandlerMap handlers; - - typedef std::list NetComputers; - NetComputers clients; + protected: + virtual NetComputer *computerConnected(ENetPeer *); + virtual void computerDisconnected(NetComputer *); }; -extern ConnectionHandler *connectionHandler; +extern ClientConnectionHandler *connectionHandler; #endif diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index cb0f87dc..b86219dc 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -83,7 +83,7 @@ DALStorage::open(void) mDb->connect(getName(), getUser(), getPassword()); if (!dbFileShown) { - LOG_INFO("Using " << dbFile << " as Database Name.", 0) + LOG_INFO("Using " << dbFile << " as Database Name.", 0); dbFileShown = true; } #elif defined (SQLITE_SUPPORT) @@ -92,7 +92,7 @@ DALStorage::open(void) mDb->connect(dbFile, "", ""); if (!dbFileShown) { - LOG_INFO("SQLite uses ./" << dbFile << " as DB.", 0) + LOG_INFO("SQLite uses ./" << dbFile << " as DB.", 0); dbFileShown = true; } #endif @@ -138,10 +138,10 @@ DALStorage::open(void) createTable(CHANNELS_TBL_NAME, SQL_CHANNELS_TABLE); } catch (const DbConnectionFailure& e) { - LOG_ERROR("unable to connect to the database: " << e.what(), 0) + LOG_ERROR("unable to connect to the database: " << e.what(), 0); } catch (const DbSqlQueryExecFailure& e) { - LOG_ERROR("SQL query failure: " << e.what(), 0) + LOG_ERROR("SQL query failure: " << e.what(), 0); } mIsOpen = mDb->isConnected(); @@ -229,7 +229,7 @@ DALStorage::getAccount(const std::string& userName) Beings beings; LOG_INFO(userName << "'s account has " << charInfo.rows() - << " character(s) in database.", 1) + << " character(s) in database.", 1); // As the recordset functions are set to be able to get one // recordset at a time, we store charInfo in a temp array of @@ -305,7 +305,7 @@ void DALStorage::addAccount(const AccountPtr& account) { if (account.get() == 0) { - LOG_WARN("Cannot add a NULL Account.", 0) + LOG_WARN("Cannot add a NULL Account.", 0); // maybe we should throw an exception instead return; } @@ -367,7 +367,7 @@ DALStorage::delAccount(const std::string& userName) } catch (const dal::DbSqlQueryExecFailure& e) { // TODO: throw an exception. - LOG_ERROR("SQL query failure: " << e.what(), 0) + LOG_ERROR("SQL query failure: " << e.what(), 0); } } @@ -480,7 +480,7 @@ DALStorage::doesCharacterNameExists(const std::string& name) } catch (const dal::DbSqlQueryExecFailure& e) { // TODO: throw an exception. - LOG_ERROR("SQL query failure: " << e.what(), 0) + LOG_ERROR("SQL query failure: " << e.what(), 0); } return false; @@ -555,7 +555,7 @@ DALStorage::getChannelList() channelInfo(i,3)))); LOG_DEBUG("Channel (" << channelInfo(i,0) << ") loaded: " << channelInfo(i,1) - << ": " << channelInfo(i,2), 5) + << ": " << channelInfo(i,2), 5); } return channels; @@ -606,7 +606,7 @@ DALStorage::updateChannels(std::map& channelList) << i->second.getPassword() << "\");"; LOG_DEBUG("Channel (" << i->first << ") saved: " << i->second.getName() - << ": " << i->second.getAnnouncement(), 5) + << ": " << i->second.getAnnouncement(), 5); } mDb->execSql(sql.str()); diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 2ccaaabb..ff4a0dde 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -30,8 +30,9 @@ #include "netcomputer.h" #include "packet.h" -void GameHandler::receiveMessage(NetComputer &computer, MessageIn &message) +void GameHandler::receiveMessage(NetComputer &comp, MessageIn &message) { + ClientComputer &computer = static_cast< ClientComputer & >(comp); if (computer.getCharacter().get() == NULL) return; diff --git a/src/main.cpp b/src/main.cpp index e7c599cb..98eb2989 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -100,7 +100,7 @@ ChatChannelManager *chatChannelManager; GameHandler *gameHandler; /** Primary connection handler */ -ConnectionHandler *connectionHandler; +ClientConnectionHandler *connectionHandler; /** * Initializes the server. @@ -150,8 +150,8 @@ void initialize() Logger::instance().setTeeMode(true); config.init(configPath); - LOG_INFO("Using Config File: " << configPath, 0) - LOG_INFO("Using Log File: " << logPath, 0) + LOG_INFO("Using Config File: " << configPath, 0); + LOG_INFO("Using Log File: " << logPath, 0); // Initialize the slang's filter. stringFilter = new StringFilter(&config); @@ -164,7 +164,7 @@ void initialize() chatHandler = new ChatHandler(); accountHandler = new AccountHandler(); gameHandler = new GameHandler(); - connectionHandler = new ConnectionHandler(); + connectionHandler = new ClientConnectionHandler(); // Reset to default segmentation fault handling for debugging purposes signal(SIGSEGV, SIG_DFL); @@ -174,13 +174,13 @@ void initialize() // initialize enet. if (enet_initialize() != 0) { - LOG_FATAL("An error occurred while initializing ENet", 0) + LOG_FATAL("An error occurred while initializing ENet", 0); exit(2); } // initialize scripting subsystem. #ifdef RUBY_SUPPORT - LOG_INFO("Script Language: " << scriptLanguage, 0) + LOG_INFO("Script Language: " << scriptLanguage, 0); // initialize ruby ruby_init(); @@ -194,17 +194,17 @@ void initialize() rb_load_file("scripts/init.rb"); rubyStatus = ruby_exec(); #else - LOG_WARN("No Scripting Language Support.", 0) + LOG_WARN("No Scripting Language Support.", 0); #endif #if defined (MYSQL_SUPPORT) - LOG_INFO("Using MySQL DB Backend.", 0) + LOG_INFO("Using MySQL DB Backend.", 0); #elif defined (POSTGRESQL_SUPPORT) - LOG_INFO("Using PostGreSQL DB Backend.", 0) + LOG_INFO("Using PostGreSQL DB Backend.", 0); #elif defined (SQLITE_SUPPORT) - LOG_INFO("Using SQLite DB Backend.", 0) + LOG_INFO("Using SQLite DB Backend.", 0); #else - LOG_WARN("No Database Backend Support.", 0) + LOG_WARN("No Database Backend Support.", 0); #endif // initialize configuration defaults @@ -298,14 +298,14 @@ void parseOptions(int argc, char *argv[]) unsigned short verbosityLevel; verbosityLevel = atoi(optarg); tmwserv::utils::Logger::instance().setVerbosity(verbosityLevel); - LOG_INFO("Setting Log Verbosity Level to " << verbosityLevel, 0) + LOG_INFO("Setting Log Verbosity Level to " << verbosityLevel, 0); break; case 'p': // Change the port to listen on. unsigned short portToListenOn; portToListenOn = atoi(optarg); config.setValue("ListenOnPort", portToListenOn); - LOG_INFO("Setting Default Port to " << portToListenOn, 0) + LOG_INFO("Setting Default Port to " << portToListenOn, 0); break; } } @@ -319,7 +319,7 @@ int main(int argc, char *argv[]) { int elapsedWorldTicks; - LOG_INFO("The Mana World Server v" << PACKAGE_VERSION, 0) + LOG_INFO("The Mana World Server v" << PACKAGE_VERSION, 0); // Parse Command Line Options parseOptions(argc, argv); @@ -396,13 +396,13 @@ int main(int argc, char *argv[]) // - Handle all messages that are in the message queue // - Update all active objects/beings connectionHandler->process(); - state.update(*connectionHandler); + state.update(); connectionHandler->process(); } worldTimer.sleep(); } - LOG_INFO("Received: Quit signal, closing down...", 0) + LOG_INFO("Received: Quit signal, closing down...", 0); connectionHandler->stopListen(); deinitialize(); } diff --git a/src/messagehandler.cpp b/src/messagehandler.cpp index ca4946ac..104ef2db 100644 --- a/src/messagehandler.cpp +++ b/src/messagehandler.cpp @@ -26,5 +26,5 @@ #include "utils/logger.h" void MessageHandler::receiveMessage(NetComputer &computer, MessageIn &message) { - LOG_WARN("MessageHandler class created without receiveMessage override", 0) + LOG_WARN("MessageHandler class created without receiveMessage override", 0); } diff --git a/src/netcomputer.cpp b/src/netcomputer.cpp index 1c736f47..7a06ba34 100644 --- a/src/netcomputer.cpp +++ b/src/netcomputer.cpp @@ -24,22 +24,16 @@ #include "netcomputer.h" #include "chatchannelmanager.h" +#include "connectionhandler.h" #include "packet.h" #include "state.h" NetComputer::NetComputer(ConnectionHandler *handler, ENetPeer *peer): handler(handler), - peer(peer), - mAccountPtr(NULL), - mCharacterPtr(NULL) + peer(peer) { } -NetComputer::~NetComputer() -{ - unsetAccount(); -} - void NetComputer::disconnect(const std::string &reason) { // Somehow notify the netsession listener about the disconnect after @@ -57,12 +51,12 @@ void NetComputer::send(const Packet *p) enet_peer_send(peer, 0, packet); } -void NetComputer::setAccount(tmwserv::AccountPtr acc) +void ClientComputer::setAccount(tmwserv::AccountPtr acc) { mAccountPtr = acc; } -void NetComputer::setCharacter(tmwserv::BeingPtr ch) +void ClientComputer::setCharacter(tmwserv::BeingPtr ch) { tmwserv::State &state = tmwserv::State::instance(); if (mCharacterPtr.get() != NULL) @@ -74,13 +68,13 @@ void NetComputer::setCharacter(tmwserv::BeingPtr ch) state.addBeing(mCharacterPtr, mCharacterPtr->getMapId()); } -void NetComputer::unsetAccount() +void ClientComputer::unsetAccount() { unsetCharacter(); mAccountPtr = tmwserv::AccountPtr(NULL); } -void NetComputer::unsetCharacter() +void ClientComputer::unsetCharacter() { // remove being from world tmwserv::State &state = tmwserv::State::instance(); @@ -89,3 +83,14 @@ void NetComputer::unsetCharacter() mCharacterPtr = tmwserv::BeingPtr(NULL); } +ClientComputer::ClientComputer(ClientConnectionHandler *handler, ENetPeer *peer): + NetComputer(handler, peer), + mAccountPtr(NULL), + mCharacterPtr(NULL) +{ +} + +ClientComputer::~ClientComputer() +{ + unsetAccount(); +} diff --git a/src/netcomputer.h b/src/netcomputer.h index de3e822d..7aaa78ea 100644 --- a/src/netcomputer.h +++ b/src/netcomputer.h @@ -34,6 +34,7 @@ // Forward declaration class ConnectionHandler; +class ClientConnectionHandler; class Packet; /** @@ -51,7 +52,7 @@ class NetComputer /** * Destructor */ - ~NetComputer(); + virtual ~NetComputer() {} /** * Returns true if this computer is disconnected. @@ -81,6 +82,29 @@ class NetComputer */ ENetPeer *getPeer() { return peer; } + private: + ConnectionHandler *handler; + + std::queue queue; /**< Message Queue (FIFO) */ + ENetPeer *peer; /**< Client peer */ +}; + +/** + * Temporary placeholder until the connection handlers have been split. + */ +class ClientComputer: public NetComputer +{ + public: + /** + * Constructor + */ + ClientComputer(ClientConnectionHandler *handler, ENetPeer *peer); + + /** + * Destructor + */ + ~ClientComputer(); + /** * Set the account associated with the connection */ @@ -119,11 +143,6 @@ class NetComputer getCharacter() { return mCharacterPtr; } private: - ConnectionHandler *handler; - - std::queue queue; /**< Message Queue (FIFO) */ - ENetPeer *peer; /**< Client peer */ - /** Account associated with connection */ tmwserv::AccountPtr mAccountPtr; diff --git a/src/skill.cpp b/src/skill.cpp index 6990a348..9d9c3065 100644 --- a/src/skill.cpp +++ b/src/skill.cpp @@ -77,7 +77,7 @@ bool Skill::deleteSkill(const std::string &ident, bool delTree) { //prevent deletion of self if (ident == id) { - LOG_ERROR("Skill: Attempt to delete self.", 0) + LOG_ERROR("Skill: Attempt to delete self.", 0); return false; } diff --git a/src/state.cpp b/src/state.cpp index b7bc7bf4..8f8c3692 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -44,7 +44,7 @@ State::~State() throw() { } } -void State::update(ConnectionHandler &connectionHandler) +void State::update() { // update game state (update AI, etc.) for (std::map::iterator i = maps.begin(); @@ -78,7 +78,7 @@ void State::update(ConnectionHandler &connectionHandler) msg.writeLong(b2->get()->getX());// x msg.writeLong(b2->get()->getY());// y - connectionHandler.sendTo(*b, msg); + connectionHandler->sendTo(*b, msg); } } } diff --git a/src/state.h b/src/state.h index 65acf4bb..0c99afc5 100644 --- a/src/state.h +++ b/src/state.h @@ -82,7 +82,7 @@ class State : public utils::Singleton /** * Update game state (contains core server logic) */ - void update(ConnectionHandler &); + void update(); /** * Add being to game world at specified map diff --git a/src/utils/logger.h b/src/utils/logger.h index 1e5ba27d..d3f0d1a6 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -300,45 +300,45 @@ class Logger: public Singleton #define LOG(msg, atVerbosity) \ - { \ + do { \ std::ostringstream os; \ os << msg; \ ::tmwserv::utils::Logger::instance().log(os.str(), atVerbosity); \ - } + } while(0) #define LOG_DEBUG(msg, atVerbosity) \ - { \ + do { \ std::ostringstream os; \ os << msg; \ ::tmwserv::utils::Logger::instance().debug(os.str(), atVerbosity); \ - } + } while (0) #define LOG_INFO(msg, atVerbosity) \ - { \ + do { \ std::ostringstream os; \ os << msg; \ ::tmwserv::utils::Logger::instance().info(os.str(), atVerbosity); \ - } + } while (0) #define LOG_WARN(msg, atVerbosity) \ - { \ + do { \ std::ostringstream os; \ os << msg; \ ::tmwserv::utils::Logger::instance().warn(os.str(), atVerbosity); \ - } + } while (0) #define LOG_ERROR(msg, atVerbosity) \ - { \ + do { \ std::ostringstream os; \ os << msg; \ ::tmwserv::utils::Logger::instance().error(os.str(), atVerbosity); \ - } + } while (0) #define LOG_FATAL(msg, atVerbosity) \ - { \ + do { \ std::ostringstream os; \ os << msg; \ ::tmwserv::utils::Logger::instance().fatal(os.str(), atVerbosity); \ - } + } while (0) #endif // _TMWSERV_LOGGER_H_ diff --git a/src/utils/stringfilter.cpp b/src/utils/stringfilter.cpp index a75d8506..cbb075a4 100644 --- a/src/utils/stringfilter.cpp +++ b/src/utils/stringfilter.cpp @@ -79,7 +79,7 @@ void StringFilter::writeSlangFilterList() bool StringFilter::filterContent(const std::string& text) { if (!mInitialized) { - LOG_INFO("Slangs List is not initialized.", 2) + LOG_INFO("Slangs List is not initialized.", 2); return true; } @@ -111,7 +111,7 @@ bool StringFilter::isEmailValid(const std::string& email) if ((email.length() < MIN_EMAIL_LENGTH) || (email.length() > MAX_EMAIL_LENGTH)) { - LOG_INFO(email << ": Email too short or too long.", 1) + LOG_INFO(email << ": Email too short or too long.", 1); return false; } -- cgit v1.2.3-70-g09d2