From ff9438ab94d19f0957a264ff79def01f9855b707 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Sun, 26 Oct 2008 20:32:38 +0000 Subject: Some cleanup and an unsuccesful attempt at fixing a crash on map server exit when there is no connection the account server (a different one surfaced). --- src/account-server/dalstorage.cpp | 8 +++++-- src/game-server/accountconnection.cpp | 26 +++++++++++++++++------ src/game-server/main-game.cpp | 5 ++--- src/net/connection.cpp | 39 +++++++++++++++++++++++------------ src/net/connection.hpp | 5 +++-- 5 files changed, 57 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 60a8967f..022c74db 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -1300,8 +1300,12 @@ void DALStorage::storePost(Letter *letter) Letter* DALStorage::getStoredPost(int playerId) { - Character *sender; - Character *receiver; + // TODO: Implement post retrieval + /* + Character *sender = 0; + Character *receiver = 0; Letter *letter = new Letter(0, sender, receiver); return letter; + */ + return 0; } diff --git a/src/game-server/accountconnection.cpp b/src/game-server/accountconnection.cpp index 94371c35..159bcb24 100644 --- a/src/game-server/accountconnection.cpp +++ b/src/game-server/accountconnection.cpp @@ -42,22 +42,36 @@ bool AccountConnection::start() { - if (!Connection::start( - Configuration::getValue("accountServerAddress", "localhost"), - Configuration::getValue("accountServerPort", DEFAULT_SERVER_PORT) + 1)) + const std::string accountServerAddress = + Configuration::getValue("accountServerAddress", "localhost"); + const int accountServerPort = + Configuration::getValue("accountServerPort", DEFAULT_SERVER_PORT) + 1; + + if (!Connection::start(accountServerAddress, accountServerPort)) { + LOG_INFO("Unable to create a connection to an account server."); return false; } + LOG_INFO("Connection established to the account server."); + + const std::string gameServerAddress = + Configuration::getValue("gameServerAddress", "localhost"); + const int gameServerPort = + Configuration::getValue("gameServerPort", DEFAULT_SERVER_PORT + 3); + + // Register with the account server and send the list of maps we handle MessageOut msg(GAMSG_REGISTER); - msg.writeString(Configuration::getValue("gameServerAddress", "localhost")); - msg.writeShort(Configuration::getValue("gameServerPort", DEFAULT_SERVER_PORT + 3)); + msg.writeString(gameServerAddress); + msg.writeShort(gameServerPort); MapManager::Maps const &m = MapManager::getMaps(); - for (MapManager::Maps::const_iterator i = m.begin(), i_end = m.end(); i != i_end; ++i) + for (MapManager::Maps::const_iterator i = m.begin(), i_end = m.end(); + i != i_end; ++i) { msg.writeShort(i->first); } send(msg); + return true; } diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp index c7105cc6..2a009a55 100644 --- a/src/game-server/main-game.cpp +++ b/src/game-server/main-game.cpp @@ -275,9 +275,8 @@ int main(int argc, char *argv[]) // General initialization initialize(); - if (!accountHandler->start()) { - LOG_INFO("Unable to create a connection to an account server."); - } + // Make an initial attempt to connect to the account server + accountHandler->start(); int gameServerPort = Configuration::getValue("gameServerPort", DEFAULT_SERVER_PORT + 3); diff --git a/src/net/connection.cpp b/src/net/connection.cpp index faff533c..38ecd1bf 100644 --- a/src/net/connection.cpp +++ b/src/net/connection.cpp @@ -26,16 +26,22 @@ #include "net/messageout.hpp" #include "utils/logger.h" +Connection::Connection(): + mRemote(0), + mLocal(0) +{ +} + bool Connection::start(std::string const &address, int port) { ENetAddress enetAddress; enet_address_set_host(&enetAddress, address.c_str()); enetAddress.port = port; - mLocal = enet_host_create(NULL /* create a client host */, - 1 /* allow one outgoing connection */, - 0 /* assume any amount of incoming bandwidth */, - 0 /* assume any amount of outgoing bandwidth */); + mLocal = enet_host_create(NULL /* create a client host */, + 1 /* allow one outgoing connection */, + 0 /* assume any amount of incoming bandwidth */, + 0 /* assume any amount of outgoing bandwidth */); if (!mLocal) return false; @@ -54,11 +60,17 @@ bool Connection::start(std::string const &address, int port) void Connection::stop() { - enet_peer_disconnect(mRemote, 0); - enet_host_flush(mLocal); - enet_peer_reset(mRemote); - enet_host_destroy(mLocal); - mRemote = NULL; + if (mRemote) + enet_peer_disconnect(mRemote, 0); + if (mLocal) + enet_host_flush(mLocal); + if (mRemote) + enet_peer_reset(mRemote); + if (mLocal) + enet_host_destroy(mLocal); + + mRemote = 0; + mLocal = 0; } bool Connection::isConnected() const @@ -68,19 +80,20 @@ bool Connection::isConnected() const void Connection::send(MessageOut const &msg, bool reliable, unsigned channel) { + if (!mRemote) { + LOG_WARN("Can't send message to unconnected host! (" << msg << ")"); + return; + } + ENetPacket *packet; packet = enet_packet_create(msg.getData(), msg.getLength(), reliable ? ENET_PACKET_FLAG_RELIABLE : 0); if (packet) - { enet_peer_send(mRemote, channel, packet); - } else - { LOG_ERROR("Failure to create packet!"); - } } void Connection::process() diff --git a/src/net/connection.hpp b/src/net/connection.hpp index 0cdb1fc0..ba36b2d3 100644 --- a/src/net/connection.hpp +++ b/src/net/connection.hpp @@ -37,7 +37,7 @@ class MessageOut; class Connection { public: - Connection(): mRemote(NULL) {} + Connection(); virtual ~Connection() {} /** @@ -59,7 +59,8 @@ class Connection /** * Sends a message to the remote host. */ - void send(MessageOut const &msg, bool reliable = true, unsigned channel = 0); + void send(MessageOut const &msg, bool reliable = true, + unsigned channel = 0); /** * Dispatches received messages to processMessage. -- cgit v1.2.3-70-g09d2