summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/connectionhandler.cpp5
-rw-r--r--src/connectionhandler.h9
-rw-r--r--src/messagehandler.h5
-rw-r--r--src/messagein.cpp25
-rw-r--r--src/messagein.h51
-rw-r--r--src/messageout.cpp36
-rw-r--r--src/messageout.h57
-rw-r--r--src/packet.cpp39
-rw-r--r--src/packet.h42
9 files changed, 268 insertions, 1 deletions
diff --git a/src/connectionhandler.cpp b/src/connectionhandler.cpp
index 354ea608..fba5d7dc 100644
--- a/src/connectionhandler.cpp
+++ b/src/connectionhandler.cpp
@@ -23,3 +23,8 @@
#include "connectionhandler.h"
+
+ConnectionHandler::registerHandler(unsigned int msgId, MessageHandler *handler)
+{
+ handlers[msgId] = handler;
+}
diff --git a/src/connectionhandler.h b/src/connectionhandler.h
index 23bc3504..2b4bd850 100644
--- a/src/connectionhandler.h
+++ b/src/connectionhandler.h
@@ -25,6 +25,7 @@
#include "netcomputer.h"
#include "packet.h"
+#include <map>
/**
* This class represents the connection handler interface. The connection
@@ -56,6 +57,14 @@ class ConnectionHandler
* Called when a computer sends a packet to the network session.
*/
void receivePacket(NetComputer *computer, Packet *packet);
+
+ /**
+ * Registers a message handler to handle a certain message type.
+ */
+ void registerHandler(unsigned int msgId, MessageHandler *handler);
+
+ private:
+ std::map<unsigned int, MessageHandler*> handlers;
};
#endif
diff --git a/src/messagehandler.h b/src/messagehandler.h
index 05bee96b..f52c61a6 100644
--- a/src/messagehandler.h
+++ b/src/messagehandler.h
@@ -23,6 +23,9 @@
#ifndef _TMW_SERVER_MESSAGEHANDLER_
+#include "netcomputer.h"
+#include "messagein.h"
+
/**
* This class represents the message handler interface. This interface is
* implemented by classes that mean to handle a certain subset of the incoming
@@ -41,7 +44,7 @@ class MessageHandler
* by both a MessageIn and a MessageOut class that would implement
* methods to convenient parse and build packets transparently.
*/
- void receiveMessage(Computer *computer, MessageIn *message);
+ void receiveMessage(NetComputer *computer, MessageIn &message);
};
#endif
diff --git a/src/messagein.cpp b/src/messagein.cpp
new file mode 100644
index 00000000..4c7dbddb
--- /dev/null
+++ b/src/messagein.cpp
@@ -0,0 +1,25 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 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
+ *
+ * $Id$
+ */
+
+#include "messagein.h"
+
diff --git a/src/messagein.h b/src/messagein.h
new file mode 100644
index 00000000..d731cdc1
--- /dev/null
+++ b/src/messagein.h
@@ -0,0 +1,51 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 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
+ *
+ * $Id$
+ */
+
+#include "packet.h"
+
+/**
+ * A helper class to
+ */
+class MessageIn
+{
+ public:
+ /**
+ * Constructor.
+ */
+ MessageIn(Packet *p);
+
+ char readByte(); /**< Reads a byte. */
+ int readShort(); /**< Reads a short. */
+ int readLong(); /**< Reads a long. */
+
+ /**
+ * Reads a string. If a length is not given (-1), it is assumed
+ * that the length of the string is stored in a short at the
+ * start of the string.
+ */
+ std::string readString(int length = -1);
+
+ private:
+ Packet *packet;
+ unsigned int pos;
+};
diff --git a/src/messageout.cpp b/src/messageout.cpp
new file mode 100644
index 00000000..7542b693
--- /dev/null
+++ b/src/messageout.cpp
@@ -0,0 +1,36 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 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
+ *
+ * $Id$
+ */
+
+#include "messageout.h"
+
+MessageOut::MessageOut():
+ packet(NULL)
+{
+}
+
+MessageOut::~MessageOut()
+{
+ if (packet) {
+ delete packet;
+ }
+}
diff --git a/src/messageout.h b/src/messageout.h
new file mode 100644
index 00000000..89119632
--- /dev/null
+++ b/src/messageout.h
@@ -0,0 +1,57 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 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
+ *
+ * $Id$
+ */
+
+#include "packet.h"
+
+class MessageOut
+{
+ public:
+ /**
+ * Constructor.
+ */
+ MessageOut();
+
+ /**
+ * Destructor.
+ */
+ ~MessageOut();
+
+ void writeByte(char value); /**< Reads a byte. */
+ void writeShort(short value); /**< Reads a short. */
+ void writeLong(long value); /**< Reads a long. */
+
+ /**
+ * Writes a string. If a fixed length is not given (-1), it is stored
+ * as a short at the start of the string.
+ */
+ void writeString(int length = -1);
+
+ /**
+ * Returns an instance of Packet derived from the written data. Use for
+ * sending the packet.
+ */
+ const Packet *getPacket();
+
+ private:
+ Packet *packet; /**< Created packet. */
+};
diff --git a/src/packet.cpp b/src/packet.cpp
new file mode 100644
index 00000000..a7cd00cb
--- /dev/null
+++ b/src/packet.cpp
@@ -0,0 +1,39 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 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
+ *
+ * $Id$
+ */
+
+#include "packet.h"
+#include <string.h>
+
+Packet::Packet(const char *data, int length):
+ length(length)
+{
+ // Create a copy of the data
+ this->data = new char[length];
+ memcpy(this->data, data, length);
+}
+
+Packet::~Packet()
+{
+ // Clean up the data
+ delete[] data;
+}
diff --git a/src/packet.h b/src/packet.h
new file mode 100644
index 00000000..5bb910ac
--- /dev/null
+++ b/src/packet.h
@@ -0,0 +1,42 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 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
+ *
+ * $Id$
+ */
+
+/**
+ * A packet wraps a certain amount of bytes for sending and receiving.
+ */
+class Packet
+{
+ public:
+ /**
+ * Constructor.
+ */
+ Packet(const char *data, int length);
+
+ /**
+ * Destructor.
+ */
+ ~Packet();
+
+ char *data; /**< Packet data */
+ unsigned int length; /**< Length of data in bytes */
+};