summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-09-16 09:58:58 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-09-16 09:58:58 +0000
commitbe461a5ca51e10dad40c87385440f6ddae14fca0 (patch)
tree030abf2b9931e92c35fde9b6c3c876c9763a734c
parente7853c292476b6d2e76c00d77638ae155d097493 (diff)
downloadmanaserv-be461a5ca51e10dad40c87385440f6ddae14fca0.tar.gz
manaserv-be461a5ca51e10dad40c87385440f6ddae14fca0.tar.bz2
manaserv-be461a5ca51e10dad40c87385440f6ddae14fca0.tar.xz
manaserv-be461a5ca51e10dad40c87385440f6ddae14fca0.zip
Cleaned game-server handler a bit before adding statistics.
-rw-r--r--ChangeLog14
-rw-r--r--src/account-server/accounthandler.cpp2
-rw-r--r--src/account-server/serverhandler.cpp106
-rw-r--r--src/account-server/serverhandler.hpp31
-rw-r--r--src/chat-server/chathandler.hpp1
-rw-r--r--src/defines.h1
-rw-r--r--src/game-server/command.cpp2
7 files changed, 94 insertions, 63 deletions
diff --git a/ChangeLog b/ChangeLog
index 24ef21e4..c2b0daa2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,10 +1,20 @@
+2007-09-16 Guillaume Melquiond <guillaume.melquiond@gmail.com>
+
+ * src/game-server/command.cpp: Removed unused argument name.
+ * src/defines.h: Added packet from for sending server statistics.
+ * src/account-server/serverhandler.hpp,
+ src/account-server/accounthandler.cpp: Cleaned function prototypes.
+ * src/chat-server/chathandler.hpp: Fixed missing header.
+ * src/account-server/serverhandler.cpp: Moved map/server data into
+ connection local storage.
+
2007-09-10 Eugenio Favalli <elvenprogrammer@gmail.com>
* accountserver.cbp, gameserver.cbp, src/game-server/spawnarea.cpp:
Dimensionless spawn points now spawn over the whole map.
* src/game-server/mapreader.cpp, src/game-server/spawnarea.cpp,
- src/game-server/spawnarea.hpp: Spawn rate and limit are now read from map
- files and really fixed size of spawn area.
+ src/game-server/spawnarea.hpp: Spawn rate and limit are now read from
+ map files and really fixed size of spawn area.
* docs/commands.txt: Added first draft of admin/gm commands reference.
2007-08-31 Guillaume Melquiond <guillaume.melquiond@gmail.com>
diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp
index e7fc2ceb..9eb6264e 100644
--- a/src/account-server/accounthandler.cpp
+++ b/src/account-server/accounthandler.cpp
@@ -640,7 +640,7 @@ handleCharacterSelectMessage(AccountClient &computer, MessageIn &msg)
Character *selectedChar = chars[charNum];
std::string address;
- short port;
+ int port;
if (!serverHandler->getGameServerFromMap
(selectedChar->getMapId(), address, port))
{
diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp
index 06c9f805..87f99c81 100644
--- a/src/account-server/serverhandler.cpp
+++ b/src/account-server/serverhandler.cpp
@@ -27,6 +27,7 @@
#include "account-server/serverhandler.hpp"
#include "account-server/accountclient.hpp"
+#include "account-server/accounthandler.hpp"
#include "account-server/character.hpp"
#include "account-server/dalstorage.hpp"
#include "net/messagein.hpp"
@@ -37,6 +38,25 @@
#include "utils/tokendispenser.hpp"
#include "utils/tokencollector.hpp"
+struct MapStatistics
+{
+ std::vector< int > players;
+ unsigned short nbThings;
+ unsigned short nbMonsters;
+};
+
+typedef std::map< unsigned short, MapStatistics > ServerStatistics;
+
+struct GameServer: NetComputer
+{
+ GameServer(ENetPeer *peer): NetComputer(peer), port(0) {}
+
+ std::string address;
+ NetComputer *server;
+ ServerStatistics maps;
+ short port;
+};
+
bool ServerHandler::startListen(enet_uint16 port)
{
LOG_INFO("Game server handler started:");
@@ -45,40 +65,42 @@ bool ServerHandler::startListen(enet_uint16 port)
NetComputer *ServerHandler::computerConnected(ENetPeer *peer)
{
- return new NetComputer(peer);
+ return new GameServer(peer);
}
void ServerHandler::computerDisconnected(NetComputer *comp)
{
- Servers::iterator i = servers.begin();
- while (i != servers.end())
+ delete comp;
+}
+
+GameServer *ServerHandler::getGameServerFromMap(int mapId) const
+{
+ for (NetComputers::const_iterator i = clients.begin(),
+ i_end = clients.end(); i != i_end; ++i)
{
- if (i->second.server == comp)
- {
- LOG_INFO("Unregistering map " << i->first << '.');
- servers.erase(i++);
- }
- else
- {
- ++i;
- }
+ GameServer *server = static_cast< GameServer * >(*i);
+ ServerStatistics::const_iterator i = server->maps.find(mapId);
+ if (i == server->maps.end()) continue;
+ return server;
}
- delete comp;
+ return NULL;
}
-bool ServerHandler::getGameServerFromMap(unsigned mapId, std::string &address,
- short &port)
+bool ServerHandler::getGameServerFromMap(int mapId, std::string &address,
+ int &port) const
{
- Servers::const_iterator i = servers.find(mapId);
- if (i == servers.end()) return false;
- address = i->second.address;
- port = i->second.port;
- return true;
+ if (GameServer *s = getGameServerFromMap(mapId))
+ {
+ address = s->address;
+ port = s->port;
+ return true;
+ }
+ return false;
}
void ServerHandler::registerGameClient(std::string const &token, Character *ptr)
{
- unsigned mapId = ptr->getMapId();
+ int mapId = ptr->getMapId();
MessageOut msg(AGMSG_PLAYER_ENTER);
msg.writeString(token, MAGIC_TOKEN_LENGTH);
@@ -86,14 +108,15 @@ void ServerHandler::registerGameClient(std::string const &token, Character *ptr)
msg.writeString(ptr->getName());
serializeCharacterData(*ptr, msg);
- Servers::const_iterator i = servers.find(mapId);
- assert(i != servers.end());
- i->second.server->send(msg);
+ GameServer *s = getGameServerFromMap(mapId);
+ assert(s);
+ s->send(msg);
}
void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
{
MessageOut result;
+ GameServer *server = static_cast< GameServer * >(comp);
switch (msg.getId())
{
@@ -101,10 +124,9 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
{
LOG_DEBUG("GAMSG_REGISTER");
// TODO: check the credentials of the game server
- std::string address = msg.readString();
- int port = msg.readShort();
- Server s = { address, port, comp };
- LOG_INFO("Game server " << address << ':' << port
+ server->address = msg.readString();
+ server->port = msg.readShort();
+ LOG_INFO("Game server " << server->address << ':' << server->port
<< " wants to register " << (msg.getUnreadLength() / 2)
<< " maps.");
@@ -112,15 +134,19 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
{
int id = msg.readShort();
LOG_INFO("Registering map " << id << '.');
- if (servers.insert(std::make_pair(id, s)).second)
+ if (GameServer *s = getGameServerFromMap(id))
{
- MessageOut outMsg(AGMSG_ACTIVE_MAP);
- outMsg.writeShort(id);
- comp->send(outMsg);
+ LOG_ERROR("Server Handler: map is already registered by "
+ << s->address << ':' << s->port << '.');
}
else
{
- LOG_ERROR("Server Handler: map is already registered.");
+ MessageOut outMsg(AGMSG_ACTIVE_MAP);
+ outMsg.writeShort(id);
+ comp->send(outMsg);
+ MapStatistics &m = server->maps[id];
+ m.nbThings = 0;
+ m.nbMonsters = 0;
}
}
} break;
@@ -153,22 +179,20 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg)
std::string magic_token(utils::getMagicToken());
if (Character *ptr = storage->getCharacter(id, NULL))
{
- std::string address;
- short port;
- if (serverHandler->getGameServerFromMap
- (ptr->getMapId(), address, port))
+ int mapId = ptr->getMapId();
+ if (GameServer *s = getGameServerFromMap(mapId))
{
registerGameClient(magic_token, ptr);
result.writeShort(AGMSG_REDIRECT_RESPONSE);
- result.writeLong(ptr->getDatabaseID());
+ result.writeLong(id);
result.writeString(magic_token, MAGIC_TOKEN_LENGTH);
- result.writeString(address);
- result.writeShort(port);
+ result.writeString(s->address);
+ result.writeShort(s->port);
}
else
{
LOG_ERROR("Server Change: No game server for map " <<
- ptr->getMapId() << ".");
+ mapId << '.');
}
delete ptr;
}
diff --git a/src/account-server/serverhandler.hpp b/src/account-server/serverhandler.hpp
index ec537dbd..febc663e 100644
--- a/src/account-server/serverhandler.hpp
+++ b/src/account-server/serverhandler.hpp
@@ -24,12 +24,13 @@
#ifndef _TMWSERV_SERVERHANDLER_H_
#define _TMWSERV_SERVERHANDLER_H_
-#include <map>
-#include "account-server/accounthandler.hpp"
-#include "account-server/character.hpp"
+#include <string>
+
#include "net/connectionhandler.hpp"
class AccountClient;
+class Character;
+struct GameServer;
/**
* Manages communications with all the game servers. This class also keeps
@@ -47,7 +48,7 @@ class ServerHandler: public ConnectionHandler
* Returns the information a client needs to connect to the game server
* corresponding to the given map ID.
*/
- bool getGameServerFromMap(unsigned, std::string &address, short &port);
+ bool getGameServerFromMap(int, std::string &address, int &port) const;
/**
* Sends a magic token and character data to the relevant game server.
@@ -87,24 +88,18 @@ class ServerHandler: public ConnectionHandler
private:
/**
- * Send invite to user
+ * Returns the information a client needs to connect to the game server
+ * corresponding to the given map ID.
*/
- void sendInvite(const std::string &invitedName, const std::string &inviterName,
- const std::string &guildName);
-
- struct Server
- {
- std::string address;
- short port;
- NetComputer *server;
- };
-
- typedef std::map< unsigned, Server > Servers;
+ GameServer *getGameServerFromMap(int) const;
+#if 0
/**
- * Maps map IDs to game server data.
+ * Send invite to user
*/
- Servers servers;
+ void sendInvite(const std::string &invitedName, const std::string &inviterName,
+ const std::string &guildName);
+#endif
};
extern ServerHandler *serverHandler;
diff --git a/src/chat-server/chathandler.hpp b/src/chat-server/chathandler.hpp
index 301ea303..8f460e9d 100644
--- a/src/chat-server/chathandler.hpp
+++ b/src/chat-server/chathandler.hpp
@@ -27,6 +27,7 @@
#include <iosfwd>
#include "net/connectionhandler.hpp"
+#include "utils/tokencollector.hpp"
class ChatClient;
diff --git a/src/defines.h b/src/defines.h
index 507265d6..d4f44b06 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -239,6 +239,7 @@ enum {
GAMSG_GET_QUEST = 0x0541, // L id, S name
AGMSG_GET_QUEST_RESPONSE = 0x0542, // L id, S name, S value
GAMSG_BAN_PLAYER = 0x550, // L id, W duration
+ GAMSG_STATISTICS = 0x560, // { W map id, W thing nb, W monster nb, W player nb, { L character id }* }*
#if 0
GAMSG_GUILD_CREATE = 0x0550, // S name
diff --git a/src/game-server/command.cpp b/src/game-server/command.cpp
index 57751621..43bfe703 100644
--- a/src/game-server/command.cpp
+++ b/src/game-server/command.cpp
@@ -234,7 +234,7 @@ static void recall(Character *from, Character *ch)
GameState::enqueueEvent(ch, e);
}
-static void reload(Character *from, std::string const &db)
+static void reload(Character *, std::string const &db)
{
if (db == "items")
{