-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: master
Are you sure you want to change the base?
Adding network msg to network msg #4878
Conversation
src/networkmessage.cpp
Outdated
@@ -191,3 +191,19 @@ void NetworkMessage::addItem(const Item* item) | |||
} | |||
|
|||
void NetworkMessage::addItemId(uint16_t itemId) { add<uint16_t>(Item::items[itemId].clientId); } | |||
|
|||
void NetworkMessage::addNetworkMessage(NetworkMessage* networkMsg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probaly can be changed from:
void NetworkMessage::addNetworkMessage(NetworkMessage* networkMsg)
to:
void NetworkMessage::addNetworkMessage(NetworkMessage networkMsg)
src/networkmessage.cpp
Outdated
return; | ||
} | ||
|
||
std::memcpy(buffer.data() + info.position, networkMsg->getBuffer() + INITIAL_BUFFER_POSITION, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldnt addBytes from networkmessage class suffice here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, i will check.
src/luascript.cpp
Outdated
@@ -6293,6 +6294,26 @@ int LuaScriptInterface::luaNetworkMessageAddItemId(lua_State* L) | |||
return 1; | |||
} | |||
|
|||
int LuaScriptInterface::luaNetworkMessageAddNetworkMessage(lua_State* L) | |||
{ | |||
// networkMessage:addNetworkMessage(msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
networkMessage:compose(otherMsg)?
@@ -100,6 +100,7 @@ class NetworkMessage | |||
} | |||
|
|||
void addBytes(const char* bytes, size_t size); | |||
void addBytes(const uint8_t* bytes, size_t size); |
There was a problem hiding this comment.
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*>
@@ -53,7 +53,14 @@ class NetworkMessage | |||
return buffer[info.position++]; | |||
} | |||
|
|||
uint8_t getPreviousByte() { return buffer[--info.position]; } | |||
// Returns first element of body |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
todo: fix comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not usually a fan of returning a bool just for success or failure, but in this case, there’s a failure when adding the message. Lua won’t actually receive this return, check it out:
void NetworkMessage::addNetworkMessage(const NetworkMessage& networkMsg) | ||
{ | ||
if (!canAdd(networkMsg.getLength())) { | ||
return; | ||
} | ||
|
||
addBytes(networkMsg.getBuffer() + INITIAL_BUFFER_POSITION, networkMsg.getLength()); | ||
} |
There was a problem hiding this comment.
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
@@ -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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion above
message->addNetworkMessage(*msgToAdd); | ||
tfs::lua::pushBoolean(L, true); | ||
return 1; | ||
} |
There was a problem hiding this comment.
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
Pull Request Prelude
NetworkMessage functionality extension.
Changes Proposed
Added ability to append a NetworkMessage to another NetworkMessage, which allows caching of parts that are repeated in the message.
Issues addressed:
suggestion: caching NetworkMessage blocks #4450
How to test:
expected output