diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game-server/gamehandler.cpp | 27 | ||||
-rw-r--r-- | src/game-server/gamehandler.hpp | 13 | ||||
-rw-r--r-- | src/game-server/main-game.cpp | 14 | ||||
-rw-r--r-- | src/net/bandwidth.cpp | 53 | ||||
-rw-r--r-- | src/net/bandwidth.hpp | 28 | ||||
-rw-r--r-- | src/net/connection.cpp | 17 | ||||
-rw-r--r-- | src/net/connection.hpp | 11 | ||||
-rw-r--r-- | src/net/connectionhandler.cpp | 3 | ||||
-rw-r--r-- | src/net/netcomputer.cpp | 27 | ||||
-rw-r--r-- | src/net/netcomputer.hpp | 23 |
10 files changed, 81 insertions, 135 deletions
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index fe5b7df1..7886ac18 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -45,9 +45,7 @@ const unsigned int TILES_TO_BE_NEAR = 7; GameHandler::GameHandler(): - mTokenCollector(this), - mTotalBandwidthOut(0), - mTotalBandwidthIn(0) + mTokenCollector(this) { } @@ -610,26 +608,3 @@ void GameHandler::handleSendPost(GameClient *client, MessageIn &message) postMan->addCharacter(client->character); accountHandler->sendPost(client->character, message); } - -int GameHandler::calculateTotalOut() -{ - for (NetComputers::const_iterator i = clients.begin(), - i_end = clients.end(); i != i_end; ++i) - { - mTotalBandwidthOut += (*i)->totalOut(); - } - - return mTotalBandwidthOut; -} - -int GameHandler::calculateTotalIn() -{ - for (NetComputers::const_iterator i = clients.begin(), - i_end = clients.end(); i != i_end; ++i) - { - mTotalBandwidthIn += (*i)->totalIn(); - (*i)->reset(); - } - - return mTotalBandwidthIn; -} diff --git a/src/game-server/gamehandler.hpp b/src/game-server/gamehandler.hpp index 42ba8cbc..0ae0d495 100644 --- a/src/game-server/gamehandler.hpp +++ b/src/game-server/gamehandler.hpp @@ -118,16 +118,6 @@ class GameHandler: public ConnectionHandler */ GameClient *getClientByNameSlow(std::string const &); - /** - * Calculate the total amount of bandwidth sent to all clients - */ - int calculateTotalOut(); - - /** - * Calculate the total amount of bandwidth received from all clients - */ - int calculateTotalIn(); - protected: NetComputer *computerConnected(ENetPeer *); void computerDisconnected(NetComputer *); @@ -164,9 +154,6 @@ class GameHandler: public ConnectionHandler */ TokenCollector<GameHandler, GameClient *, Character *> mTokenCollector; - int mTotalBandwidthOut; - int mTotalBandwidthIn; - }; extern GameHandler *gameHandler; diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp index 40b99358..a0723e31 100644 --- a/src/game-server/main-game.cpp +++ b/src/game-server/main-game.cpp @@ -38,6 +38,7 @@ #include "game-server/postman.hpp" #include "game-server/resourcemanager.hpp" #include "game-server/state.hpp" +#include "net/bandwidth.hpp" #include "net/connectionhandler.hpp" #include "net/messageout.hpp" #include "utils/logger.h" @@ -68,6 +69,9 @@ AccountConnection *accountHandler; /** Post Man **/ PostMan *postMan; +/** Bandwidth Monitor */ +BandwidthMonitor *gBandwidth; + /** Callback used when SIGQUIT signal is received. */ void closeGracefully(int) { @@ -151,6 +155,7 @@ void initialize() gameHandler = new GameHandler; accountHandler = new AccountConnection; postMan = new PostMan; + gBandwidth = new BandwidthMonitor; // --- Initialize enet. if (enet_initialize() != 0) { @@ -187,6 +192,7 @@ void deinitialize() delete gameHandler; delete accountHandler; delete postMan; + delete gBandwidth; // Destroy Managers delete stringFilter; @@ -314,10 +320,10 @@ int main(int argc, char *argv[]) if (worldTime % 300 == 0) { accountHandler->sendStatistics(); - LOG_INFO("Total Account Output: " << accountHandler->totalOut() << " Bytes"); - LOG_INFO("Total Account Input: " << accountHandler->totalIn() << " Bytes"); - LOG_INFO("Total Client Output: " << gameHandler->calculateTotalOut() << " Bytes"); - LOG_INFO("Total Client Input: " << gameHandler->calculateTotalIn() << " Bytes"); + LOG_INFO("Total Account Output: " << gBandwidth->totalInterServerOut() << " Bytes"); + LOG_INFO("Total Account Input: " << gBandwidth->totalInterServerIn() << " Bytes"); + LOG_INFO("Total Client Output: " << gBandwidth->totalClientOut() << " Bytes"); + LOG_INFO("Total Client Input: " << gBandwidth->totalClientIn() << " Bytes"); } } else diff --git a/src/net/bandwidth.cpp b/src/net/bandwidth.cpp index 5fa580ee..c7b6f8e3 100644 --- a/src/net/bandwidth.cpp +++ b/src/net/bandwidth.cpp @@ -21,24 +21,59 @@ #include "bandwidth.hpp" +#include "netcomputer.hpp" + BandwidthMonitor::BandwidthMonitor(): - mAmountOutput(0), - mAmountInput(0) + mAmountServerOutput(0), + mAmountServerInput(0), + mAmountClientOutput(0), + mAmountClientInput(0) { } -void BandwidthMonitor::increaseOutput(int size) +void BandwidthMonitor::increaseInterServerOutput(int size) { - mAmountOutput += size; + mAmountServerOutput += size; } -void BandwidthMonitor::increaseInput(int size) +void BandwidthMonitor::increaseInterServerInput(int size) { - mAmountInput += size; + mAmountServerInput += size; } -void BandwidthMonitor::reset() +void BandwidthMonitor::increaseClientOutput(NetComputer *nc, int size) { - mAmountOutput = 0; - mAmountInput = 0; + mAmountClientOutput += size; + // look for an existing client stored + ClientBandwidth::iterator itr = mClientBandwidth.find(nc); + + // if there isnt one, create one + if (itr == mClientBandwidth.end()) + { + std::pair<ClientBandwidth::iterator, bool> retItr; + retItr = mClientBandwidth.insert(std::pair<NetComputer*, std::pair<int, int> >(nc, std::pair<int, int>(0, 0))); + itr = retItr.first; + } + + itr->second.first += size; + } + +void BandwidthMonitor::increaseClientInput(NetComputer *nc, int size) +{ + mAmountClientInput += size; + + // look for an existing client stored + ClientBandwidth::iterator itr = mClientBandwidth.find(nc); + + // if there isnt one, create it + if (itr == mClientBandwidth.end()) + { + std::pair<ClientBandwidth::iterator, bool> retItr; + retItr = mClientBandwidth.insert(std::pair<NetComputer*, std::pair<int, int> >(nc, std::pair<int, int>(0, 0))); + itr = retItr.first; + } + + itr->second.second += size; +} + diff --git a/src/net/bandwidth.hpp b/src/net/bandwidth.hpp index a524bfc0..2129fc9a 100644 --- a/src/net/bandwidth.hpp +++ b/src/net/bandwidth.hpp @@ -22,19 +22,33 @@ #ifndef _TMWSERV_BANDWIDTH_H_ #define _TMWSERV_BANDWIDTH_H_ +#include <map> + +class NetComputer; + class BandwidthMonitor { public: BandwidthMonitor(); - void increaseOutput(int size); - void increaseInput(int size); - void reset(); - int totalOut() const { return mAmountOutput; } - int totalIn() const { return mAmountInput; } + void increaseInterServerOutput(int size); + void increaseInterServerInput(int size); + void increaseClientOutput(NetComputer *nc, int size); + void increaseClientInput(NetComputer *nc, int size); + int totalInterServerOut() const { return mAmountServerOutput; } + int totalInterServerIn() const { return mAmountServerInput; } + int totalClientOut() const { return mAmountClientOutput; } + int totalClientIn() const { return mAmountClientInput; } private: - int mAmountOutput; - int mAmountInput; + int mAmountServerOutput; + int mAmountServerInput; + int mAmountClientOutput; + int mAmountClientInput; + // map of client to output and input + typedef std::map<NetComputer*, std::pair<int, int> > ClientBandwidth; + ClientBandwidth mClientBandwidth; }; +extern BandwidthMonitor *gBandwidth; + #endif diff --git a/src/net/connection.cpp b/src/net/connection.cpp index c99ee909..0b1f6079 100644 --- a/src/net/connection.cpp +++ b/src/net/connection.cpp @@ -29,7 +29,6 @@ Connection::Connection(): mRemote(0), mLocal(0) { - mBandwidth = new BandwidthMonitor; } bool Connection::start(std::string const &address, int port) @@ -68,11 +67,9 @@ void Connection::stop() enet_peer_reset(mRemote); if (mLocal) enet_host_destroy(mLocal); - delete mBandwidth; mRemote = 0; mLocal = 0; - mBandwidth = 0; } bool Connection::isConnected() const @@ -87,7 +84,7 @@ void Connection::send(MessageOut const &msg, bool reliable, unsigned channel) return; } - mBandwidth->increaseOutput(msg.getLength()); + gBandwidth->increaseInterServerOutput(msg.getLength()); ENetPacket *packet; packet = enet_packet_create(msg.getData(), @@ -113,7 +110,7 @@ void Connection::process() { MessageIn msg((char *)event.packet->data, event.packet->dataLength); - mBandwidth->increaseInput(event.packet->dataLength); + gBandwidth->increaseInterServerInput(event.packet->dataLength); processMessage(msg); } else @@ -129,13 +126,3 @@ void Connection::process() } } } - -int Connection::totalOut() -{ - return mBandwidth->totalOut(); -} - -int Connection::totalIn() -{ - return mBandwidth->totalIn(); -} diff --git a/src/net/connection.hpp b/src/net/connection.hpp index 0d417092..f815d46b 100644 --- a/src/net/connection.hpp +++ b/src/net/connection.hpp @@ -66,16 +66,6 @@ class Connection */ void process(); - /** - * Return total output - */ - int totalOut(); - - /** - * Return total input - */ - int totalIn(); - protected: /** * Processes a single message from the remote host. @@ -85,7 +75,6 @@ class Connection private: ENetPeer *mRemote; ENetHost *mLocal; - BandwidthMonitor *mBandwidth; }; #endif diff --git a/src/net/connectionhandler.cpp b/src/net/connectionhandler.cpp index dd1828ac..3f3bb72a 100644 --- a/src/net/connectionhandler.cpp +++ b/src/net/connectionhandler.cpp @@ -23,6 +23,7 @@ #include "net/connectionhandler.hpp" #include "defines.h" +#include "net/bandwidth.hpp" #include "net/messagein.hpp" #include "net/messageout.hpp" #include "net/netcomputer.hpp" @@ -104,7 +105,7 @@ void ConnectionHandler::process(enet_uint32 timeout) LOG_DEBUG("Received message " << msg << " from " << *comp); - comp->increaseIn(event.packet->dataLength); + gBandwidth->increaseClientInput(comp, event.packet->dataLength); processMessage(comp, msg); } else { diff --git a/src/net/netcomputer.cpp b/src/net/netcomputer.cpp index 657f615b..ff2bbb71 100644 --- a/src/net/netcomputer.cpp +++ b/src/net/netcomputer.cpp @@ -35,7 +35,6 @@ NetComputer::NetComputer(ENetPeer *peer): mPeer(peer) { - mBandwidth = new BandwidthMonitor; } bool @@ -68,7 +67,7 @@ NetComputer::send(const MessageOut &msg, bool reliable, { LOG_DEBUG("Sending message " << msg << " to " << *this); - mBandwidth->increaseOutput(msg.getLength()); + gBandwidth->increaseClientOutput(this, msg.getLength()); ENetPacket *packet; packet = enet_packet_create(msg.getData(), @@ -85,30 +84,6 @@ NetComputer::send(const MessageOut &msg, bool reliable, } } -int -NetComputer::totalOut() -{ - return mBandwidth->totalOut(); -} - -int -NetComputer::totalIn() -{ - return mBandwidth->totalIn(); -} - -void -NetComputer::increaseIn(int size) -{ - mBandwidth->increaseInput(size); -} - -void -NetComputer::reset() -{ - mBandwidth->reset(); -} - std::ostream& operator <<(std::ostream &os, const NetComputer &comp) { diff --git a/src/net/netcomputer.hpp b/src/net/netcomputer.hpp index 6acbb53b..b85456a6 100644 --- a/src/net/netcomputer.hpp +++ b/src/net/netcomputer.hpp @@ -81,31 +81,8 @@ class NetComputer send(const MessageOut &msg, bool reliable = true, unsigned int channel = 0); - /** - * Gets the amount of bandwidth that has been sent out by the server - * to this client - */ - int totalOut(); - - /** - * Gets the amount of bandwidth that has been received by the server - * from this client - */ - int totalIn(); - - /** - * Increase the total received by the server from the client - */ - void increaseIn(int size); - - /** - * Reset the totals - */ - void reset(); - private: ENetPeer *mPeer; /**< Client peer */ - BandwidthMonitor *mBandwidth; /**< Bandwidth monitoring */ /** * Converts the ip-address of the peer to a stringstream. |