Skip to content

Commit

Permalink
Stop NetworkPacket methods from producing bloated packets
Browse files Browse the repository at this point in the history
  • Loading branch information
jayarndt authored and est31 committed May 4, 2015
1 parent 3ee854c commit 2923eaf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
24 changes: 6 additions & 18 deletions src/network/networkpacket.cpp
Expand Up @@ -72,8 +72,8 @@ char* NetworkPacket::getString(u32 from_offset)

void NetworkPacket::putRawString(const char* src, u32 len)
{
if (m_read_offset + len * sizeof(char) >= m_datasize) {
m_datasize += len * sizeof(char);
if (m_read_offset + len > m_datasize) {
m_datasize = m_read_offset + len;
m_data.resize(m_datasize);
}

Expand All @@ -95,7 +95,7 @@ NetworkPacket& NetworkPacket::operator>>(std::string& dst)
dst.reserve(strLen);
dst.append((char*)&m_data[m_read_offset], strLen);

m_read_offset += strLen * sizeof(char);
m_read_offset += strLen;
return *this;
}

Expand All @@ -108,13 +108,7 @@ NetworkPacket& NetworkPacket::operator<<(std::string src)

*this << msgsize;

if (m_read_offset + msgsize * sizeof(char) >= m_datasize) {
m_datasize += msgsize * sizeof(char);
m_data.resize(m_datasize);
}

memcpy(&m_data[m_read_offset], src.c_str(), msgsize);
m_read_offset += msgsize;
putRawString(src.c_str(), (u32)msgsize);

return *this;
}
Expand All @@ -128,13 +122,7 @@ void NetworkPacket::putLongString(std::string src)

*this << msgsize;

if (m_read_offset + msgsize * sizeof(char) >= m_datasize) {
m_datasize += msgsize * sizeof(char);
m_data.resize(m_datasize);
}

memcpy(&m_data[m_read_offset], src.c_str(), msgsize);
m_read_offset += msgsize;
putRawString(src.c_str(), msgsize);
}

NetworkPacket& NetworkPacket::operator>>(std::wstring& dst)
Expand Down Expand Up @@ -189,7 +177,7 @@ std::string NetworkPacket::readLongString()
dst.reserve(strLen);
dst.append((char*)&m_data[m_read_offset], strLen);

m_read_offset += strLen*sizeof(char);
m_read_offset += strLen;

return dst;
}
Expand Down
2 changes: 1 addition & 1 deletion src/network/networkpacket.h
Expand Up @@ -114,7 +114,7 @@ class NetworkPacket
template<typename T> void checkDataSize()
{
if (m_read_offset + sizeof(T) > m_datasize) {
m_datasize += sizeof(T);
m_datasize = m_read_offset + sizeof(T);
m_data.resize(m_datasize);
}
}
Expand Down

2 comments on commit 2923eaf

@nerzhul
Copy link
Member

@nerzhul nerzhul commented on 2923eaf May 5, 2015

Choose a reason for hiding this comment

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

does this commits have been tested in all situations ?

@est31
Copy link
Contributor

@est31 est31 commented on 2923eaf May 5, 2015

Choose a reason for hiding this comment

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

I haven't given this very thorough testing, but the new code looks much better than the old one. I have tested it however.

Please sign in to comment.