summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-09-23 15:01:47 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-09-23 15:01:47 +0000
commit239c22a35258051e62c27173dfb513f689b03e77 (patch)
tree19cba4d5eec590056cea9358ea0ae51a82840898
parent0d00b4e7f49a0373d350353e62e08cdd903628e9 (diff)
downloadmanaserv-239c22a35258051e62c27173dfb513f689b03e77.tar.gz
manaserv-239c22a35258051e62c27173dfb513f689b03e77.tar.bz2
manaserv-239c22a35258051e62c27173dfb513f689b03e77.tar.xz
manaserv-239c22a35258051e62c27173dfb513f689b03e77.zip
Simplified interface of the handler used for connections from game servers to account server.
-rw-r--r--ChangeLog11
-rw-r--r--src/account-server/accounthandler.cpp4
-rw-r--r--src/account-server/main-account.cpp15
-rw-r--r--src/account-server/serverhandler.cpp86
-rw-r--r--src/account-server/serverhandler.hpp111
5 files changed, 113 insertions, 114 deletions
diff --git a/ChangeLog b/ChangeLog
index 082cb570..b30666ca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -15,12 +15,13 @@
src/game-server/command.cpp, src/game-server/gamehandler.cpp: Taken
insertion failures into account.
* src/account-server/accounthandler.hpp,
- src/account-server/accounthandler.cpp: Simplified interface of
- connection handler. Moved all the implementation details outside the
+ src/account-server/accounthandler.cpp,
+ src/account-server/serverhandler.cpp,
+ src/account-server/serverhandler.hpp: Simplified interface of
+ connection handlers. Moved all the implementation details outside the
header file.
- * src/account-server/serverhandler.cpp,
- src/account-server/main-account.cpp: Updated to new interface of
- connection handler.
+ * src/account-server/main-account.cpp: Updated to new interface of
+ connection handlers.
2007-09-22 Guillaume Melquiond <guillaume.melquiond@gmail.com>
diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp
index 1e46d111..3d2161e4 100644
--- a/src/account-server/accounthandler.cpp
+++ b/src/account-server/accounthandler.cpp
@@ -612,7 +612,7 @@ static void handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg
std::string address;
int port;
- if (!serverHandler->getGameServerFromMap
+ if (!GameServerHandler::getGameServerFromMap
(selectedChar->getMapId(), address, port))
{
LOG_ERROR("Character Selection: No game server for the map.");
@@ -636,7 +636,7 @@ static void handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg
reply.writeShort(Configuration::getValue("accountServerPort",
DEFAULT_SERVER_PORT) + 2);
- serverHandler->registerGameClient(magic_token, selectedChar);
+ GameServerHandler::registerClient(magic_token, selectedChar);
registerChatClient(magic_token, selectedChar->getName(), AL_NORMAL);
computer.send(reply);
diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp
index 30eec4db..e0d11c28 100644
--- a/src/account-server/main-account.cpp
+++ b/src/account-server/main-account.cpp
@@ -62,9 +62,6 @@ DALStorage *storage;
/** Communications (chat) message handler */
ChatHandler *chatHandler;
-/** Server message handler */
-ServerHandler *serverHandler;
-
/** Chat Channels Manager */
ChatChannelManager *chatChannelManager;
@@ -162,7 +159,6 @@ static void initialize()
// FIXME: Make the global handlers global vars or part of a bigger
// singleton or a local variable in the event-loop
chatHandler = new ChatHandler;
- serverHandler = new ServerHandler;
// --- Initialize enet.
if (enet_initialize() != 0) {
@@ -187,13 +183,13 @@ static void deinitialize()
// Write configuration file
Configuration::deinitialize();
+ // Destroy message handlers.
AccountClientHandler::deinitialize();
+ GameServerHandler::deinitialize();
// Quit ENet
enet_deinitialize();
- // Destroy message handlers
- delete serverHandler;
delete chatHandler;
// Destroy Managers
@@ -229,7 +225,7 @@ static void dumpStatistics()
std::ofstream os(path.c_str());
os << "<statistics>\n";
- serverHandler->dumpStatistics(os);
+ GameServerHandler::dumpStatistics(os);
os << "</statistics>\n";
}
@@ -307,7 +303,7 @@ int main(int argc, char *argv[])
int port = Configuration::getValue("accountServerPort", DEFAULT_SERVER_PORT);
if (!AccountClientHandler::initialize(port) ||
- !serverHandler->startListen(port + 1) ||
+ !GameServerHandler::initialize(port + 1) ||
!chatHandler->startListen(port + 2))
{
LOG_FATAL("Unable to create an ENet server host.");
@@ -319,13 +315,12 @@ int main(int argc, char *argv[])
while (running) {
AccountClientHandler::process();
+ GameServerHandler::process();
chatHandler->process(50);
- serverHandler->process(50);
if (statTimer.poll()) dumpStatistics();
}
LOG_INFO("Received: Quit signal, closing down...");
- serverHandler->stopListen();
chatHandler->stopListen();
deinitialize();
}
diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp
index d5d9d5a9..f7c32245 100644
--- a/src/account-server/serverhandler.cpp
+++ b/src/account-server/serverhandler.cpp
@@ -30,13 +30,13 @@
#include "account-server/accounthandler.hpp"
#include "account-server/character.hpp"
#include "account-server/dalstorage.hpp"
+#include "net/connectionhandler.hpp"
#include "net/messagein.hpp"
#include "net/messageout.hpp"
#include "net/netcomputer.hpp"
#include "serialize/characterdata.hpp"
#include "utils/logger.h"
#include "utils/tokendispenser.hpp"
-#include "utils/tokencollector.hpp"
struct MapStatistics
{
@@ -47,6 +47,9 @@ struct MapStatistics
typedef std::map< unsigned short, MapStatistics > ServerStatistics;
+/**
+ * Stores address, maps, and statistics, of a connected game server.
+ */
struct GameServer: NetComputer
{
GameServer(ENetPeer *peer): NetComputer(peer), port(0) {}
@@ -57,10 +60,52 @@ struct GameServer: NetComputer
short port;
};
-bool ServerHandler::startListen(enet_uint16 port)
+static GameServer *getGameServerFromMap(int);
+
+/**
+ * Manages communications with all the game servers.
+ */
+class ServerHandler: public ConnectionHandler
{
+ friend GameServer *getGameServerFromMap(int);
+ friend void GameServerHandler::dumpStatistics(std::ostream &);
+
+ protected:
+ /**
+ * Processes server messages.
+ */
+ void processMessage(NetComputer *computer, MessageIn &message);
+
+ /**
+ * Called when a game server connects. Initializes a simple NetComputer
+ * as these connections are stateless.
+ */
+ NetComputer *computerConnected(ENetPeer *peer);
+
+ /**
+ * Called when a game server disconnects.
+ */
+ void computerDisconnected(NetComputer *comp);
+};
+
+static ServerHandler *serverHandler;
+
+bool GameServerHandler::initialize(int port)
+{
+ serverHandler = new ServerHandler;
LOG_INFO("Game server handler started:");
- return ConnectionHandler::startListen(port);
+ return serverHandler->startListen(port);
+}
+
+void GameServerHandler::deinitialize()
+{
+ serverHandler->stopListen();
+ delete serverHandler;
+}
+
+void GameServerHandler::process()
+{
+ serverHandler->process(50);
}
NetComputer *ServerHandler::computerConnected(ENetPeer *peer)
@@ -73,10 +118,11 @@ void ServerHandler::computerDisconnected(NetComputer *comp)
delete comp;
}
-GameServer *ServerHandler::getGameServerFromMap(int mapId) const
+static GameServer *getGameServerFromMap(int mapId)
{
- for (NetComputers::const_iterator i = clients.begin(),
- i_end = clients.end(); i != i_end; ++i)
+ for (ServerHandler::NetComputers::const_iterator
+ i = serverHandler->clients.begin(),
+ i_end = serverHandler->clients.end(); i != i_end; ++i)
{
GameServer *server = static_cast< GameServer * >(*i);
ServerStatistics::const_iterator i = server->maps.find(mapId);
@@ -86,10 +132,10 @@ GameServer *ServerHandler::getGameServerFromMap(int mapId) const
return NULL;
}
-bool ServerHandler::getGameServerFromMap(int mapId, std::string &address,
- int &port) const
+bool GameServerHandler::getGameServerFromMap
+ (int mapId, std::string &address, int &port)
{
- if (GameServer *s = getGameServerFromMap(mapId))
+ if (GameServer *s = ::getGameServerFromMap(mapId))
{
address = s->address;
port = s->port;
@@ -98,19 +144,22 @@ bool ServerHandler::getGameServerFromMap(int mapId, std::string &address,
return false;
}
-void ServerHandler::registerGameClient(std::string const &token, Character *ptr)
+static void registerGameClient
+ (GameServer *s, std::string const &token, Character *ptr)
{
- int mapId = ptr->getMapId();
-
MessageOut msg(AGMSG_PLAYER_ENTER);
msg.writeString(token, MAGIC_TOKEN_LENGTH);
msg.writeLong(ptr->getDatabaseID());
msg.writeString(ptr->getName());
serializeCharacterData(*ptr, msg);
+ s->send(msg);
+}
- GameServer *s = getGameServerFromMap(mapId);
+void GameServerHandler::registerClient(std::string const &token, Character *ptr)
+{
+ GameServer *s = ::getGameServerFromMap(ptr->getMapId());
assert(s);
- s->send(msg);
+ registerGameClient(s, token, ptr);
}
void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
@@ -182,7 +231,7 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
int mapId = ptr->getMapId();
if (GameServer *s = getGameServerFromMap(mapId))
{
- registerGameClient(magic_token, ptr);
+ registerGameClient(s, magic_token, ptr);
result.writeShort(AGMSG_REDIRECT_RESPONSE);
result.writeLong(id);
result.writeString(magic_token, MAGIC_TOKEN_LENGTH);
@@ -449,10 +498,11 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
comp->send(result);
}
-void ServerHandler::dumpStatistics(std::ostream &os) const
+void GameServerHandler::dumpStatistics(std::ostream &os)
{
- for (NetComputers::const_iterator i = clients.begin(),
- i_end = clients.end(); i != i_end; ++i)
+ for (ServerHandler::NetComputers::const_iterator
+ i = serverHandler->clients.begin(),
+ i_end = serverHandler->clients.end(); i != i_end; ++i)
{
GameServer *server = static_cast< GameServer * >(*i);
if (!server->port) continue;
diff --git a/src/account-server/serverhandler.hpp b/src/account-server/serverhandler.hpp
index 76151569..e5309cff 100644
--- a/src/account-server/serverhandler.hpp
+++ b/src/account-server/serverhandler.hpp
@@ -27,87 +27,40 @@
#include <iosfwd>
#include <string>
-#include "net/connectionhandler.hpp"
-
-class AccountClient;
class Character;
-struct GameServer;
-/**
- * Manages communications with all the game servers. This class also keeps
- * track of the maps each game server supports.
- */
-class ServerHandler: public ConnectionHandler
+namespace GameServerHandler
{
- public:
- /**
- * Starts the handler on the given port.
- */
- bool startListen(enet_uint16 port);
-
- /**
- * Returns the information a client needs to connect to the game server
- * corresponding to the given map ID.
- */
- bool getGameServerFromMap(int, std::string &address, int &port) const;
-
- /**
- * Sends a magic token and character data to the relevant game server.
- */
- void registerGameClient(std::string const &, Character *);
-
-// There is no rationale for having a character name, but not its ID.
-#if 0
- /**
- * Get character (temp used by chat server).
- */
- CharacterPtr getCharacter(const std::string &name);
-#endif
-
- /**
- * Make client join the specified guild channel
- */
- void enterChannel(const std::string &guildName, Character *player);
-
- /**
- * Dumps per-server statistics into given stream
- */
- void dumpStatistics(std::ostream &) const;
-
- protected:
- /**
- * Processes server messages.
- */
- void processMessage(NetComputer *computer, MessageIn &message);
-
- /**
- * Called when a game server connects. Initializes a simple NetComputer
- * as these connections are stateless.
- */
- NetComputer *computerConnected(ENetPeer *peer);
-
- /**
- * Called when a game server disconnects.
- */
- void computerDisconnected(NetComputer *comp);
-
- private:
-
- /**
- * Returns the information a client needs to connect to the game server
- * corresponding to the given map ID.
- */
- GameServer *getGameServerFromMap(int) const;
-
-#if 0
- /**
- * Send invite to user
- */
- void sendInvite(const std::string &invitedName, const std::string &inviterName,
- const std::string &guildName);
-#endif
-};
-
-extern ServerHandler *serverHandler;
+ /**
+ * Creates a connection handler and starts listening on given port.
+ */
+ bool initialize(int port);
+
+ /**
+ * Stops listening to messages and destroys the connection handler.
+ */
+ void deinitialize();
+
+ /**
+ * Returns the information a client needs to connect to the game server
+ * corresponding to the given map ID.
+ */
+ bool getGameServerFromMap(int, std::string &address, int &port);
+
+ /**
+ * Warns a game server about a soon-to-connect client.
+ */
+ void registerClient(std::string const &token, Character *);
+
+ /**
+ * Dumps per-server statistics into given stream
+ */
+ void dumpStatistics(std::ostream &);
+
+ /**
+ * Processes messages received by the connection handler.
+ */
+ void process();
+}
#endif