From 1a6f01a13ab9267fd7de46592bbb3107311f8709 Mon Sep 17 00:00:00 2001 From: David Athay Date: Thu, 20 Nov 2008 14:52:22 +0000 Subject: Made Bandwidth Monitor Global --- src/net/bandwidth.cpp | 53 +++++++++++++++++++++++++++++++++++-------- src/net/bandwidth.hpp | 28 +++++++++++++++++------ src/net/connection.cpp | 17 ++------------ src/net/connection.hpp | 11 --------- src/net/connectionhandler.cpp | 3 ++- src/net/netcomputer.cpp | 27 +--------------------- src/net/netcomputer.hpp | 23 ------------------- 7 files changed, 70 insertions(+), 92 deletions(-) (limited to 'src/net') 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 retItr; + retItr = mClientBandwidth.insert(std::pair >(nc, std::pair(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 retItr; + retItr = mClientBandwidth.insert(std::pair >(nc, std::pair(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 + +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 > 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. -- cgit v1.2.3-70-g09d2