summaryrefslogtreecommitdiff
path: root/src/net
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-03-31 09:45:43 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-03-31 09:45:43 +0000
commitbd9b9a04c0ec07c957014d0679d386c7b42e5312 (patch)
treef18a9c3de8a88f99472d0e87dd19e89512dca880 /src/net
parenta85d2b47912ea32e3ecf77632242fa6f759a0ade (diff)
downloadmanaserv-bd9b9a04c0ec07c957014d0679d386c7b42e5312.tar.gz
manaserv-bd9b9a04c0ec07c957014d0679d386c7b42e5312.tar.bz2
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.cpp12
-rw-r--r--src/net/messagein.cpp11
-rw-r--r--src/net/messagein.hpp30
3 files changed, 43 insertions, 10 deletions
diff --git a/src/net/connectionhandler.cpp b/src/net/connectionhandler.cpp
index 04bf0169..6de1835e 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 783cbf11..2b5eaf27 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 79063f97..674974f8 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