diff options
author | David Athay <ko2fan@gmail.com> | 2008-11-19 13:36:50 +0000 |
---|---|---|
committer | David Athay <ko2fan@gmail.com> | 2008-11-19 15:02:10 +0000 |
commit | 8481413ea17177945d3d280b1518eb6f1f25cd5d (patch) | |
tree | 77ed689d7587a6d06c07dd81c3ca0d5c49b3284d /src/net | |
parent | 751cc20a3be820724ca575aab32c2bb424f89a5b (diff) | |
download | manaserv-8481413ea17177945d3d280b1518eb6f1f25cd5d.tar.gz manaserv-8481413ea17177945d3d280b1518eb6f1f25cd5d.tar.bz2 manaserv-8481413ea17177945d3d280b1518eb6f1f25cd5d.tar.xz manaserv-8481413ea17177945d3d280b1518eb6f1f25cd5d.zip |
Added bandwidth monitoring
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/bandwidth.cpp | 44 | ||||
-rw-r--r-- | src/net/bandwidth.hpp | 40 | ||||
-rw-r--r-- | src/net/connection.cpp | 17 | ||||
-rw-r--r-- | src/net/connection.hpp | 12 | ||||
-rw-r--r-- | src/net/connectionhandler.cpp | 2 | ||||
-rw-r--r-- | src/net/netcomputer.cpp | 40 | ||||
-rw-r--r-- | src/net/netcomputer.hpp | 24 |
7 files changed, 174 insertions, 5 deletions
diff --git a/src/net/bandwidth.cpp b/src/net/bandwidth.cpp new file mode 100644 index 00000000..5fa580ee --- /dev/null +++ b/src/net/bandwidth.cpp @@ -0,0 +1,44 @@ +/* + * The Mana World Server + * Copyright 2008 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 + */ + +#include "bandwidth.hpp" + +BandwidthMonitor::BandwidthMonitor(): + mAmountOutput(0), + mAmountInput(0) +{ +} + +void BandwidthMonitor::increaseOutput(int size) +{ + mAmountOutput += size; +} + +void BandwidthMonitor::increaseInput(int size) +{ + mAmountInput += size; +} + +void BandwidthMonitor::reset() +{ + mAmountOutput = 0; + mAmountInput = 0; +} diff --git a/src/net/bandwidth.hpp b/src/net/bandwidth.hpp new file mode 100644 index 00000000..a524bfc0 --- /dev/null +++ b/src/net/bandwidth.hpp @@ -0,0 +1,40 @@ +/* + * The Mana World Server + * Copyright 2008 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 + */ + +#ifndef _TMWSERV_BANDWIDTH_H_ +#define _TMWSERV_BANDWIDTH_H_ + +class BandwidthMonitor +{ +public: + BandwidthMonitor(); + void increaseOutput(int size); + void increaseInput(int size); + void reset(); + int totalOut() const { return mAmountOutput; } + int totalIn() const { return mAmountInput; } + +private: + int mAmountOutput; + int mAmountInput; +}; + +#endif diff --git a/src/net/connection.cpp b/src/net/connection.cpp index 4749c888..c99ee909 100644 --- a/src/net/connection.cpp +++ b/src/net/connection.cpp @@ -20,6 +20,7 @@ */ #include "net/connection.hpp" +#include "net/bandwidth.hpp" #include "net/messagein.hpp" #include "net/messageout.hpp" #include "utils/logger.h" @@ -28,6 +29,7 @@ Connection::Connection(): mRemote(0), mLocal(0) { + mBandwidth = new BandwidthMonitor; } bool Connection::start(std::string const &address, int port) @@ -66,9 +68,11 @@ 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 @@ -83,6 +87,8 @@ void Connection::send(MessageOut const &msg, bool reliable, unsigned channel) return; } + mBandwidth->increaseOutput(msg.getLength()); + ENetPacket *packet; packet = enet_packet_create(msg.getData(), msg.getLength(), @@ -107,6 +113,7 @@ void Connection::process() { MessageIn msg((char *)event.packet->data, event.packet->dataLength); + mBandwidth->increaseInput(event.packet->dataLength); processMessage(msg); } else @@ -122,3 +129,13 @@ 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 74eaaac7..0d417092 100644 --- a/src/net/connection.hpp +++ b/src/net/connection.hpp @@ -27,6 +27,7 @@ class MessageIn; class MessageOut; +class BandwidthMonitor; /** * A point-to-point connection to a remote host. The remote host can use a @@ -65,6 +66,16 @@ class Connection */ void process(); + /** + * Return total output + */ + int totalOut(); + + /** + * Return total input + */ + int totalIn(); + protected: /** * Processes a single message from the remote host. @@ -74,6 +85,7 @@ class Connection private: ENetPeer *mRemote; ENetHost *mLocal; + BandwidthMonitor *mBandwidth; }; #endif diff --git a/src/net/connectionhandler.cpp b/src/net/connectionhandler.cpp index 88e6f636..dd1828ac 100644 --- a/src/net/connectionhandler.cpp +++ b/src/net/connectionhandler.cpp @@ -104,6 +104,8 @@ void ConnectionHandler::process(enet_uint32 timeout) LOG_DEBUG("Received message " << msg << " from " << *comp); + comp->increaseIn(event.packet->dataLength); + processMessage(comp, msg); } else { LOG_ERROR("Message too short from " << *comp); diff --git a/src/net/netcomputer.cpp b/src/net/netcomputer.cpp index d5b0ea77..657f615b 100644 --- a/src/net/netcomputer.cpp +++ b/src/net/netcomputer.cpp @@ -23,15 +23,19 @@ #include <queue> #include <enet/enet.h> -#include "defines.h" -#include "net/messageout.hpp" -#include "net/netcomputer.hpp" -#include "utils/logger.h" -#include "utils/processorutils.hpp" +#include "bandwidth.hpp" +#include "messageout.hpp" +#include "netcomputer.hpp" + +#include "../defines.h" + +#include "../utils/logger.h" +#include "../utils/processorutils.hpp" NetComputer::NetComputer(ENetPeer *peer): mPeer(peer) { + mBandwidth = new BandwidthMonitor; } bool @@ -64,6 +68,8 @@ NetComputer::send(const MessageOut &msg, bool reliable, { LOG_DEBUG("Sending message " << msg << " to " << *this); + mBandwidth->increaseOutput(msg.getLength()); + ENetPacket *packet; packet = enet_packet_create(msg.getData(), msg.getLength(), @@ -79,6 +85,30 @@ 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 9a73c576..6acbb53b 100644 --- a/src/net/netcomputer.hpp +++ b/src/net/netcomputer.hpp @@ -26,6 +26,7 @@ #include <enet/enet.h> class MessageOut; +class BandwidthMonitor; /** * This class represents a known computer on the network. For example a @@ -80,8 +81,31 @@ 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. |