Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding network msg to network msg #4878

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2527,6 +2527,7 @@ void LuaScriptInterface::registerFunctions()
registerMethod(L, "NetworkMessage", "addDouble", LuaScriptInterface::luaNetworkMessageAddDouble);
registerMethod(L, "NetworkMessage", "addItem", LuaScriptInterface::luaNetworkMessageAddItem);
registerMethod(L, "NetworkMessage", "addItemId", LuaScriptInterface::luaNetworkMessageAddItemId);
registerMethod(L, "NetworkMessage", "compose", LuaScriptInterface::luaNetworkMessageCompose);

registerMethod(L, "NetworkMessage", "reset", LuaScriptInterface::luaNetworkMessageReset);
registerMethod(L, "NetworkMessage", "seek", LuaScriptInterface::luaNetworkMessageSeek);
Expand Down Expand Up @@ -6293,6 +6294,26 @@ int LuaScriptInterface::luaNetworkMessageAddItemId(lua_State* L)
return 1;
}

int LuaScriptInterface::luaNetworkMessageCompose(lua_State* L)
{
// networkMessage:compose(otherMsg)
NetworkMessage* message = tfs::lua::getUserdata<NetworkMessage>(L, 1);
if (!message) {
lua_pushnil(L);
return 1;
}

NetworkMessage* msgToAdd = tfs::lua::getUserdata<NetworkMessage>(L, 2);
if (!msgToAdd) {
lua_pushnil(L);
return 1;
}

message->addNetworkMessage(*msgToAdd);
tfs::lua::pushBoolean(L, true);
return 1;
}
Comment on lines +6312 to +6315
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we always return true, even when there’s no space in the msg body


