diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-03-31 09:45:43 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-03-31 09:45:43 +0000 |
commit | bd9b9a04c0ec07c957014d0679d386c7b42e5312 (patch) | |
tree | f18a9c3de8a88f99472d0e87dd19e89512dca880 /src/net | |
parent | a85d2b47912ea32e3ecf77632242fa6f759a0ade (diff) | |
download | manaserv-bd9b9a04c0ec07c957014d0679d386c7b42e5312.tar.gz manaserv-bd9b9a04c0ec07c957014d0679d386c7b42e5312.tar.xz manaserv-bd9b9a04c0ec07c957014d0679d386c7b42e5312.zip |
Moved writing of incoming messages into MessageIn and tweaked the printing of
the message ID.
Diffstat (limited to 'src/net')
-rw-r--r-- | src/net/connectionhandler.cpp | 12 | ||||
-rw-r--r-- | src/net/messagein.cpp | 11 | ||||
-rw-r--r-- | src/net/messagein.hpp | 30 |
3 files changed, 43 insertions, 10 deletions
diff --git a/src/net/connectionhandler.cpp b/src/net/connectionhandler.cpp index 04bf016..6de1835 100644 --- a/src/net/connectionhandler.cpp +++ b/src/net/connectionhandler.cpp @@ -38,10 +38,11 @@ bool ConnectionHandler::startListen(enet_uint16 port) address.port = port; LOG_INFO("Listening on port " << port << "..."); - host = enet_host_create(&address /* the address to bind the server host to */, - MAX_CLIENTS /* allow up to MAX_CLIENTS clients and/or outgoing connections */, - 0 /* assume any amount of incoming bandwidth */, - 0 /* assume any amount of outgoing bandwidth */); + host = enet_host_create( + &address /* the address to bind the server host to */, + MAX_CLIENTS /* allow up to MAX_CLIENTS connections */, + 0 /* assume any amount of incoming bandwidth */, + 0 /* assume any amount of outgoing bandwidth */); return host; } @@ -111,8 +112,7 @@ void ConnectionHandler::process(enet_uint32 timeout) if (event.packet->dataLength >= 2) { MessageIn msg((char *)event.packet->data, event.packet->dataLength); - LOG_DEBUG("Received message " << msg.getId() << " (" - << event.packet->dataLength << " B) from " + LOG_DEBUG("Received message " << msg << " from " << *comp); processMessage(comp, msg); diff --git a/src/net/messagein.cpp b/src/net/messagein.cpp index 783cbf1..2b5eaf2 100644 --- a/src/net/messagein.cpp +++ b/src/net/messagein.cpp @@ -22,6 +22,8 @@ */ #include <string> +#include <iostream> +#include <iomanip> #include <enet/enet.h> #include "net/messagein.hpp" @@ -96,3 +98,12 @@ std::string MessageIn::readString(int length) return readString; } + +std::ostream& +operator <<(std::ostream &os, const MessageIn &msg) +{ + os << std::setw(6) << std::hex << std::showbase << std::internal + << std::setfill('0') << msg.getId() + << std::dec << " (" << msg.getLength() << " B)"; + return os; +} diff --git a/src/net/messagein.hpp b/src/net/messagein.hpp index 79063f9..674974f 100644 --- a/src/net/messagein.hpp +++ b/src/net/messagein.hpp @@ -39,7 +39,17 @@ class MessageIn */ MessageIn(const char *data, int length); - int getId() { return mId; } /**< Returns the message ID. */ + /** + * Returns the message ID. + */ + int + getId() const { return mId; } + + /** + * Returns the total length of this message. + */ + int + getLength() const { return mLength; } int readByte(); /**< Reads a byte. */ int readShort(); /**< Reads a short. */ @@ -50,16 +60,22 @@ class MessageIn * that the length of the string is stored in a short at the * start of the string. */ - std::string readString(int length = -1); + std::string + readString(int length = -1); /** * Returns the length of unread data. */ - int getUnreadLength() { return mLength - mPos; } + int + getUnreadLength() const { return mLength - mPos; } + + /** + * Returns + */ private: const char *mData; /**< Packet data */ - unsigned short mLength; /**< Length of data in bytes */ + unsigned short mLength; /**< Length of data in bytes */ unsigned short mId; /**< The message ID. */ /** @@ -67,6 +83,12 @@ class MessageIn * bigger than packet->length means EOP was reached when reading it. */ unsigned short mPos; + + /** + * Streams message ID and length to the given output stream. + */ + friend std::ostream& operator <<(std::ostream &os, + const MessageIn &msg); }; #endif |