WIP Clients are working properly, servers are not.

This commit is contained in:
Jocke
2016-02-09 11:43:16 +01:00
parent ba9419b342
commit fccd8c64e7
19 changed files with 277 additions and 166 deletions
+32 -9
View File
@@ -37,6 +37,7 @@ void Packet::Init(MessageType type, unsigned int & packetID)
// allocate memory for size of packet(only used in tcp)
Packet::WritePrimitive<int>(0);
// Add message type
m_MessageType = type;
int messageType = static_cast<int>(type);
Packet::WritePrimitive<int>(messageType);
Packet::WritePrimitive<int>(packetID);
@@ -58,9 +59,12 @@ void Packet::WriteString(const std::string& str)
void Packet::WriteData(char * data, int sizeOfData)
{
if (m_Offset + sizeOfData > m_MaxPacketSize) {
//LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size. New size is %i bytes\n", m_MaxPacketSize*2);
resizeData();
while (m_Offset + sizeOfData > m_MaxPacketSize) {
resizeData();
}
}
memcpy(m_Data + m_Offset, data, sizeOfData);
m_Offset += sizeOfData;
@@ -78,19 +82,34 @@ std::string Packet::ReadString()
return returnValue;
}
void Packet::ReconstructFromData(char * data, int sizeOfData)
{
if (sizeOfData > m_MaxPacketSize) {
// Delete our data
delete[] m_Data;
// Set new max size
m_MaxPacketSize = sizeOfData;
m_Data = new char[m_MaxPacketSize];
// while we resized the old data container.
}
memcpy(m_Data, data, sizeOfData);
m_Offset = sizeOfData;
}
void Packet::UpdateSize()
{
{
memcpy(m_Data, &m_Offset, sizeof(int));
}
char * Packet::ReadData(int SizeOfData)
char * Packet::ReadData(int sizeOfData)
{
if (m_Offset < m_ReturnDataOffset + SizeOfData) {
if (m_Offset < m_ReturnDataOffset + sizeOfData) {
//LOG_WARNING("packet ReadData(): Oh no! You are trying to remove things outside my memory kingdom");
return nullptr;
}
unsigned int oldReturnDataOffset = m_ReturnDataOffset;
m_ReturnDataOffset += SizeOfData;
m_ReturnDataOffset += sizeOfData;
return (m_Data + oldReturnDataOffset);
}
@@ -103,20 +122,24 @@ void Packet::ChangePacketID(unsigned int & packetID)
void Packet::resizeData()
{
resizeData(m_MaxPacketSize * 2);
}
void Packet::resizeData(int size)
{
// Allocate memory to store our data in
char* holdData = new char[m_MaxPacketSize];
// Copy our data to the newly allocated memory
memcpy(holdData, m_Data, m_Offset);
// Increase max packet size
m_MaxPacketSize = m_MaxPacketSize * 2;
m_MaxPacketSize = size;
// Delete our data
delete m_Data;
// Allocate twice the memory we had before
delete[] m_Data;
// Allocate memory
m_Data = new char[m_MaxPacketSize];
// Copy our data to new location
memcpy(m_Data, holdData, m_Offset);
// Delete the memory allocated to hold our data
// while we resized the old data container.
delete holdData;
delete[] holdData;
}