diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-11-02 12:43:49 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-11-02 12:43:49 +0000 |
commit | 5f0ebee0d4d75fa91d417f4f352abdbc7502c2f0 (patch) | |
tree | b030f9ff00fceb9f90fab2c9fd56bdc16cd4126c | |
parent | fa035c2fd70945a0b12143ce9b81284d8d24d13b (diff) | |
download | mana-5f0ebee0d4d75fa91d417f4f352abdbc7502c2f0.tar.gz mana-5f0ebee0d4d75fa91d417f4f352abdbc7502c2f0.tar.bz2 mana-5f0ebee0d4d75fa91d417f4f352abdbc7502c2f0.tar.xz mana-5f0ebee0d4d75fa91d417f4f352abdbc7502c2f0.zip |
Network layer refactoring.
39 files changed, 1443 insertions, 377 deletions
@@ -1,3 +1,25 @@ +2006-11-02 Björn Steinbrink <B.Steinbrink@gmx.de> + + * src/localplayer.cpp, src/game.cpp, src/gui/trade.cpp, + src/gui/sell.cpp, src/gui/char_select.cpp, src/gui/serverdialog.cpp, + src/gui/chat.cpp, src/gui/buy.cpp, src/gui/status.cpp, + src/beingmanager.cpp, src/npc.cpp, src/main.cpp, src/CMakeLists.txt, + src/net/connection.cpp, src/net/accountserver, + src/net/accountserver/account.h, src/net/accountserver/internal.h, + src/net/accountserver/accountserver.cpp, + src/net/accountserver/accountserver.h, + src/net/accountserver/account.cpp, src/net/accountserver/internal.cpp, + src/net/internal.cpp, src/net/network.h, + src/net/charserverhandler.cpp, src/net/connection.h, + src/net/gameserver, src/net/gameserver/gameserver.cpp, + src/net/gameserver/player.h, src/net/gameserver/internal.h, + src/net/gameserver/gameserver.h, src/net/gameserver/internal.cpp, + src/net/gameserver/player.cpp, src/net/internal.h, + src/net/messagehandler.cpp, src/net/chatserver, + src/net/chatserver/internal.h, src/net/chatserver/chatserver.cpp, + src/net/chatserver/chatserver.h, src/net/chatserver/internal.cpp, + src/net/network.cpp, src/Makefile.am: Network layer refactoring. + 2006-11-01 Björn Steinbrink <B.Steinbrink@gmx.de> * CMake/Modules/FindLibXml2.cmake, CMake/Modules/FindENet.cmake, diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b3bf323e..f99ded75 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -166,8 +166,12 @@ SET(SRCS net/charserverhandler.h net/chathandler.cpp net/chathandler.h + net/connection.cpp + net/connection.h net/equipmenthandler.cpp net/equipmenthandler.h + net/internal.cpp + net/internal.h net/inventoryhandler.cpp net/inventoryhandler.h net/itemhandler.cpp @@ -193,6 +197,22 @@ SET(SRCS net/skillhandler.h net/tradehandler.cpp net/tradehandler.h + net/accountserver/account.cpp + net/accountserver/account.h + net/accountserver/accountserver.cpp + net/accountserver/accountserver.h + net/accountserver/internal.cpp + net/accountserver/internal.h + net/chatserver/chatserver.cpp + net/chatserver/chatserver.h + net/chatserver/internal.cpp + net/chatserver/internal.h + net/gameserver/gameserver.cpp + net/gameserver/gameserver.h + net/gameserver/internal.cpp + net/gameserver/internal.h + net/gameserver/player.cpp + net/gameserver/player.h resources/buddylist.cpp resources/buddylist.h resources/image.cpp diff --git a/src/Makefile.am b/src/Makefile.am index b621c63e..655c10de 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -131,6 +131,8 @@ tmw_SOURCES = graphic/imagerect.h \ net/charserverhandler.cpp \ net/chathandler.h \ net/chathandler.cpp \ + net/connection.h \ + net/connection.cpp \ net/equipmenthandler.h \ net/equipmenthandler.cpp \ net/inventoryhandler.h \ @@ -158,6 +160,22 @@ tmw_SOURCES = graphic/imagerect.h \ net/skillhandler.h \ net/tradehandler.cpp \ net/tradehandler.h \ + net/accountserver/account.cpp \ + net/accountserver/account.h \ + net/accountserver/accountserver.cpp \ + net/accountserver/accountserver.h \ + net/accountserver/internal.cpp \ + net/accountserver/internal.h \ + net/chatserver/chatserver.cpp \ + net/chatserver/chatserver.h \ + net/chatserver/internal.cpp \ + net/chatserver/internal.h \ + net/gameserver/gameserver.cpp \ + net/gameserver/gameserver.h \ + net/gameserver/internal.cpp \ + net/gameserver/internal.h \ + net/gameserver/player.cpp \ + net/gameserver/player.h \ resources/image.cpp \ resources/image.h \ resources/imagewriter.cpp \ diff --git a/src/beingmanager.cpp b/src/beingmanager.cpp index 30b68ee2..923283b5 100644 --- a/src/beingmanager.cpp +++ b/src/beingmanager.cpp @@ -28,9 +28,6 @@ #include "npc.h" #include "player.h" -#include "net/messageout.h" -#include "net/protocol.h" - #include "utils/dtor.h" class FindBeingFunctor @@ -69,8 +66,11 @@ Being* BeingManager::createBeing(Uint16 id, Uint16 job) if (job < 10) { being = new Player(id, job, mMap); + // XXX Convert for new server + /* MessageOut outMsg(0x0094); outMsg.writeLong(id); + */ } else if (job >= 100 & job < 200) being = new NPC(id, job, mMap); diff --git a/src/game.cpp b/src/game.cpp index 5e4c24c2..2abd9e26 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -292,16 +292,16 @@ Game::Game(): joystick = new Joystick(0); } - Network::registerHandler(mBeingHandler.get()); - Network::registerHandler(mBuySellHandler.get()); - Network::registerHandler(mChatHandler.get()); - Network::registerHandler(mEquipmentHandler.get()); - Network::registerHandler(mInventoryHandler.get()); - Network::registerHandler(mItemHandler.get()); - Network::registerHandler(mNpcHandler.get()); - Network::registerHandler(mPlayerHandler.get()); - Network::registerHandler(mSkillHandler.get()); - Network::registerHandler(mTradeHandler.get()); + Net::registerHandler(mBeingHandler.get()); + Net::registerHandler(mBuySellHandler.get()); + Net::registerHandler(mChatHandler.get()); + Net::registerHandler(mEquipmentHandler.get()); + Net::registerHandler(mInventoryHandler.get()); + Net::registerHandler(mItemHandler.get()); + Net::registerHandler(mNpcHandler.get()); + Net::registerHandler(mPlayerHandler.get()); + Net::registerHandler(mSkillHandler.get()); + Net::registerHandler(mTradeHandler.get()); } Game::~Game() @@ -403,7 +403,7 @@ void Game::logic() } // Handle network stuff - Network::flush(); + Net::flush(); } } diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp index ae779503..41bdd67e 100644 --- a/src/gui/buy.cpp +++ b/src/gui/buy.cpp @@ -36,9 +36,6 @@ #include "../resources/iteminfo.h" #include "../resources/itemmanager.h" -#include "../net/messageout.h" -#include "../net/protocol.h" - #include "../utils/tostring.h" @@ -225,10 +222,13 @@ void BuyDialog::action(const std::string &eventId, gcn::Widget *widget) else if (eventId == "buy" && (mAmountItems > 0 && mAmountItems <= mMaxItems)) { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_BUY_REQUEST); outMsg.writeShort(8); outMsg.writeShort(mAmountItems); outMsg.writeShort(mShopItems->at(selectedItem).id); + */ // update money ! mMoney -= mAmountItems * mShopItems->at(selectedItem).price; diff --git a/src/gui/char_select.cpp b/src/gui/char_select.cpp index 3e6c4a5f..d825db31 100644 --- a/src/gui/char_select.cpp +++ b/src/gui/char_select.cpp @@ -37,9 +37,7 @@ #include "../localplayer.h" #include "../main.h" -#include "../net/messageout.h" -#include "../net/network.h" -#include "../net/protocol.h" +#include "../net/accountserver/account.h" #include "../utils/tostring.h" @@ -134,7 +132,8 @@ void CharSelectDialog::action(const std::string &eventId, gcn::Widget *widget) mPreviousButton->setEnabled(false); mNextButton->setEnabled(false); mCharSelected = true; - attemptCharSelect(); + Net::AccountServer::Account::selectCharacter(mCharInfo->getPos()); + mCharInfo->lock(); } else if (eventId == "cancel") { @@ -202,20 +201,7 @@ void CharSelectDialog::updatePlayerInfo() void CharSelectDialog::attemptCharDelete() { - // Request character deletion - MessageOut msg(PAMSG_CHAR_DELETE); - // TODO: Send the selected slot - msg.writeByte(0); - Network::send(Network::ACCOUNT, msg); - mCharInfo->lock(); -} - -void CharSelectDialog::attemptCharSelect() -{ - // Request character selection - MessageOut msg(PAMSG_CHAR_SELECT); - msg.writeByte(mCharInfo->getPos()); - Network::send(Network::ACCOUNT, msg); + Net::AccountServer::Account::deleteCharacter(mCharInfo->getPos()); mCharInfo->lock(); } @@ -312,7 +298,15 @@ void CharCreateDialog::action(const std::string &eventId, gcn::Widget *widget) if (getName().length() >= 4) { // Attempt to create the character mCreateButton->setEnabled(false); - attemptCharCreate(); + Net::AccountServer::Account::createCharacter( + getName(), mPlayerBox->mHairStyle, mPlayerBox->mHairColor, + 0, // gender + 10, // STR + 10, // AGI + 10, // VIT + 10, // INT + 10, // DEX + 10); // LUK scheduleDelete(); } else { @@ -344,21 +338,3 @@ std::string CharCreateDialog::getName() { return mNameField->getText(); } - -void CharCreateDialog::attemptCharCreate() -{ - // Send character infos - MessageOut outMsg(PAMSG_CHAR_CREATE); - outMsg.writeString(getName()); - outMsg.writeByte(mPlayerBox->mHairStyle); - outMsg.writeByte(mPlayerBox->mHairColor); - // TODO: send selected sex - outMsg.writeByte(0); // Player sex - outMsg.writeShort(10); // STR - outMsg.writeShort(10); // AGI - outMsg.writeShort(10); // VIT - outMsg.writeShort(10); // INT - outMsg.writeShort(10); // DEX - outMsg.writeShort(10); // LUK - Network::send(Network::ACCOUNT, outMsg); -} diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 9a5d60b0..3dc252ab 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -36,9 +36,9 @@ #include "../game.h" #include "../localplayer.h" -#include "../net/messageout.h" -#include "../net/network.h" -#include "../net/protocol.h" +#include "../net/chatserver/chatserver.h" + +#include "../net/gameserver/player.h" ChatWindow::ChatWindow(): Window(""), @@ -249,16 +249,12 @@ ChatWindow::chatSend(const std::string &nick, std::string msg) // Prepare ordinary message if (msg.substr(0, 1) != "/") { - MessageOut outMsg(PGMSG_SAY); - outMsg.writeString(msg); - Network::send(Network::GAME, outMsg); + Net::GameServer::Player::say(msg); } else if (msg.substr(0, IS_ANNOUNCE_LENGTH) == IS_ANNOUNCE) { msg.erase(0, IS_ANNOUNCE_LENGTH); - MessageOut outMsg(0x0099); - outMsg.writeShort(msg.length() + 4); - outMsg.writeString(msg, msg.length()); + Net::ChatServer::announce(msg); } else if (msg.substr(0, IS_HELP_LENGTH) == IS_HELP) { @@ -274,7 +270,10 @@ ChatWindow::chatSend(const std::string &nick, std::string msg) } else if (msg.substr(0, IS_WHO_LENGTH) == IS_WHO) { + // XXX Convert for new server + /* MessageOut outMsg(0x00c1); + */ } else { diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index d6d8cad5..9c25aced 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -39,9 +39,6 @@ #include "../resources/iteminfo.h" #include "../resources/itemmanager.h" -#include "../net/messageout.h" -#include "../net/protocol.h" - #include "../utils/tostring.h" SellDialog::SellDialog(): @@ -218,10 +215,13 @@ void SellDialog::action(const std::string &eventId, gcn::Widget *widget) // Attempt sell assert(mAmountItems > 0 && mAmountItems <= mMaxItems); + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_SELL_REQUEST); outMsg.writeShort(8); outMsg.writeShort(mShopItems->at(selectedItem).index); outMsg.writeShort(mAmountItems); + */ mMaxItems -= mAmountItems; mAmountItems = 0; diff --git a/src/gui/serverdialog.cpp b/src/gui/serverdialog.cpp index 450ae809..39abd5ed 100644 --- a/src/gui/serverdialog.cpp +++ b/src/gui/serverdialog.cpp @@ -236,9 +236,6 @@ ServerDialog::action(const std::string &eventId, gcn::Widget *widget) currentConfig = "MostUsedServerPort" + toString(i); config.setValue(currentConfig, toString(currentServer.port)); } - logger->log("Trying to connect to account server..."); - Network::connect(Network::ACCOUNT, - mLoginData->hostname, mLoginData->port); state = STATE_CONNECT_ACCOUNT; } } diff --git a/src/gui/status.cpp b/src/gui/status.cpp index 2b61ed35..b53e0942 100644 --- a/src/gui/status.cpp +++ b/src/gui/status.cpp @@ -368,27 +368,27 @@ void StatusWindow::action(const std::string &eventId, gcn::Widget *widget) { if (eventId == "STR") { - player_node->raiseAttribute(LocalPlayer::STR); + mPlayer->raiseAttribute(LocalPlayer::STR); } if (eventId == "AGI") { - player_node->raiseAttribute(LocalPlayer::AGI); + mPlayer->raiseAttribute(LocalPlayer::AGI); } if (eventId == "VIT") { - player_node->raiseAttribute(LocalPlayer::VIT); + mPlayer->raiseAttribute(LocalPlayer::VIT); } if (eventId == "INT") { - player_node->raiseAttribute(LocalPlayer::INT); + mPlayer->raiseAttribute(LocalPlayer::INT); } if (eventId == "DEX") { - player_node->raiseAttribute(LocalPlayer::DEX); + mPlayer->raiseAttribute(LocalPlayer::DEX); } if (eventId == "LUK") { - player_node->raiseAttribute(LocalPlayer::LUK); + mPlayer->raiseAttribute(LocalPlayer::LUK); } } } diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index 44efbdb1..630881ea 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -38,9 +38,6 @@ #include "../inventory.h" #include "../item.h" -#include "../net/messageout.h" -#include "../net/protocol.h" - #include "../resources/iteminfo.h" #include "../utils/tostring.h" @@ -216,9 +213,12 @@ void TradeWindow::receivedOk(bool own) void TradeWindow::tradeItem(Item *item, int quantity) { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_ITEM_ADD_REQUEST); outMsg.writeShort(item->getInvIndex()); outMsg.writeLong(quantity); + */ } void TradeWindow::selectionChanged(const SelectionEvent &event) @@ -288,7 +288,10 @@ void TradeWindow::action(const std::string &eventId, gcn::Widget *widget) } else if (eventId == "cancel") { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_CANCEL_REQUEST); + */ } else if (eventId == "ok") { @@ -298,17 +301,27 @@ void TradeWindow::action(const std::string &eventId, gcn::Widget *widget) { mMoneyField->setText(toString(tempInt)); + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_ITEM_ADD_REQUEST); outMsg.writeShort(0); outMsg.writeLong(tempInt); + */ } else { mMoneyField->setText(""); } mMoneyField->setEnabled(false); + + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_ADD_COMPLETE); + */ } else if (eventId == "trade") { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_OK); + */ } } diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 87a55744..6898dfb7 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -31,9 +31,7 @@ #include "main.h" #include "sound.h" -#include "net/messageout.h" -#include "net/network.h" -#include "net/protocol.h" +#include "net/gameserver/player.h" LocalPlayer *player_node = NULL; @@ -100,9 +98,10 @@ Item* LocalPlayer::getInvItem(int index) void LocalPlayer::equipItem(Item *item) { - MessageOut outMsg(CMSG_PLAYER_EQUIP); - outMsg.writeShort(item->getInvIndex()); - outMsg.writeShort(0); + // XXX What's itemId and slot exactly? Same as eAthena? + /* + Net::GameServer::Player::equip(itemId, slot)); + */ } void LocalPlayer::unequipItem(Item *item) @@ -110,8 +109,11 @@ void LocalPlayer::unequipItem(Item *item) if (!item) return; + // XXX Convert for new server + /* MessageOut outMsg(CMSG_PLAYER_UNEQUIP); outMsg.writeShort(item->getInvIndex()); + */ // Tidy equipment directly to avoid weapon still shown bug, by instance mEquipment->removeEquipment(item); @@ -119,18 +121,23 @@ void LocalPlayer::unequipItem(Item *item) void LocalPlayer::useItem(Item *item) { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_PLAYER_INVENTORY_USE); outMsg.writeShort(item->getInvIndex()); outMsg.writeLong(item->getId()); // Note: id is dest of item, usually player_node->account_ID ?? + */ } void LocalPlayer::dropItem(Item *item, int quantity) { - // TODO: Fix wrong coordinates of drops, serverside? + // XXX Convert for new server + /* MessageOut outMsg(CMSG_PLAYER_INVENTORY_DROP); outMsg.writeShort(item->getInvIndex()); outMsg.writeShort(quantity); + */ } void LocalPlayer::pickUp(FloorItem *item) @@ -139,8 +146,11 @@ void LocalPlayer::pickUp(FloorItem *item) int dy = item->getY() - mY / 32; if (dx * dx + dy * dy < 4) { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_ITEM_PICKUP); outMsg.writeLong(item->getId()); + */ mPickUpTarget = NULL; } else { setDestination(item->getX() * 32 + 16, item->getY() * 32 + 16); @@ -206,10 +216,7 @@ void LocalPlayer::setDestination(Uint16 x, Uint16 y) x = tx * 32 + fx; y = ty * 32 + fy; - MessageOut msg(PGMSG_WALK); - msg.writeShort(x); - msg.writeShort(y); - Network::send(Network::GAME, msg); + Net::GameServer::Player::walk(x, y); mPickUpTarget = NULL; @@ -218,6 +225,8 @@ void LocalPlayer::setDestination(Uint16 x, Uint16 y) void LocalPlayer::raiseAttribute(Attribute attr) { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_STAT_UPDATE_REQUEST); switch (attr) @@ -247,6 +256,7 @@ void LocalPlayer::raiseAttribute(Attribute attr) break; } outMsg.writeByte(1); + */ } void LocalPlayer::raiseSkill(Uint16 skillId) @@ -254,8 +264,11 @@ void LocalPlayer::raiseSkill(Uint16 skillId) if (mSkillPoint <= 0) return; + // XXX Convert for new server + /* MessageOut outMsg(CMSG_SKILL_LEVELUP_REQUEST); outMsg.writeShort(skillId); + */ } void LocalPlayer::toggleSit() @@ -272,9 +285,12 @@ void LocalPlayer::toggleSit() default: return; } + // XXX Convert for new server + /* MessageOut outMsg(0x0089); outMsg.writeLong(0); outMsg.writeByte(type); + */ } void LocalPlayer::emote(Uint8 emotion) @@ -283,8 +299,11 @@ void LocalPlayer::emote(Uint8 emotion) return; mLastAction = tick_time; + // XXX Convert for new server + /* MessageOut outMsg(0x00bf); outMsg.writeByte(emotion); + */ } void LocalPlayer::tradeReply(bool accept) @@ -292,14 +311,20 @@ void LocalPlayer::tradeReply(bool accept) if (!accept) mTrading = false; + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_RESPONSE); outMsg.writeByte(accept ? 3 : 4); + */ } void LocalPlayer::trade(Being *being) const { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_TRADE_REQUEST); outMsg.writeLong(being->getId()); + */ } bool LocalPlayer::tradeRequestOk() const @@ -349,9 +374,12 @@ void LocalPlayer::attack(Being *target, bool keep) else sound.playSfx("sfx/fist-swish.ogg"); + // XXX Convert for new server + /* MessageOut outMsg(0x0089); outMsg.writeLong(target->getId()); outMsg.writeByte(0); + */ } void LocalPlayer::stopAttack() @@ -366,6 +394,9 @@ Being* LocalPlayer::getTarget() const void LocalPlayer::revive() { + // XXX Convert for new server + /* MessageOut outMsg(0x00b2); outMsg.writeByte(0); + */ } diff --git a/src/main.cpp b/src/main.cpp index f881ddad..41d49b43 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -66,11 +66,16 @@ #include "gui/textfield.h" #include "net/charserverhandler.h" +#include "net/connection.h" #include "net/loginhandler.h" #include "net/maploginhandler.h" -#include "net/messageout.h" #include "net/network.h" -#include "net/protocol.h" + +#include "net/accountserver/accountserver.h" + +#include "net/chatserver/chatserver.h" + +#include "net/gameserver/gameserver.h" #include "resources/image.h" #include "resources/resourcemanager.h" @@ -98,6 +103,10 @@ Music *bgm; Configuration config; /**< Xml file configuration reader */ Logger *logger; /**< Log object */ +Net::Connection *accountServerConnection = 0; +Net::Connection *gameServerConnection = 0; +Net::Connection *chatServerConnection = 0; + namespace { struct ErrorListener : public gcn::ActionListener { @@ -443,18 +452,15 @@ MapLoginHandler mapLoginHandler; void accountLogin(LoginData *loginData) { logger->log("Username is %s", loginData->username.c_str()); - Network::registerHandler(&loginHandler); - Network::registerHandler(&charServerHandler); + Net::registerHandler(&loginHandler); + Net::registerHandler(&charServerHandler); loginHandler.setLoginData(loginData); charServerHandler.setLoginData(loginData); charServerHandler.setCharInfo(&charInfo); // Send login infos - MessageOut msg(PAMSG_LOGIN); - msg.writeLong(0); // client version - msg.writeString(loginData->username); - msg.writeString(loginData->password); - Network::send(Network::ACCOUNT, msg); + Net::AccountServer::login(accountServerConnection, 0, + loginData->username, loginData->password); // Clear the password, avoids auto login when returning to login loginData->password = ""; @@ -471,36 +477,25 @@ void accountLogin(LoginData *loginData) void accountRegister(LoginData *loginData) { logger->log("Username is %s", loginData->username.c_str()); - Network::registerHandler(&loginHandler); + Net::registerHandler(&loginHandler); loginHandler.setLoginData(loginData); charServerHandler.setLoginData(loginData); charServerHandler.setCharInfo(&charInfo); - // Send login infos - MessageOut msg(PAMSG_REGISTER); - msg.writeLong(0); // client version - msg.writeString(loginData->username); - msg.writeString(loginData->password); - msg.writeString(loginData->email); - Network::send(Network::ACCOUNT, msg); + Net::AccountServer::registerAccount(accountServerConnection, 0, + loginData->username, loginData->password, loginData->email); } void mapLogin(LoginData *loginData) { - Network::registerHandler(&mapLoginHandler); + Net::registerHandler(&mapLoginHandler); logger->log("Memorizing selected character %s", player_node->getName().c_str()); config.setValue("lastCharacter", player_node->getName()); - // Send connect messages with the magic token to game and chat servers - MessageOut gameServerConnect(PGMSG_CONNECT); - gameServerConnect.writeString(token, 32); - Network::send(Network::GAME, gameServerConnect); - - MessageOut chatServerConnect(PCMSG_CONNECT); - chatServerConnect.writeString(token, 32); - Network::send(Network::CHAT, chatServerConnect); + Net::GameServer::connect(gameServerConnection, token); + Net::ChatServer::connect(chatServerConnection, token); } /** Main */ @@ -582,12 +577,10 @@ int main(int argc, char *argv[]) loginData.remember = config.getValue("remember", 0); - if (enet_initialize() != 0) - { - logger->error("An error occurred while initializing ENet."); - } - Network::initialize(); - + Net::initialize(); + accountServerConnection = Net::getConnection(); + gameServerConnection = Net::getConnection(); + chatServerConnection = Net::getConnection(); SDL_Event event; @@ -610,12 +603,15 @@ int main(int argc, char *argv[]) } gui->logic(); - Network::flush(); + Net::flush(); - if (Network::getState() == Network::NET_ERROR) + if (state > STATE_CONNECT_ACCOUNT && state < STATE_GAME) { - state = STATE_ERROR; - errorMessage = "Got disconnected from server!"; + if (!accountServerConnection->isConnected()) + { + state = STATE_ERROR; + errorMessage = "Got disconnected from account server!"; + } } if (!login_wallpaper) @@ -638,7 +634,7 @@ int main(int argc, char *argv[]) // TODO: Add connect timeout to go back to choose server if (state == STATE_CONNECT_ACCOUNT && - Network::isConnected(Network::ACCOUNT)) + accountServerConnection->isConnected()) { if (options.skipUpdate) { state = STATE_LOGIN; @@ -647,10 +643,10 @@ int main(int argc, char *argv[]) } } else if (state == STATE_CONNECT_GAME && - Network::isConnected(Network::GAME) && - Network::isConnected(Network::CHAT)) + gameServerConnection->isConnected() && + chatServerConnection->isConnected()) { - // TODO: Somehow send the token + accountServerConnection->disconnect(); state = STATE_GAME; } @@ -661,12 +657,6 @@ int main(int argc, char *argv[]) loadUpdates(); } - // Disconnect from account server once connected to game server - if (oldstate == STATE_CONNECT_GAME && state == STATE_GAME) - { - Network::disconnect(Network::ACCOUNT); - } - oldstate = state; // Get rid of the dialog of the previous state @@ -685,9 +675,6 @@ int main(int argc, char *argv[]) if (options.serverName.empty() && options.serverPort == 0) { currentDialog = new ServerDialog(&loginData); } else { - logger->log("Trying to connect to account server..."); - Network::connect(Network::ACCOUNT, - loginData.hostname, loginData.port); state = STATE_CONNECT_ACCOUNT; // Reset options so that cancelling or connect timeout @@ -699,6 +686,9 @@ int main(int argc, char *argv[]) case STATE_CONNECT_ACCOUNT: logger->log("State: CONNECT_ACCOUNT"); + logger->log("Trying to connect to account server..."); + accountServerConnection->connect(loginData.hostname, + loginData.port); currentDialog = new ConnectionDialog(STATE_CHOOSE_SERVER); break; @@ -751,9 +741,9 @@ int main(int argc, char *argv[]) currentDialog = new OkDialog("Error", errorMessage); currentDialog->addActionListener(&errorListener); currentDialog = NULL; // OkDialog deletes itself - Network::disconnect(Network::GAME); - Network::disconnect(Network::CHAT); - Network::clearHandlers(); + gameServerConnection->disconnect(); + chatServerConnection->disconnect(); + Net::clearHandlers(); break; case STATE_CONNECT_GAME: @@ -783,8 +773,10 @@ int main(int argc, char *argv[]) } } - Network::finalize(); - enet_deinitialize(); + delete accountServerConnection; + delete gameServerConnection; + delete chatServerConnection; + Net::finalize(); if (nullFile) { diff --git a/src/net/accountserver/account.cpp b/src/net/accountserver/account.cpp new file mode 100644 index 00000000..385cd77a --- /dev/null +++ b/src/net/accountserver/account.cpp @@ -0,0 +1,116 @@ +/* + * The Mana World + * 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$ + */ + +#include "account.h" + +#include "internal.h" + +#include "../connection.h" +#include "../messageout.h" +#include "../protocol.h" + +void Net::AccountServer::Account::createCharacter( + const std::string &name, char hairColor, char hairStyle, char gender, + short strength, short agility, short vitality, + short intelligence, short dexterity, short luck) +{ + MessageOut msg(PAMSG_CHAR_CREATE); + + msg.writeString(name); + msg.writeByte(hairStyle); + msg.writeByte(hairColor); + msg.writeByte(gender); + msg.writeShort(strength); + msg.writeShort(agility); + msg.writeShort(vitality); + msg.writeShort(intelligence); + msg.writeShort(dexterity); + msg.writeShort(luck); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::deleteCharacter(char slot) +{ + MessageOut msg(PAMSG_CHAR_DELETE); + + msg.writeByte(slot); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::selectCharacter(char slot) +{ + MessageOut msg(PAMSG_CHAR_SELECT); + + msg.writeByte(slot); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::unregister() +{ + MessageOut msg(PAMSG_UNREGISTER); + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::changeEmail(const std::string &email) +{ + MessageOut msg(PAMSG_EMAIL_CHANGE); + + msg.writeString(email); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::getEmail() +{ + MessageOut msg(PAMSG_EMAIL_GET); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::changePassword( + const std::string &oldPassword, const std::string &newPassword) +{ + MessageOut msg(PAMSG_PASSWORD_CHANGE); + + msg.writeString(oldPassword); + msg.writeString(newPassword); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::enterWorld() +{ + MessageOut msg(PAMSG_ENTER_WORLD); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::Account::enterChat() +{ + MessageOut msg(PAMSG_ENTER_CHAT); + + Net::AccountServer::connection->send(msg); +} diff --git a/src/net/accountserver/account.h b/src/net/accountserver/account.h new file mode 100644 index 00000000..8e46eaa5 --- /dev/null +++ b/src/net/accountserver/account.h @@ -0,0 +1,60 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_ACCOUNTSERVER_CHARACTER_H +#define _TMW_NET_ACCOUNTSERVER_CHARACTER_H + +#include <iosfwd> + +namespace Net +{ + namespace AccountServer + { + namespace Account + { + void createCharacter(const std::string &name, + char hairColor, char hairStyle, char gender, + short strength, short agility, short vitality, + short intelligence, short dexterity, short luck); + + void deleteCharacter(char slot); + + void selectCharacter(char slot); + + void unregister(); + + void changeEmail(const std::string &email); + + void getEmail(); + + void changePassword(const std::string &oldPassowrd, + const std::string &newPassword); + + void enterWorld(); + + void enterChat(); + } + } +} + +#endif diff --git a/src/net/accountserver/accountserver.cpp b/src/net/accountserver/accountserver.cpp new file mode 100644 index 00000000..8fde6d5e --- /dev/null +++ b/src/net/accountserver/accountserver.cpp @@ -0,0 +1,68 @@ +/* + * The Mana World + * 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$ + */ + +#include "accountserver.h" + +#include "internal.h" + +#include "../connection.h" +#include "../messageout.h" +#include "../protocol.h" + +void Net::AccountServer::login(Net::Connection *connection, int version, + const std::string &username, const std::string &password) +{ + Net::AccountServer::connection = connection; + + MessageOut msg(PAMSG_LOGIN); + + msg.writeLong(version); + msg.writeString(username); + msg.writeString(password); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::registerAccount(Net::Connection *connection, + int version, const std::string &username, const std::string &password, + const std::string &email) +{ + Net::AccountServer::connection = connection; + + MessageOut msg(PAMSG_REGISTER); + + msg.writeLong(version); // client version + msg.writeString(username); + msg.writeString(password); + msg.writeString(email); + + Net::AccountServer::connection->send(msg); +} + +void Net::AccountServer::logout() +{ + MessageOut msg(PAMSG_LOGOUT); + Net::AccountServer::connection->send(msg); + + Net::AccountServer::connection = 0; +} diff --git a/src/net/accountserver/accountserver.h b/src/net/accountserver/accountserver.h new file mode 100644 index 00000000..c05b5317 --- /dev/null +++ b/src/net/accountserver/accountserver.h @@ -0,0 +1,46 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_ACCOUNTSERVER_ACCOUNTSERVER_H +#define _TMW_NET_ACCOUNTSERVER_ACCOUNTSERVER_H + +#include <iosfwd> + +namespace Net +{ + class Connection; + + namespace AccountServer + { + void login(Net::Connection *connection, int version, + const std::string &username, const std::string &password); + + void registerAccount(Net::Connection *connection, int version, + const std::string &username, const std::string &password, + const std::string &email); + + void logout(); + } +} + +#endif diff --git a/src/net/accountserver/internal.cpp b/src/net/accountserver/internal.cpp new file mode 100644 index 00000000..28a9695e --- /dev/null +++ b/src/net/accountserver/internal.cpp @@ -0,0 +1,34 @@ +/* + * The Mana World + * 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$ + */ + +#include "internal.h" + +namespace Net +{ + class Connection; + + namespace AccountServer + { + Connection *connection = 0; + } +} diff --git a/src/net/accountserver/internal.h b/src/net/accountserver/internal.h new file mode 100644 index 00000000..8af5ec04 --- /dev/null +++ b/src/net/accountserver/internal.h @@ -0,0 +1,37 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_ACCOUNTSERVER_INTERNAL_H +#define _TMW_NET_ACCOUNTSERVER_INTERNAL_H + +namespace Net +{ + class Connection; + + namespace AccountServer + { + extern Connection *connection; + } +} + +#endif diff --git a/src/net/charserverhandler.cpp b/src/net/charserverhandler.cpp index 4e251524..f715b434 100644 --- a/src/net/charserverhandler.cpp +++ b/src/net/charserverhandler.cpp @@ -23,9 +23,9 @@ #include "charserverhandler.h" -#include "messagein.h" -#include "network.h" +#include "connection.h" #include "protocol.h" +#include "messagein.h" #include "../game.h" #include "../localplayer.h" @@ -35,6 +35,9 @@ #include "../gui/ok_dialog.h" +extern Net::Connection *gameServerConnection; +extern Net::Connection *chatServerConnection; + CharServerHandler::CharServerHandler() { static const Uint16 _messages[] = { @@ -174,8 +177,8 @@ CharServerHandler::handleCharSelectResponse(MessageIn &msg) logger->log("Game server: %s:%d", gameServer.c_str(), gameServerPort); logger->log("Chat server: %s:%d", chatServer.c_str(), chatServerPort); - Network::connect(Network::GAME, gameServer, gameServerPort); - Network::connect(Network::CHAT, chatServer, chatServerPort); + gameServerConnection->connect(gameServer, gameServerPort); + chatServerConnection->connect(chatServer, chatServerPort); // Keep the selected character and delete the others player_node = mCharInfo->getEntry(); diff --git a/src/net/chatserver/chatserver.cpp b/src/net/chatserver/chatserver.cpp new file mode 100644 index 00000000..e6a3331d --- /dev/null +++ b/src/net/chatserver/chatserver.cpp @@ -0,0 +1,116 @@ +/* + * The Mana World + * 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$ + */ + +#include "chatserver.h" + +#include "internal.h" + +#include "../connection.h" +#include "../messageout.h" +#include "../protocol.h" + +using Net::ChatServer::connection; + +void Net::ChatServer::connect(Net::Connection *connection, + const std::string &token) +{ + Net::ChatServer::connection = connection; + + MessageOut msg(PCMSG_CONNECT); + + msg.writeString(token, 32); + + connection->send(msg); +} + +void Net::ChatServer::chat(short channel, const std::string &text) +{ + MessageOut msg(PCMSG_CHAT); + + msg.writeString(text); + msg.writeShort(channel); + + connection->send(msg); +} + +void Net::ChatServer::announce(const std::string &text) +{ + MessageOut msg(PCMSG_ANNOUNCE); + + msg.writeString(text); + + connection->send(msg); +} + +void Net::ChatServer::privMsg(const std::string &recipient, + const std::string &text) +{ + MessageOut msg(PCMSG_PRIVMSG); + + msg.writeString(recipient); + msg.writeString(text); + + connection->send(msg); +} + +void Net::ChatServer::registerChannel(const std::string &name, + const std::string &annoucement, const std::string &password, + char isPrivate) +{ + MessageOut msg(PCMSG_REGISTER_CHANNEL); + + msg.writeByte(isPrivate); + msg.writeString(name); + msg.writeString(annoucement); + msg.writeString(password); + + connection->send(msg); +} + +void Net::ChatServer::unregisterChannel(short channel) +{ + MessageOut msg(PCMSG_UNREGISTER_CHANNEL); + + msg.writeShort(channel); + + connection->send(msg); +} + +void Net::ChatServer::enterChannel(short channel, const std::string &password) +{ + MessageOut msg(PCMSG_ENTER_CHANNEL); + + msg.writeShort(channel); + msg.writeString(password); + + connection->send(msg); +} + +void Net::ChatServer::quitChannel(short channel) +{ + MessageOut msg(PCMSG_QUIT_CHANNEL); + + msg.writeShort(channel); + + connection->send(msg); +} diff --git a/src/net/chatserver/chatserver.h b/src/net/chatserver/chatserver.h new file mode 100644 index 00000000..93fe17c4 --- /dev/null +++ b/src/net/chatserver/chatserver.h @@ -0,0 +1,55 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_CHATSERVER_CHATSERVER_H +#define _TMW_NET_CHATSERVER_CHATSERVER_H + +#include <iosfwd> + +namespace Net +{ + class Connection; + + namespace ChatServer + { + void connect(Net::Connection *connection, const std::string &token); + + void chat(short channel, const std::string &text); + + void announce(const std::string &text); + + void privMsg(const std::string &recipient, const std::string &text); + + void registerChannel(const std::string &name, + const std::string &announcement, const std::string &password, + char isPrivate); + + void unregisterChannel(short channel); + + void enterChannel(short channel, const std::string &password); + + void quitChannel(short channel); + } +} + +#endif diff --git a/src/net/chatserver/internal.cpp b/src/net/chatserver/internal.cpp new file mode 100644 index 00000000..c1f7a3f7 --- /dev/null +++ b/src/net/chatserver/internal.cpp @@ -0,0 +1,34 @@ +/* + * The Mana World + * 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$ + */ + +#include "internal.h" + +namespace Net +{ + class Connection; + + namespace ChatServer + { + Connection *connection = 0; + } +} diff --git a/src/net/chatserver/internal.h b/src/net/chatserver/internal.h new file mode 100644 index 00000000..7579972b --- /dev/null +++ b/src/net/chatserver/internal.h @@ -0,0 +1,37 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_CHATSERVER_INTERNAL_H +#define _TMW_NET_CHATSERVER_INTERNAL_H + +namespace Net +{ + class Connection; + + namespace ChatServer + { + extern Connection *connection; + } +} + +#endif diff --git a/src/net/connection.cpp b/src/net/connection.cpp new file mode 100644 index 00000000..a17bc727 --- /dev/null +++ b/src/net/connection.cpp @@ -0,0 +1,104 @@ +/* + * The Mana World + * 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$ + */ + +#include "connection.h" + +#include <string> + +#include "internal.h" +#include "messageout.h" + +#include "../log.h" + +Net::Connection::Connection(ENetHost *client): + mConnection(0), mClient(client) +{ + Net::connections++; +} + +Net::Connection::~Connection() +{ + Net::connections--; +} + +bool Net::Connection::connect(const std::string &address, short port) +{ + logger->log("Net::Connection::connect(%s, %i)", address.c_str(), port); + + if (address.empty()) + { + logger->log("Net::Connection::connect() got empty address!"); + mState = ERROR; + return false; + } + + ENetAddress enetAddress; + + enet_address_set_host(&enetAddress, address.c_str()); + enetAddress.port = port; + + // Initiate the connection, allocating channel 0. + mConnection = enet_host_connect(mClient, &enetAddress, 1); + + if (!mConnection) + { + logger->log("Unable to initiate connection to the server."); + mState = ERROR; + return false; + } + + return true; +} + +void Net::Connection::disconnect() +{ + if (!mConnection) + return; + + enet_peer_disconnect(mConnection, 0); + enet_host_flush(mClient); + enet_peer_reset(mConnection); + + mConnection = 0; +} + +bool Net::Connection::isConnected() +{ + return mConnection && mConnection->state == ENET_PEER_STATE_CONNECTED; +} + +void Net::Connection::send(const MessageOut &msg) +{ + if (!isConnected()) + { + logger->log("Warning: cannot send message to not connected server!"); + return; + } + + logger->log("Sending message of size %d...", msg.getDataSize()); + + ENetPacket *packet = enet_packet_create(msg.getData(), + msg.getDataSize(), + ENET_PACKET_FLAG_RELIABLE); + enet_peer_send(mConnection, 0, packet); +} diff --git a/src/net/connection.h b/src/net/connection.h new file mode 100644 index 00000000..179367c6 --- /dev/null +++ b/src/net/connection.h @@ -0,0 +1,78 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_CONNECTION_H +#define _TMW_NET_CONNECTION_H + +#include <iosfwd> + +#include <enet/enet.h> + +class MessageOut; + +namespace Net +{ + class Connection + { + public: + enum State { + OK, ERROR + }; + + ~Connection(); + + /** + * Connects to the given server with the specified address and port. + * This method is non-blocking, use isConnected to check whether the + * server is connected. + */ + bool connect(const std::string &address, short port); + + /** + * Disconnects from the given server. + */ + void disconnect(); + + State getState() { return mState; } + + /** + * Returns whether the server is connected. + */ + bool isConnected(); + + /** + * Sends a message. + */ + void send(const MessageOut &msg); + + private: + friend Connection *Net::getConnection(); + Connection(ENetHost *client); + + ENetPeer *mConnection; + ENetHost *mClient; + State mState; + }; +} + +#endif diff --git a/src/net/gameserver/gameserver.cpp b/src/net/gameserver/gameserver.cpp new file mode 100644 index 00000000..04e5bb08 --- /dev/null +++ b/src/net/gameserver/gameserver.cpp @@ -0,0 +1,42 @@ +/* + * The Mana World + * 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$ + */ + +#include "gameserver.h" + +#include "internal.h" + +#include "../connection.h" +#include "../messageout.h" +#include "../protocol.h" + +void Net::GameServer::connect(Net::Connection *connection, + const std::string &token) +{ + Net::GameServer::connection = connection; + + MessageOut msg(PGMSG_CONNECT); + + msg.writeString(token, 32); + + Net::GameServer::connection->send(msg); +} diff --git a/src/net/gameserver/gameserver.h b/src/net/gameserver/gameserver.h new file mode 100644 index 00000000..ee49d7e3 --- /dev/null +++ b/src/net/gameserver/gameserver.h @@ -0,0 +1,39 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_GAMESERVER_GAMESERVER_H +#define _TMW_NET_GAMESERVER_GAMESERVER_H + +#include <iosfwd> + +namespace Net +{ + class Connection; + + namespace GameServer + { + void connect(Net::Connection *connection, const std::string &token); + } +} + +#endif diff --git a/src/net/gameserver/internal.cpp b/src/net/gameserver/internal.cpp new file mode 100644 index 00000000..328b4863 --- /dev/null +++ b/src/net/gameserver/internal.cpp @@ -0,0 +1,34 @@ +/* + * The Mana World + * 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$ + */ + +#include "internal.h" + +namespace Net +{ + class Connection; + + namespace GameServer + { + Connection *connection = 0; + } +} diff --git a/src/net/gameserver/internal.h b/src/net/gameserver/internal.h new file mode 100644 index 00000000..567e15d2 --- /dev/null +++ b/src/net/gameserver/internal.h @@ -0,0 +1,37 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_GAMESERVER_INTERNAL_H +#define _TMW_NET_GAMESERVER_INTERNAL_H + +namespace Net +{ + class Connection; + + namespace GameServer + { + extern Connection *connection; + } +} + +#endif diff --git a/src/net/gameserver/player.cpp b/src/net/gameserver/player.cpp new file mode 100644 index 00000000..1f27276a --- /dev/null +++ b/src/net/gameserver/player.cpp @@ -0,0 +1,68 @@ +/* + * The Mana World + * 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$ + */ + +#include "player.h" + +#include "internal.h" + +#include "../connection.h" +#include "../messageout.h" +#include "../protocol.h" + +void Net::GameServer::Player::say(const std::string &text) +{ + MessageOut msg(PGMSG_SAY); + + msg.writeString(text); + + Net::GameServer::connection->send(msg); +} + +void Net::GameServer::Player::walk(short x, short y) +{ + MessageOut msg(PGMSG_WALK); + + msg.writeShort(x); + msg.writeShort(y); + + Net::GameServer::connection->send(msg); +} + +void Net::GameServer::Player::useItem(int itemId) +{ + MessageOut msg(PGMSG_USE_ITEM); + + msg.writeLong(itemId); + + Net::GameServer::connection->send(msg); +} + +void Net::GameServer::Player::equip(int itemId, char slot) +{ + MessageOut msg(PGMSG_EQUIP); + + msg.writeLong(itemId); + msg.writeByte(slot); + + Net::GameServer::connection->send(msg); +} diff --git a/src/net/gameserver/player.h b/src/net/gameserver/player.h new file mode 100644 index 00000000..34d5bb45 --- /dev/null +++ b/src/net/gameserver/player.h @@ -0,0 +1,46 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_GAMESERVER_PLAYER_H +#define _TMW_NET_GAMESERVER_PLAYER_H + +#include <iosfwd> + +namespace Net +{ + class Connection; + + namespace GameServer + { + namespace Player + { + void say(const std::string &text); + void walk(short x, short y); +// void pickUp(...); + void useItem(int itemId); + void equip(int itemId, char slot); + } + } +} + +#endif diff --git a/src/net/internal.cpp b/src/net/internal.cpp new file mode 100644 index 00000000..358aa143 --- /dev/null +++ b/src/net/internal.cpp @@ -0,0 +1,29 @@ +/* + * The Mana World + * 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$ + */ + +#include "internal.h" + +namespace Net +{ + int connections = 0; +} diff --git a/src/net/internal.h b/src/net/internal.h new file mode 100644 index 00000000..e1ef648a --- /dev/null +++ b/src/net/internal.h @@ -0,0 +1,32 @@ +/* + * The Mana World + * 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$ + */ + +#ifndef _TMW_NET_INTERNAL_H +#define _TMW_NET_INTERNAL_H + +namespace Net +{ + extern int connections; +} + +#endif diff --git a/src/net/messagehandler.cpp b/src/net/messagehandler.cpp index 0b5cd87c..b6074690 100644 --- a/src/net/messagehandler.cpp +++ b/src/net/messagehandler.cpp @@ -29,5 +29,5 @@ MessageHandler::~MessageHandler() { - Network::unregisterHandler(this); + Net::unregisterHandler(this); } diff --git a/src/net/network.cpp b/src/net/network.cpp index e56f6df0..b94c9eb8 100644 --- a/src/net/network.cpp +++ b/src/net/network.cpp @@ -23,107 +23,70 @@ #include "network.h" +#include <enet/enet.h> + +#include <map> + +#include "connection.h" +#include "internal.h" #include "messagehandler.h" #include "messagein.h" -#include "messageout.h" #include "../log.h" -static Network::State mState; - /** - * The local host. + * The local host which is shared for all outgoing connections. */ -static ENetHost *mClient; - -/** - * An array holding the peers of the account, game and chat servers. - */ -static ENetPeer *mServers[3]; +namespace { + ENetHost *client; +} typedef std::map<unsigned short, MessageHandler*> MessageHandlers; typedef MessageHandlers::iterator MessageHandlerIterator; static MessageHandlers mMessageHandlers; -Network::State Network::getState() { return mState; } - -void Network::initialize() +void Net::initialize() { - // Initialize server peers - for (int i = 0; i < 3; ++i) - mServers[i] = NULL; - - mClient = enet_host_create(NULL, 3, 0, 0); - - if (!mClient) + if (enet_initialize()) { - logger->error( - "An error occurred while trying to create an ENet client."); - mState = NET_ERROR; + logger->error("Failed to initialize ENet."); } -} - -void Network::finalize() -{ - clearHandlers(); - disconnect(ACCOUNT); - disconnect(GAME); - disconnect(CHAT); -} - -bool -Network::connect(Server server, const std::string &address, short port) -{ - logger->log("Network::connect(%d, %s, %i)", server, address.c_str(), port); + client = enet_host_create(NULL, 3, 0, 0); - if (address.empty()) + if (!client) { - logger->log("Network::connect() got empty address!"); - mState = NET_ERROR; - return false; + logger->error("Failed to create the local host."); } +} - if (mServers[server] != NULL) - { - logger->log("Network::connect() already connected (or connecting) to " - "this server!"); - return false; - } - - ENetAddress enetAddress; - - enet_address_set_host(&enetAddress, address.c_str()); - enetAddress.port = port; - - // Initiate the connection, allocating channel 0. - mServers[server] = enet_host_connect(mClient, &enetAddress, 1); +void Net::finalize() +{ + if (!client) + return; // Wasn't initialized at all - if (mServers[server] == NULL) - { - logger->log("Unable to initiate connection to the server."); - mState = NET_ERROR; - return false; + if (Net::connections) { + logger->error("Tried to shutdown the network subsystem while there " + "are network connections left!"); } - return true; + clearHandlers(); + enet_deinitialize(); } -void -Network::disconnect(Server server) +Net::Connection *Net::getConnection() { - if (mServers[server]) + if (!client) { - enet_peer_disconnect(mServers[server], 0); - enet_host_flush(mClient); - enet_peer_reset(mServers[server]); - - mServers[server] = NULL; + logger->error("Tried to instantiate a network object before " + "initializing the network subsystem!"); } + + return new Net::Connection(client); } void -Network::registerHandler(MessageHandler *handler) +Net::registerHandler(MessageHandler *handler) { for (const Uint16 *i = handler->handledMessages; *i; i++) { @@ -132,7 +95,7 @@ Network::registerHandler(MessageHandler *handler) } void -Network::unregisterHandler(MessageHandler *handler) +Net::unregisterHandler(MessageHandler *handler) { for (const Uint16 *i = handler->handledMessages; *i; i++) { @@ -141,54 +104,46 @@ Network::unregisterHandler(MessageHandler *handler) } void -Network::clearHandlers() +Net::clearHandlers() { mMessageHandlers.clear(); } -bool -Network::isConnected(Server server) -{ - return mServers[server] != NULL && - mServers[server]->state == ENET_PEER_STATE_CONNECTED; -} /** * Dispatches a message to the appropriate message handler and * destroys it afterwards. */ -static void -dispatchMessage(ENetPacket *packet) +namespace { - MessageIn msg((const char *)packet->data, packet->dataLength); - - MessageHandlerIterator iter = mMessageHandlers.find(msg.getId()); - - if (iter != mMessageHandlers.end()) { - logger->log("Received packet %x (%i B)", - msg.getId(), msg.getLength()); - iter->second->handleMessage(msg); - } - else { - logger->log("Unhandled packet %x (%i B)", - msg.getId(), msg.getLength()); - } - - // Clean up the packet now that we're done using it. - enet_packet_destroy(packet); + void + dispatchMessage(ENetPacket *packet) + { + MessageIn msg((const char *)packet->data, packet->dataLength); + + MessageHandlerIterator iter = mMessageHandlers.find(msg.getId()); + + if (iter != mMessageHandlers.end()) { + logger->log("Received packet %x (%i B)", + msg.getId(), msg.getLength()); + iter->second->handleMessage(msg); + } + else { + logger->log("Unhandled packet %x (%i B)", + msg.getId(), msg.getLength()); + } + + // Clean up the packet now that we're done using it. + enet_packet_destroy(packet); + } } -void Network::flush() +void Net::flush() { - if (mState == NET_ERROR) - { - return; - } - ENetEvent event; // Wait up to 10 milliseconds for an event. - while (enet_host_service(mClient, &event, 10) > 0) + while (enet_host_service(client, &event, 10) > 0) { switch (event.type) { @@ -218,40 +173,3 @@ void Network::flush() } } } - -void Network::send(Server server, const MessageOut &msg) -{ - if (mState == NET_ERROR) - { - logger->log("Warning: attempt to send a message while network not " - "ready."); - return; - } - else if (!isConnected(server)) - { - logger->log("Warning: cannot send message to not connected server %d!", - server); - return; - } - - logger->log("Sending message of size %d to server %d...", - msg.getDataSize(), server); - - ENetPacket *packet = enet_packet_create(msg.getData(), - msg.getDataSize(), - ENET_PACKET_FLAG_RELIABLE); - enet_peer_send(mServers[server], 0, packet); -} - -char *iptostring(int address) -{ - static char asciiIP[16]; - - sprintf(asciiIP, "%i.%i.%i.%i", - (unsigned char)(address), - (unsigned char)(address >> 8), - (unsigned char)(address >> 16), - (unsigned char)(address >> 24)); - - return asciiIP; -} diff --git a/src/net/network.h b/src/net/network.h index 1e403c24..819115dd 100644 --- a/src/net/network.h +++ b/src/net/network.h @@ -21,98 +21,51 @@ * $Id$ */ -#ifndef _TMW_NETWORK_ -#define _TMW_NETWORK_ +#ifndef _TMW_NET_NETWORK_H +#define _TMW_NET_NETWORK_H -#include <map> -#include <string> - -#include <enet/enet.h> +#include <iosfwd> class MessageHandler; class MessageOut; -/** - * The client network layer. Facilitates connecting and communicating to the - * account, game and chat servers. Also routes incoming message to the - * appropriate message handlers. - */ -class Network +namespace Net { - public: - /** - * Sets up the local host. - */ - static void - initialize(); - - /** - * Closes the connections. - */ - static void - finalize(); - - enum Server { - ACCOUNT, - GAME, - CHAT - }; - - enum State { - NET_OK, - NET_ERROR - }; - - /** - * Connects to the given server with the specified address and port. - * This method is non-blocking, use isConnected to check whether the - * server is connected. - */ - static bool - connect(Server server, const std::string &address, short port); - - /** - * Disconnects from the given server. - */ - static void - disconnect(Server server); - - /** - * Registers a message handler. A message handler handles a certain - * subset of incoming messages. - */ - static void - registerHandler(MessageHandler *handler); - - /** - * Unregisters a message handler. - */ - static void - unregisterHandler(MessageHandler *handler); - - static void - clearHandlers(); - - static State - getState(); - - /** - * Returns whether the given server is connected. - */ - static bool - isConnected(Server server); - - static void - flush(); - - /** - * Sends a message to a given server. The server should be connected. - */ - static void - send(Server server, const MessageOut &msg); + class Connection; + + /** + * Initializes the network subsystem. + */ + void initialize(); + + /** + * Finalizes the network subsystem. + */ + void finalize(); + + Connection *getConnection(); + + /** + * Registers a message handler. A message handler handles a certain + * subset of incoming messages. + */ + void registerHandler(MessageHandler *handler); + + /** + * Unregisters a message handler. + */ + void unregisterHandler(MessageHandler *handler); + + /** + * Clears all registered message handlers. + */ + void clearHandlers(); + + /* + * Handles all events and dispatches incoming messages to the + * registered handlers + */ + void flush(); }; -/** Convert an address from int format to string */ -char *iptostring(int address); - #endif diff --git a/src/npc.cpp b/src/npc.cpp index b2b426dd..3bd4371b 100644 --- a/src/npc.cpp +++ b/src/npc.cpp @@ -25,9 +25,6 @@ #include "animatedsprite.h" -#include "net/messageout.h" -#include "net/protocol.h" - class Spriteset; extern Spriteset *npcset; @@ -48,25 +45,34 @@ NPC::getType() const void NPC::talk() { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_TALK); outMsg.writeLong(mId); outMsg.writeByte(0); current_npc = this; + */ } void NPC::nextDialog() { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_NEXT_REQUEST); outMsg.writeLong(mId); + */ } void NPC::dialogChoice(char choice) { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_LIST_CHOICE); outMsg.writeLong(mId); outMsg.writeByte(choice); + */ } /* @@ -76,15 +82,21 @@ NPC::dialogChoice(char choice) void NPC::buy() { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_BUY_SELL_REQUEST); outMsg.writeLong(mId); outMsg.writeByte(0); + */ } void NPC::sell() { + // XXX Convert for new server + /* MessageOut outMsg(CMSG_NPC_BUY_SELL_REQUEST); outMsg.writeLong(mId); outMsg.writeByte(1); + */ } |