summaryrefslogtreecommitdiff
path: root/src/net/tmwa
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/tmwa')
-rw-r--r--src/net/tmwa/adminhandler.cpp4
-rw-r--r--src/net/tmwa/adminhandler.h2
-rw-r--r--src/net/tmwa/beinghandler.cpp4
-rw-r--r--src/net/tmwa/beinghandler.h2
-rw-r--r--src/net/tmwa/buysellhandler.cpp5
-rw-r--r--src/net/tmwa/buysellhandler.h2
-rw-r--r--src/net/tmwa/charserverhandler.cpp8
-rw-r--r--src/net/tmwa/charserverhandler.h4
-rw-r--r--src/net/tmwa/chathandler.cpp7
-rw-r--r--src/net/tmwa/chathandler.h2
-rw-r--r--src/net/tmwa/gamehandler.cpp7
-rw-r--r--src/net/tmwa/gamehandler.h2
-rw-r--r--src/net/tmwa/generalhandler.cpp6
-rw-r--r--src/net/tmwa/generalhandler.h2
-rw-r--r--src/net/tmwa/guildhandler.cpp3
-rw-r--r--src/net/tmwa/guildhandler.h2
-rw-r--r--src/net/tmwa/inventoryhandler.cpp7
-rw-r--r--src/net/tmwa/inventoryhandler.h2
-rw-r--r--src/net/tmwa/itemhandler.cpp5
-rw-r--r--src/net/tmwa/itemhandler.h2
-rw-r--r--src/net/tmwa/loginhandler.cpp6
-rw-r--r--src/net/tmwa/loginhandler.h2
-rw-r--r--src/net/tmwa/messagehandler.h6
-rw-r--r--src/net/tmwa/messagein.cpp120
-rw-r--r--src/net/tmwa/messagein.h73
-rw-r--r--src/net/tmwa/messageout.cpp45
-rw-r--r--src/net/tmwa/messageout.h48
-rw-r--r--src/net/tmwa/network.cpp2
-rw-r--r--src/net/tmwa/npchandler.cpp6
-rw-r--r--src/net/tmwa/npchandler.h2
-rw-r--r--src/net/tmwa/partyhandler.cpp7
-rw-r--r--src/net/tmwa/partyhandler.h2
-rw-r--r--src/net/tmwa/playerhandler.cpp7
-rw-r--r--src/net/tmwa/playerhandler.h2
-rw-r--r--src/net/tmwa/specialhandler.cpp7
-rw-r--r--src/net/tmwa/specialhandler.h2
-rw-r--r--src/net/tmwa/tradehandler.cpp6
-rw-r--r--src/net/tmwa/tradehandler.h2
38 files changed, 338 insertions, 85 deletions
diff --git a/src/net/tmwa/adminhandler.cpp b/src/net/tmwa/adminhandler.cpp
index 9184c761..29202eb7 100644
--- a/src/net/tmwa/adminhandler.cpp
+++ b/src/net/tmwa/adminhandler.cpp
@@ -30,6 +30,8 @@
#include "net/chathandler.h"
#include "net/net.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "utils/gettext.h"
@@ -53,7 +55,7 @@ AdminHandler::AdminHandler()
adminHandler = this;
}
-void AdminHandler::handleMessage(Net::MessageIn &msg)
+void AdminHandler::handleMessage(MessageIn &msg)
{
int id;
switch (msg.getId())
diff --git a/src/net/tmwa/adminhandler.h b/src/net/tmwa/adminhandler.h
index 9b38fa67..71c37a09 100644
--- a/src/net/tmwa/adminhandler.h
+++ b/src/net/tmwa/adminhandler.h
@@ -34,7 +34,7 @@ class AdminHandler : public MessageHandler, public Net::AdminHandler
public:
AdminHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void announce(const std::string &text);
diff --git a/src/net/tmwa/beinghandler.cpp b/src/net/tmwa/beinghandler.cpp
index b7d84685..fb01990a 100644
--- a/src/net/tmwa/beinghandler.cpp
+++ b/src/net/tmwa/beinghandler.cpp
@@ -33,6 +33,8 @@
#include "party.h"
#include "playerrelations.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "resources/hairdb.h"
@@ -146,7 +148,7 @@ static void handlePosMessage(Map *map, Being *dstBeing, Uint16 x, Uint16 y,
}
}
-void BeingHandler::handleMessage(Net::MessageIn &msg)
+void BeingHandler::handleMessage(MessageIn &msg)
{
if (!actorSpriteManager)
return;
diff --git a/src/net/tmwa/beinghandler.h b/src/net/tmwa/beinghandler.h
index c1bdbe43..81bbad74 100644
--- a/src/net/tmwa/beinghandler.h
+++ b/src/net/tmwa/beinghandler.h
@@ -31,7 +31,7 @@ class BeingHandler : public MessageHandler
public:
BeingHandler(bool enableSync);
- virtual void handleMessage(Net::MessageIn &msg);
+ virtual void handleMessage(MessageIn &msg);
private:
// Should we honor server "Stop Walking" packets
diff --git a/src/net/tmwa/buysellhandler.cpp b/src/net/tmwa/buysellhandler.cpp
index e6ccae56..908e1d6d 100644
--- a/src/net/tmwa/buysellhandler.cpp
+++ b/src/net/tmwa/buysellhandler.cpp
@@ -32,8 +32,7 @@
#include "gui/buyselldialog.h"
#include "gui/selldialog.h"
-#include "net/messagein.h"
-
+#include "net/tmwa/messagein.h"
#include "net/tmwa/protocol.h"
#include "utils/gettext.h"
@@ -54,7 +53,7 @@ BuySellHandler::BuySellHandler()
handledMessages = _messages;
}
-void BuySellHandler::handleMessage(Net::MessageIn &msg)
+void BuySellHandler::handleMessage(MessageIn &msg)
{
int n_items;
diff --git a/src/net/tmwa/buysellhandler.h b/src/net/tmwa/buysellhandler.h
index 18d0294c..b5229e0f 100644
--- a/src/net/tmwa/buysellhandler.h
+++ b/src/net/tmwa/buysellhandler.h
@@ -33,7 +33,7 @@ class BuySellHandler : public MessageHandler
public:
BuySellHandler();
- virtual void handleMessage(Net::MessageIn &msg);
+ virtual void handleMessage(MessageIn &msg);
private:
int mNpcId;
diff --git a/src/net/tmwa/charserverhandler.cpp b/src/net/tmwa/charserverhandler.cpp
index 8834798f..ce193821 100644
--- a/src/net/tmwa/charserverhandler.cpp
+++ b/src/net/tmwa/charserverhandler.cpp
@@ -29,12 +29,12 @@
#include "gui/okdialog.h"
#include "net/logindata.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
#include "net/net.h"
#include "net/tmwa/gamehandler.h"
#include "net/tmwa/loginhandler.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/network.h"
#include "net/tmwa/protocol.h"
@@ -68,7 +68,7 @@ CharServerHandler::CharServerHandler()
charHandler = this;
}
-void CharServerHandler::handleMessage(Net::MessageIn &msg)
+void CharServerHandler::handleMessage(MessageIn &msg)
{
switch (msg.getId())
{
@@ -196,7 +196,7 @@ void CharServerHandler::handleMessage(Net::MessageIn &msg)
}
}
-void CharServerHandler::readPlayerData(Net::MessageIn &msg, Net::Character *character)
+void CharServerHandler::readPlayerData(MessageIn &msg, Net::Character *character)
{
const Token &token =
static_cast<LoginHandler*>(Net::getLoginHandler())->getToken();
diff --git a/src/net/tmwa/charserverhandler.h b/src/net/tmwa/charserverhandler.h
index 31b2ba8e..c9f44e0f 100644
--- a/src/net/tmwa/charserverhandler.h
+++ b/src/net/tmwa/charserverhandler.h
@@ -40,7 +40,7 @@ class CharServerHandler : public MessageHandler, public Net::CharHandler
public:
CharServerHandler();
- virtual void handleMessage(Net::MessageIn &msg);
+ virtual void handleMessage(MessageIn &msg);
void setCharSelectDialog(CharSelectDialog *window);
@@ -82,7 +82,7 @@ class CharServerHandler : public MessageHandler, public Net::CharHandler
void connect();
private:
- void readPlayerData(Net::MessageIn &msg, Net::Character *character);
+ void readPlayerData(MessageIn &msg, Net::Character *character);
};
} // namespace TmwAthena
diff --git a/src/net/tmwa/chathandler.cpp b/src/net/tmwa/chathandler.cpp
index 134db59f..9e5e7b19 100644
--- a/src/net/tmwa/chathandler.cpp
+++ b/src/net/tmwa/chathandler.cpp
@@ -28,9 +28,8 @@
#include "localplayer.h"
#include "playerrelations.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
-
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "utils/gettext.h"
@@ -57,7 +56,7 @@ ChatHandler::ChatHandler()
chatHandler = this;
}
-void ChatHandler::handleMessage(Net::MessageIn &msg)
+void ChatHandler::handleMessage(MessageIn &msg)
{
Being *being;
std::string chatMsg;
diff --git a/src/net/tmwa/chathandler.h b/src/net/tmwa/chathandler.h
index 65db4bec..fa3a8ec3 100644
--- a/src/net/tmwa/chathandler.h
+++ b/src/net/tmwa/chathandler.h
@@ -36,7 +36,7 @@ class ChatHandler : public MessageHandler, public Net::ChatHandler
public:
ChatHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void talk(const std::string &text);
diff --git a/src/net/tmwa/gamehandler.cpp b/src/net/tmwa/gamehandler.cpp
index 93f0b86f..68ed62b0 100644
--- a/src/net/tmwa/gamehandler.cpp
+++ b/src/net/tmwa/gamehandler.cpp
@@ -29,10 +29,9 @@
#include "gui/okdialog.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
-
#include "net/tmwa/loginhandler.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/network.h"
#include "net/tmwa/protocol.h"
@@ -60,7 +59,7 @@ GameHandler::GameHandler()
listen(Event::GameChannel);
}
-void GameHandler::handleMessage(Net::MessageIn &msg)
+void GameHandler::handleMessage(MessageIn &msg)
{
switch (msg.getId())
{
diff --git a/src/net/tmwa/gamehandler.h b/src/net/tmwa/gamehandler.h
index 4f0525b9..a6bcde0a 100644
--- a/src/net/tmwa/gamehandler.h
+++ b/src/net/tmwa/gamehandler.h
@@ -39,7 +39,7 @@ class GameHandler : public MessageHandler, public Net::GameHandler,
public:
GameHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void event(Event::Channel channel, const Event &event);
diff --git a/src/net/tmwa/generalhandler.cpp b/src/net/tmwa/generalhandler.cpp
index 3bbadccd..6c2bcddb 100644
--- a/src/net/tmwa/generalhandler.cpp
+++ b/src/net/tmwa/generalhandler.cpp
@@ -32,8 +32,6 @@
#include "gui/socialwindow.h"
#include "gui/statuswindow.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
#include "net/serverinfo.h"
#include "net/tmwa/adminhandler.h"
@@ -46,6 +44,8 @@
#include "net/tmwa/inventoryhandler.h"
#include "net/tmwa/itemhandler.h"
#include "net/tmwa/loginhandler.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/network.h"
#include "net/tmwa/npchandler.h"
#include "net/tmwa/partyhandler.h"
@@ -116,7 +116,7 @@ GeneralHandler::~GeneralHandler()
delete mNetwork;
}
-void GeneralHandler::handleMessage(Net::MessageIn &msg)
+void GeneralHandler::handleMessage(MessageIn &msg)
{
int code;
diff --git a/src/net/tmwa/generalhandler.h b/src/net/tmwa/generalhandler.h
index 7c8c6037..a34852fd 100644
--- a/src/net/tmwa/generalhandler.h
+++ b/src/net/tmwa/generalhandler.h
@@ -39,7 +39,7 @@ class GeneralHandler : public MessageHandler, public Net::GeneralHandler,
~GeneralHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void load();
diff --git a/src/net/tmwa/guildhandler.cpp b/src/net/tmwa/guildhandler.cpp
index f34b914e..a3194991 100644
--- a/src/net/tmwa/guildhandler.cpp
+++ b/src/net/tmwa/guildhandler.cpp
@@ -28,6 +28,7 @@
#include "gui/socialwindow.h"
#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "net/tmwa/gui/guildtab.h"
@@ -85,7 +86,7 @@ GuildHandler::~GuildHandler()
guildTab = 0;
}
-void GuildHandler::handleMessage(Net::MessageIn &msg)
+void GuildHandler::handleMessage(MessageIn &msg)
{
switch (msg.getId())
{
diff --git a/src/net/tmwa/guildhandler.h b/src/net/tmwa/guildhandler.h
index cc2dd524..4a74a26b 100644
--- a/src/net/tmwa/guildhandler.h
+++ b/src/net/tmwa/guildhandler.h
@@ -34,7 +34,7 @@ class GuildHandler : public Net::GuildHandler, public MessageHandler
~GuildHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void create(const std::string &name);
diff --git a/src/net/tmwa/inventoryhandler.cpp b/src/net/tmwa/inventoryhandler.cpp
index f260110d..256e7159 100644
--- a/src/net/tmwa/inventoryhandler.cpp
+++ b/src/net/tmwa/inventoryhandler.cpp
@@ -34,9 +34,8 @@
#include "gui/equipmentwindow.h"
#include "gui/widgets/chattab.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
-
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "resources/iteminfo.h"
@@ -125,7 +124,7 @@ InventoryHandler::~InventoryHandler()
delete mStorage;
}
-void InventoryHandler::handleMessage(Net::MessageIn &msg)
+void InventoryHandler::handleMessage(MessageIn &msg)
{
int number, flag;
int index, amount, itemId, equipType;
diff --git a/src/net/tmwa/inventoryhandler.h b/src/net/tmwa/inventoryhandler.h
index 88ab953d..3eefdd9b 100644
--- a/src/net/tmwa/inventoryhandler.h
+++ b/src/net/tmwa/inventoryhandler.h
@@ -187,7 +187,7 @@ class InventoryHandler : public MessageHandler, public Net::InventoryHandler,
~InventoryHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void event(Event::Channel channel, const Event &event);
diff --git a/src/net/tmwa/itemhandler.cpp b/src/net/tmwa/itemhandler.cpp
index d1a560f3..f05bb899 100644
--- a/src/net/tmwa/itemhandler.cpp
+++ b/src/net/tmwa/itemhandler.cpp
@@ -21,8 +21,7 @@
#include "net/tmwa/itemhandler.h"
-#include "net/messagein.h"
-
+#include "net/tmwa/messagein.h"
#include "net/tmwa/protocol.h"
#include "actorspritemanager.h"
@@ -42,7 +41,7 @@ ItemHandler::ItemHandler()
handledMessages = _messages;
}
-void ItemHandler::handleMessage(Net::MessageIn &msg)
+void ItemHandler::handleMessage(MessageIn &msg)
{
switch (msg.getId())
{
diff --git a/src/net/tmwa/itemhandler.h b/src/net/tmwa/itemhandler.h
index 96557d8b..3f58a172 100644
--- a/src/net/tmwa/itemhandler.h
+++ b/src/net/tmwa/itemhandler.h
@@ -31,7 +31,7 @@ class ItemHandler : public MessageHandler
public:
ItemHandler();
- virtual void handleMessage(Net::MessageIn &msg);
+ virtual void handleMessage(MessageIn &msg);
};
} // namespace TmwAthena
diff --git a/src/net/tmwa/loginhandler.cpp b/src/net/tmwa/loginhandler.cpp
index 393e712a..51c757cc 100644
--- a/src/net/tmwa/loginhandler.cpp
+++ b/src/net/tmwa/loginhandler.cpp
@@ -25,9 +25,9 @@
#include "log.h"
#include "net/logindata.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/network.h"
#include "net/tmwa/protocol.h"
@@ -62,7 +62,7 @@ LoginHandler::~LoginHandler()
delete_all(mWorlds);
}
-void LoginHandler::handleMessage(Net::MessageIn &msg)
+void LoginHandler::handleMessage(MessageIn &msg)
{
int code, worldCount;
diff --git a/src/net/tmwa/loginhandler.h b/src/net/tmwa/loginhandler.h
index fcd51e46..fcc7c5cc 100644
--- a/src/net/tmwa/loginhandler.h
+++ b/src/net/tmwa/loginhandler.h
@@ -40,7 +40,7 @@ class LoginHandler : public MessageHandler, public Net::LoginHandler
~LoginHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void connect();
diff --git a/src/net/tmwa/messagehandler.h b/src/net/tmwa/messagehandler.h
index 82bbabbc..2b1f5b7e 100644
--- a/src/net/tmwa/messagehandler.h
+++ b/src/net/tmwa/messagehandler.h
@@ -23,14 +23,12 @@
#define NET_TA_MESSAGEHANDLER_H
#include "net/messagehandler.h"
-#include "net/messagein.h"
-
-#include "net/tmwa/messageout.h"
#include <memory>
namespace TmwAthena {
+class MessageIn;
class Network;
/**
@@ -43,6 +41,8 @@ class MessageHandler : public Net::MessageHandler
~MessageHandler();
+ virtual void handleMessage(MessageIn &msg) = 0;
+
void setNetwork(Network *network);
protected:
diff --git a/src/net/tmwa/messagein.cpp b/src/net/tmwa/messagein.cpp
index c4d083b3..899b135d 100644
--- a/src/net/tmwa/messagein.cpp
+++ b/src/net/tmwa/messagein.cpp
@@ -24,15 +24,32 @@
#include <SDL.h>
#include <SDL_endian.h>
+#define MAKEWORD(low,high) \
+ ((unsigned short)(((unsigned char)(low)) | \
+ ((unsigned short)((unsigned char)(high))) << 8))
+
namespace TmwAthena {
MessageIn::MessageIn(const char *data, unsigned int length):
- Net::MessageIn(data, length)
+ mData(data),
+ mLength(length),
+ mPos(0)
{
// Read the message ID
mId = readInt16();
}
+uint8_t MessageIn::readInt8()
+{
+ uint8_t value = 0;
+ if (mPos < mLength)
+ {
+ value = mData[mPos];
+ }
+ mPos++;
+ return value;
+}
+
uint16_t MessageIn::readInt16()
{
uint16_t value = 0;
@@ -67,4 +84,105 @@ uint32_t MessageIn::readInt32()
return value;
}
+void MessageIn::readCoordinates(uint16_t &x, uint16_t &y, uint8_t &direction)
+{
+ if (mPos + 3 <= mLength)
+ {
+ const char *data = mData + mPos;
+ uint16_t temp;
+
+ temp = MAKEWORD(data[1] & 0x00c0, data[0] & 0x00ff);
+ x = temp >> 6;
+ temp = MAKEWORD(data[2] & 0x00f0, data[1] & 0x003f);
+ y = temp >> 4;
+
+ direction = data[2] & 0x000f;
+
+ // Translate from tmwAthena format
+ switch (direction)
+ {
+ case 0:
+ direction = 1;
+ break;
+ case 1:
+ direction = 3;
+ break;
+ case 2:
+ direction = 2;
+ break;
+ case 3:
+ direction = 6;
+ break;
+ case 4:
+ direction = 4;
+ break;
+ case 5:
+ direction = 12;
+ break;
+ case 6:
+ direction = 8;
+ break;
+ case 7:
+ direction = 9;
+ break;
+ case 8:
+ direction = 8;
+ break;
+ default:
+ // OOPSIE! Impossible or unknown
+ direction = 0;
+ }
+ }
+ mPos += 3;
+}
+
+void MessageIn::readCoordinatePair(uint16_t &srcX, uint16_t &srcY,
+ uint16_t &dstX, uint16_t &dstY)
+{
+ if (mPos + 5 <= mLength)
+ {
+ const char *data = mData + mPos;
+ uint16_t temp;
+
+ temp = MAKEWORD(data[3], data[2] & 0x000f);
+ dstX = temp >> 2;
+
+ dstY = MAKEWORD(data[4], data[3] & 0x0003);
+
+ temp = MAKEWORD(data[1], data[0]);
+ srcX = temp >> 6;
+
+ temp = MAKEWORD(data[2], data[1] & 0x003f);
+ srcY = temp >> 4;
+ }
+ mPos += 5;
+}
+
+void MessageIn::skip(unsigned int length)
+{
+ mPos += length;
+}
+
+std::string MessageIn::readString(int length)
+{
+ // Get string length
+ if (length < 0)
+ length = readInt16();
+
+ // Make sure the string isn't erroneous
+ if (length < 0 || mPos + length > mLength)
+ {
+ mPos = mLength + 1;
+ return "";
+ }
+
+ // Read the string
+ char const *stringBeg = mData + mPos;
+ char const *stringEnd = (char const *)memchr(stringBeg, '\0', length);
+ std::string readString(stringBeg,
+ stringEnd ? stringEnd - stringBeg : length);
+ mPos += length;
+ return readString;
+}
+
} // namespace TmwAthena
diff --git a/src/net/tmwa/messagein.h b/src/net/tmwa/messagein.h
index 714f71bb..48121187 100644
--- a/src/net/tmwa/messagein.h
+++ b/src/net/tmwa/messagein.h
@@ -22,26 +22,89 @@
#ifndef NET_TA_MESSAGEIN_H
#define NET_TA_MESSAGEIN_H
-#include "net/messagein.h"
-
+#include <cstdint>
#include <string>
namespace TmwAthena {
/**
- * Used for parsing an incoming message.
+ * Used for parsing an incoming message from eAthena.
*
* \ingroup Network
*/
- class MessageIn : public Net::MessageIn
+class MessageIn
{
public:
MessageIn(const char *data, unsigned int length);
+ /**
+ * Returns the message ID.
+ */
+ uint16_t getId() const { return mId; }
+
+ /**
+ * Returns the message length.
+ */
+ unsigned int getLength() const { return mLength; }
+
+ /**
+ * Returns the length of unread data.
+ */
+ unsigned int getUnreadLength() const { return mLength - mPos; }
+
+ /**
+ * Reads an unsigned 8-bit integer from the message.
+ */
+ uint8_t readInt8();
+
+ /**
+ * Reads an unsigned 16-bit integer from the message.
+ */
uint16_t readInt16();
+
+ /**
+ * Reads an unsigned 32-bit integer from the message.
+ */
uint32_t readInt32();
+
+ /**
+ * Reads a special 3 byte block used by eAthena, containing x and y
+ * coordinates and direction.
+ */
+ void readCoordinates(uint16_t &x, uint16_t &y, uint8_t &direction);
+
+ /**
+ * Reads a special 5 byte block used by eAthena, containing a source
+ * and destination coordinate pair.
+ */
+ void readCoordinatePair(uint16_t &srcX, uint16_t &srcY,
+ uint16_t &dstX, uint16_t &dstY);
+
+ /**
+ * Skips a given number of bytes.
+ */
+ void skip(unsigned int length);
+
+ /**
+ * 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:
+ const char *mData; /**< The message data. */
+ unsigned int mLength; /**< The length of the data. */
+ unsigned short mId; /**< The message ID. */
+
+ /**
+ * Actual position in the packet. From 0 to packet->length.
+ * A value bigger than packet->length means EOP was reached when
+ * reading it.
+ */
+ unsigned int mPos;
};
-}
+} // TmwAthena
#endif // NET_TA_MESSAGEIN_H
diff --git a/src/net/tmwa/messageout.cpp b/src/net/tmwa/messageout.cpp
index 7f09ecce..ae5f6dba 100644
--- a/src/net/tmwa/messageout.cpp
+++ b/src/net/tmwa/messageout.cpp
@@ -27,15 +27,16 @@
#include <SDL_endian.h>
#include <cstring>
-#include <string>
namespace TmwAthena {
MessageOut::MessageOut(uint16_t id):
- Net::MessageOut(id)
+ mDataSize(0),
+ mPos(0)
{
mNetwork = TmwAthena::Network::instance();
mData = mNetwork->mOutBuffer + mNetwork->mOutSize;
+
writeInt16(id);
}
@@ -44,11 +45,18 @@ void MessageOut::expand(size_t bytes)
mNetwork->mOutSize += bytes;
}
+void MessageOut::writeInt8(uint8_t value)
+{
+ expand(1);
+ mData[mPos] = value;
+ mPos += 1;
+}
+
void MessageOut::writeInt16(uint16_t value)
{
expand(2);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- uint16_t swap=SDL_Swap16(value);
+ uint16_t swap = SDL_Swap16(value);
memcpy(mData + mPos, &swap, sizeof(uint16_t));
#else
memcpy(mData + mPos, &value, sizeof(uint16_t));
@@ -60,7 +68,7 @@ void MessageOut::writeInt32(uint32_t value)
{
expand(4);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- uint32_t swap=SDL_Swap32(value);
+ uint32_t swap = SDL_Swap32(value);
memcpy(mData + mPos, &swap, sizeof(uint32_t));
#else
memcpy(mData + mPos, &value, sizeof(uint32_t));
@@ -68,10 +76,37 @@ void MessageOut::writeInt32(uint32_t value)
mPos += 4;
}
+void MessageOut::writeString(const std::string &string, int length)
+{
+ int stringLength = string.length();
+ if (length < 0)
+ {
+ // Write the length at the start if not fixed
+ writeInt16(stringLength);
+ length = stringLength;
+ }
+ else if (length < stringLength)
+ {
+ // Make sure the length of the string is no longer than specified
+ stringLength = length;
+ }
+ expand(length);
+
+ // Write the actual string
+ memcpy(mData + mPos, string.data(), stringLength);
+
+ // Pad remaining space with zeros
+ if (length > stringLength)
+ {
+ memset(mData + mPos + stringLength, '\0', length - stringLength);
+ }
+ mPos += length;
+}
+
void MessageOut::writeCoordinates(uint16_t x, uint16_t y, uint8_t direction)
{
char *data = mData + mPos;
- mNetwork->mOutSize += 3;
+ expand(3);
mPos += 3;
uint16_t temp = x;
diff --git a/src/net/tmwa/messageout.h b/src/net/tmwa/messageout.h
index d30fd1ec..dd2b84cc 100644
--- a/src/net/tmwa/messageout.h
+++ b/src/net/tmwa/messageout.h
@@ -22,37 +22,77 @@
#ifndef NET_TA_MESSAGEOUT_H
#define NET_TA_MESSAGEOUT_H
-#include "net/messageout.h"
+#include <cstdint>
+#include <string>
namespace TmwAthena {
class Network;
/**
- * Used for building an outgoing message.
+ * Used for building an outgoing message to eAthena.
*
* \ingroup Network
*/
-class MessageOut : public Net::MessageOut
+class MessageOut
{
public:
MessageOut(uint16_t id);
+ /**
+ * Writes an unsigned 8-bit integer to the message.
+ */
+ void writeInt8(uint8_t value);
+
+ /**
+ * Writes an unsigned 16-bit integer to the message.
+ */
void writeInt16(uint16_t value);
+
+ /**
+ * Writes an unsigned 32-bit integer to the message.
+ */
void writeInt32(uint32_t value);
/**
+ * 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(const std::string &string, int length = -1);
+
+ /**
* Encodes coordinates and direction in 3 bytes.
*/
void writeCoordinates(uint16_t x, uint16_t y,
uint8_t direction);
+ /**
+ * Returns the content of the message.
+ */
+ char *getData() const { return mData; }
+
+ /**
+ * Returns the length of the data.
+ */
+ unsigned int getDataSize() const { return mDataSize; }
+
private:
+ /**
+ * Expand the packet data to be able to hold more data.
+ *
+ * NOTE: For performance enhancements this method could allocate extra
+ * memory in advance instead of expanding size every time more data is
+ * added.
+ */
void expand(size_t size);
Network *mNetwork;
+
+ char *mData; /**< Data building up. */
+ unsigned int mDataSize; /**< Size of data. */
+ unsigned int mPos; /**< Position in the data. */
};
-}
+} // namespace TmwAthena
#endif // NET_TA_MESSAGEOUT_H
diff --git a/src/net/tmwa/network.cpp b/src/net/tmwa/network.cpp
index 5f5d4631..3a02deba 100644
--- a/src/net/tmwa/network.cpp
+++ b/src/net/tmwa/network.cpp
@@ -24,8 +24,8 @@
#include "log.h"
#include "net/messagehandler.h"
-#include "net/messagein.h"
+#include "net/tmwa/messagein.h"
#include "net/tmwa/protocol.h"
#include "utils/gettext.h"
diff --git a/src/net/tmwa/npchandler.cpp b/src/net/tmwa/npchandler.cpp
index 71f9ba2a..2b0a5b0a 100644
--- a/src/net/tmwa/npchandler.cpp
+++ b/src/net/tmwa/npchandler.cpp
@@ -25,11 +25,11 @@
#include "event.h"
#include "localplayer.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
#include "net/net.h"
#include "net/npchandler.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "utils/stringutils.h"
@@ -68,7 +68,7 @@ NpcHandler::NpcHandler()
npcHandler = this;
}
-void NpcHandler::handleMessage(Net::MessageIn &msg)
+void NpcHandler::handleMessage(MessageIn &msg)
{
if (msg.getId() == SMSG_NPC_CHOICE || msg.getId() == SMSG_NPC_MESSAGE)
{
diff --git a/src/net/tmwa/npchandler.h b/src/net/tmwa/npchandler.h
index 1c3d99c3..71486a40 100644
--- a/src/net/tmwa/npchandler.h
+++ b/src/net/tmwa/npchandler.h
@@ -37,7 +37,7 @@ class NpcHandler : public MessageHandler, public Net::NpcHandler
public:
NpcHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void startShopping(int beingId);
diff --git a/src/net/tmwa/partyhandler.cpp b/src/net/tmwa/partyhandler.cpp
index 314136e6..4753dac1 100644
--- a/src/net/tmwa/partyhandler.cpp
+++ b/src/net/tmwa/partyhandler.cpp
@@ -28,9 +28,8 @@
#include "gui/socialwindow.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
-
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "net/tmwa/gui/partytab.h"
@@ -74,7 +73,7 @@ PartyHandler::~PartyHandler()
partyTab = 0;
}
-void PartyHandler::handleMessage(Net::MessageIn &msg)
+void PartyHandler::handleMessage(MessageIn &msg)
{
switch (msg.getId())
{
diff --git a/src/net/tmwa/partyhandler.h b/src/net/tmwa/partyhandler.h
index bf567bb6..9a3570a8 100644
--- a/src/net/tmwa/partyhandler.h
+++ b/src/net/tmwa/partyhandler.h
@@ -38,7 +38,7 @@ class PartyHandler : public MessageHandler, public Net::PartyHandler
~PartyHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void create(const std::string &name = "");
diff --git a/src/net/tmwa/playerhandler.cpp b/src/net/tmwa/playerhandler.cpp
index a3651a2f..c598c5f9 100644
--- a/src/net/tmwa/playerhandler.cpp
+++ b/src/net/tmwa/playerhandler.cpp
@@ -38,9 +38,8 @@
#include "gui/statuswindow.h"
#include "gui/viewport.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
-
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "utils/stringutils.h"
@@ -159,7 +158,7 @@ PlayerHandler::PlayerHandler()
playerHandler = this;
}
-void PlayerHandler::handleMessage(Net::MessageIn &msg)
+void PlayerHandler::handleMessage(MessageIn &msg)
{
if (!local_player)
return;
diff --git a/src/net/tmwa/playerhandler.h b/src/net/tmwa/playerhandler.h
index 7e31366b..2c4b6194 100644
--- a/src/net/tmwa/playerhandler.h
+++ b/src/net/tmwa/playerhandler.h
@@ -34,7 +34,7 @@ class PlayerHandler : public MessageHandler, public Net::PlayerHandler
public:
PlayerHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void attack(int id);
void emote(int emoteId);
diff --git a/src/net/tmwa/specialhandler.cpp b/src/net/tmwa/specialhandler.cpp
index b814cd41..7de0aaea 100644
--- a/src/net/tmwa/specialhandler.cpp
+++ b/src/net/tmwa/specialhandler.cpp
@@ -27,9 +27,8 @@
#include "gui/skilldialog.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
-
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "utils/gettext.h"
@@ -82,7 +81,7 @@ SpecialHandler::SpecialHandler()
specialHandler = this;
}
-void SpecialHandler::handleMessage(Net::MessageIn &msg)
+void SpecialHandler::handleMessage(MessageIn &msg)
{
int skillCount;
int skillId;
diff --git a/src/net/tmwa/specialhandler.h b/src/net/tmwa/specialhandler.h
index 40223056..bb772ff1 100644
--- a/src/net/tmwa/specialhandler.h
+++ b/src/net/tmwa/specialhandler.h
@@ -34,7 +34,7 @@ class SpecialHandler : public MessageHandler, public Net::SpecialHandler
public:
SpecialHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void use(int id);
diff --git a/src/net/tmwa/tradehandler.cpp b/src/net/tmwa/tradehandler.cpp
index 66772468..40e37594 100644
--- a/src/net/tmwa/tradehandler.cpp
+++ b/src/net/tmwa/tradehandler.cpp
@@ -32,9 +32,9 @@
#include "gui/tradewindow.h"
#include "net/inventoryhandler.h"
-#include "net/messagein.h"
-#include "net/messageout.h"
+#include "net/tmwa/messagein.h"
+#include "net/tmwa/messageout.h"
#include "net/tmwa/protocol.h"
#include "resources/iteminfo.h"
@@ -81,7 +81,7 @@ TradeHandler::TradeHandler()
}
-void TradeHandler::handleMessage(Net::MessageIn &msg)
+void TradeHandler::handleMessage(MessageIn &msg)
{
switch (msg.getId())
{
diff --git a/src/net/tmwa/tradehandler.h b/src/net/tmwa/tradehandler.h
index 443d763a..60ee4444 100644
--- a/src/net/tmwa/tradehandler.h
+++ b/src/net/tmwa/tradehandler.h
@@ -34,7 +34,7 @@ class TradeHandler : public MessageHandler, public Net::TradeHandler
public:
TradeHandler();
- void handleMessage(Net::MessageIn &msg);
+ void handleMessage(MessageIn &msg);
void request(Being *being);