int LuaScriptInterface::luaNetworkMessageReset(lua_State* L)
{
// networkMessage:reset()
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ class LuaScriptInterface
static int luaNetworkMessageAddDouble(lua_State* L);
static int luaNetworkMessageAddItem(lua_State* L);
static int luaNetworkMessageAddItemId(lua_State* L);
static int luaNetworkMessageCompose(lua_State* L);

static int luaNetworkMessageReset(lua_State* L);
static int luaNetworkMessageSeek(lua_State* L);
Expand Down
26 changes: 24 additions & 2 deletions src/networkmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <boost/locale.hpp>

static constexpr size_t MAX_BODY_SIZE = 8192;

std::string NetworkMessage::getString(uint16_t stringLen /* = 0*/)
{
if (stringLen == 0) {
Expand Down Expand Up @@ -42,7 +44,7 @@ void NetworkMessage::addString(std::string_view value)
std::string latin1Str = boost::locale::conv::from_utf<char>(value.data(), value.data() + value.size(), "ISO-8859-1",
boost::locale::conv::skip);
size_t stringLen = latin1Str.size();
if (!canAdd(stringLen + 2) || stringLen > 8192) {
if (!canAdd(stringLen + 2) || stringLen > MAX_BODY_SIZE) {
return;
}

Expand All @@ -61,7 +63,18 @@ void NetworkMessage::addDouble(double value, uint8_t precision /* = 2*/)

void NetworkMessage::addBytes(const char* bytes, size_t size)
{
if (!canAdd(size) || size > 8192) {
if (!canAdd(size) || size > MAX_BODY_SIZE) {
return;
}

std::memcpy(buffer.data() + info.position, bytes, size);
info.position += size;
info.length += size;
}

void NetworkMessage::addBytes(const uint8_t* bytes, size_t size)
{
if (!canAdd(size) || size > MAX_BODY_SIZE) {
return;
}

Expand Down Expand Up @@ -191,3 +204,12 @@ void NetworkMessage::addItem(const Item* item)
}

void NetworkMessage::addItemId(uint16_t itemId) { add<uint16_t>(Item::items[itemId].clientId); }

void NetworkMessage::addNetworkMessage(const NetworkMessage& networkMsg)
{
if (!canAdd(networkMsg.getLength())) {
return;
}

addBytes(networkMsg.getBuffer() + INITIAL_BUFFER_POSITION, networkMsg.getLength());
}
Comment on lines +208 to +215
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a suggestion, you can abbreviate networkMsg to msg

11 changes: 10 additions & 1 deletion src/networkmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@ class NetworkMessage
return buffer[info.position++];
}

uint8_t getPreviousByte() { return buffer[--info.position]; }
// Returns first element of body
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: fix comment

uint8_t getPreviousByte()
{
if (info.position == INITIAL_BUFFER_POSITION) {
return buffer[INITIAL_BUFFER_POSITION];
}
return buffer[--info.position];
}

template <typename T>
std::enable_if_t<std::is_trivially_copyable_v<T>, T> get() noexcept
Expand Down Expand Up @@ -100,6 +107,7 @@ class NetworkMessage
}

void addBytes(const char* bytes, size_t size);
void addBytes(const uint8_t* bytes, size_t size);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid reinterpret_cast<const char*>

void addPaddingBytes(size_t n);

void addString(std::string_view value);
Expand All @@ -111,6 +119,7 @@ class NetworkMessage
void addItem(uint16_t id, uint8_t count);
void addItem(const Item* item);
void addItemId(uint16_t itemId);
void addNetworkMessage(const NetworkMessage& networkMsg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion above


MsgSize_t getLength() const { return info.length; }

Expand Down
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(tests_SRC
${CMAKE_CURRENT_LIST_DIR}/test_rsa.cpp
${CMAKE_CURRENT_LIST_DIR}/test_sha1.cpp
${CMAKE_CURRENT_LIST_DIR}/test_xtea.cpp
${CMAKE_CURRENT_LIST_DIR}/test_networkmessage.cpp
)

foreach(test_src ${tests_SRC})
Expand Down
121 changes: 121 additions & 0 deletions src/tests/test_networkmessage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#define BOOST_TEST_MODULE networkmessage

#include "../otpch.h"

#include "../networkmessage.h"
#include "../position.h"

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(test_networkmessage_reset)
{
uint8_t expected = 12;
uint8_t actual = 0;
NetworkMessage msg{};
msg.addByte(expected);
msg.setBufferPosition(0);
actual = msg.getByte();
BOOST_TEST(msg.getLength() == 1);
BOOST_TEST(msg.getBufferPosition() == 1 + NetworkMessage::INITIAL_BUFFER_POSITION);
msg.reset();
BOOST_TEST(msg.getLength() == 0);
BOOST_TEST(actual == expected);
}

BOOST_AUTO_TEST_CASE(test_networkmessage_getPreviousByte)
{
NetworkMessage msg{};
msg.addByte(11);
msg.addByte(22);
msg.addByte(33);
msg.addByte(44);
BOOST_TEST(msg.getLength() == 4);
BOOST_TEST(msg.getPreviousByte() == 44);
BOOST_TEST(msg.getPreviousByte() == 33);
BOOST_TEST(msg.getPreviousByte() == 22);
BOOST_TEST(msg.getPreviousByte() == 11);
// overflow case
BOOST_TEST(msg.getPreviousByte() == 11);
BOOST_TEST(msg.getBufferPosition() == NetworkMessage::INITIAL_BUFFER_POSITION);
}

BOOST_AUTO_TEST_CASE(test_networkmessage_add_get_template)
{
NetworkMessage msg{};
msg.add<uint16_t>(std::numeric_limits<uint16_t>::max());
msg.add<uint32_t>(std::numeric_limits<uint32_t>::max());
msg.add<uint64_t>(std::numeric_limits<uint64_t>::max());
msg.setBufferPosition(0);
BOOST_TEST(msg.getLength() == 14);
BOOST_TEST(msg.get<uint16_t>() == std::numeric_limits<uint16_t>::max());
BOOST_TEST(msg.get<uint32_t>() == std::numeric_limits<uint32_t>::max());
BOOST_TEST(msg.get<uint64_t>() == std::numeric_limits<uint64_t>::max());
}

BOOST_AUTO_TEST_CASE(test_networkmessage_string)
{
NetworkMessage msg{};
msg.addString("test");
msg.addString("Msg");
msg.setBufferPosition(0);
BOOST_TEST(msg.getLength() == 11);
BOOST_TEST(msg.getString() == "test");
BOOST_TEST(msg.getString() == "Msg");
}

BOOST_AUTO_TEST_CASE(test_networkmessage_position)
{
Position position1(11, 22, 3);
Position position2(111, 222, 4);
NetworkMessage msg{};
msg.addPosition(position1);
msg.addPosition(position2);
msg.setBufferPosition(0);
BOOST_TEST(msg.getLength() == 10);
BOOST_TEST(msg.getPosition() == position1);
BOOST_TEST(msg.getPosition() == position2);
}

BOOST_AUTO_TEST_CASE(test_networkmessage_skip)
{
NetworkMessage msg{};
msg.addByte(1);
msg.addByte(2);
msg.addByte(5);
msg.setBufferPosition(0);
msg.skipBytes(2);
BOOST_TEST(msg.getLength() == 3);
BOOST_TEST(msg.getByte() == 5);
}

BOOST_AUTO_TEST_CASE(test_networkmessage_byte)
{
NetworkMessage msg{};
msg.addByte(12);
msg.addByte(34);
msg.setBufferPosition(0);
BOOST_TEST(msg.getLength() == 2);
BOOST_TEST(msg.getByte() == 12);
BOOST_TEST(msg.getByte() == 34);
}

BOOST_AUTO_TEST_CASE(test_networkmessage_bytes)
{
uint8_t expected1[] = {1, 2, 3};
NetworkMessage msg1{};
msg1.addBytes(expected1, 3);
msg1.setBufferPosition(0);
BOOST_TEST(msg1.getLength() == 3);
BOOST_TEST(msg1.getByte() == expected1[0]);
BOOST_TEST(msg1.getByte() == expected1[1]);
BOOST_TEST(msg1.getByte() == expected1[2]);

const char* expected2 = {"abc"};
NetworkMessage msg2{};
msg2.addBytes(expected2, 3);
msg2.setBufferPosition(0);
BOOST_TEST(msg2.getLength() == 3);
BOOST_TEST(msg2.getByte() == expected2[0]);
BOOST_TEST(msg2.getByte() == expected2[1]);
BOOST_TEST(msg2.getByte() == expected2[2]);
}
Loading