-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNetIncomingMessage.cpp
48 lines (40 loc) · 1023 Bytes
/
NetIncomingMessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <boost/assert.hpp>
#include "NetIncomingMessage.h"
#include "Logging.h"
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
NetIncomingMessage::NetIncomingMessage() :
m_messageType(0), m_body(0), m_bodyLength(0), m_readPosition(0)
{
m_header = new char[HEADER_LENGTH+1];
m_header[HEADER_LENGTH] = '\0';
}
NetIncomingMessage::~NetIncomingMessage()
{
delete m_header; m_header = 0;
delete[] m_header;
delete m_body; m_body = 0;
delete[] m_body;
}
void NetIncomingMessage::DecodeHeader()
{
// Header format:
// 0005,0000564538
// Read message type (4chars)
char messageTypeData[5] = "";
strncat(messageTypeData, m_header, 4);
m_messageType = atoi(messageTypeData);
// Read message length (10chars)
char bodyLengthData[11] = "";
strncat(bodyLengthData, m_header + 5, 10);
size_t bodyLength = atoi(bodyLengthData);
// Set m_body size
if (bodyLength != m_bodyLength)
{
delete m_body; m_body = 0;
delete[] m_body;
m_bodyLength = bodyLength;
m_body = new char[m_bodyLength];
}
}