Stop NetworkPacket methods from producing bloated packets

master
Jay Arndt 2015-05-04 04:53:41 -05:00 committed by est31
parent 3ee854cf25
commit 2923eafaca
2 changed files with 7 additions and 19 deletions

View File

@ -72,8 +72,8 @@ char* NetworkPacket::getString(u32 from_offset)
void NetworkPacket::putRawString(const char* src, u32 len) void NetworkPacket::putRawString(const char* src, u32 len)
{ {
if (m_read_offset + len * sizeof(char) >= m_datasize) { if (m_read_offset + len > m_datasize) {
m_datasize += len * sizeof(char); m_datasize = m_read_offset + len;
m_data.resize(m_datasize); m_data.resize(m_datasize);
} }
@ -95,7 +95,7 @@ NetworkPacket& NetworkPacket::operator>>(std::string& dst)
dst.reserve(strLen); dst.reserve(strLen);
dst.append((char*)&m_data[m_read_offset], strLen); dst.append((char*)&m_data[m_read_offset], strLen);
m_read_offset += strLen * sizeof(char); m_read_offset += strLen;
return *this; return *this;
} }
@ -108,13 +108,7 @@ NetworkPacket& NetworkPacket::operator<<(std::string src)
*this << msgsize; *this << msgsize;
if (m_read_offset + msgsize * sizeof(char) >= m_datasize) { putRawString(src.c_str(), (u32)msgsize);
m_datasize += msgsize * sizeof(char);
m_data.resize(m_datasize);
}
memcpy(&m_data[m_read_offset], src.c_str(), msgsize);
m_read_offset += msgsize;
return *this; return *this;
} }
@ -128,13 +122,7 @@ void NetworkPacket::putLongString(std::string src)
*this << msgsize; *this << msgsize;
if (m_read_offset + msgsize * sizeof(char) >= m_datasize) { putRawString(src.c_str(), msgsize);
m_datasize += msgsize * sizeof(char);
m_data.resize(m_datasize);
}
memcpy(&m_data[m_read_offset], src.c_str(), msgsize);
m_read_offset += msgsize;
} }
NetworkPacket& NetworkPacket::operator>>(std::wstring& dst) NetworkPacket& NetworkPacket::operator>>(std::wstring& dst)
@ -189,7 +177,7 @@ std::string NetworkPacket::readLongString()
dst.reserve(strLen); dst.reserve(strLen);
dst.append((char*)&m_data[m_read_offset], strLen); dst.append((char*)&m_data[m_read_offset], strLen);
m_read_offset += strLen*sizeof(char); m_read_offset += strLen;
return dst; return dst;
} }

View File

@ -114,7 +114,7 @@ private:
template<typename T> void checkDataSize() template<typename T> void checkDataSize()
{ {
if (m_read_offset + sizeof(T) > m_datasize) { if (m_read_offset + sizeof(T) > m_datasize) {
m_datasize += sizeof(T); m_datasize = m_read_offset + sizeof(T);
m_data.resize(m_datasize); m_data.resize(m_datasize);
} }
} }