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 2 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

2 changes: 2 additions & 0 deletions src/networkmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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 +112,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
Loading