-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.cpp
More file actions
98 lines (66 loc) · 1.59 KB
/
Message.cpp
File metadata and controls
98 lines (66 loc) · 1.59 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "Message.h"
#include <iostream>
Message::Message()
:
m_lengthBody( 0 )
{
}
const char* Message::Data() const
{
Logger::__instance().LogLine( "Message::Data()" );
return m_data;
}
char* Message::Data()
{
Logger::__instance().LogLine( "Message::Data()" );
return m_data;
}
std::size_t Message::GetLengthFull() const
{
Logger::__instance().LogLine( "Message::GetLengthFull()" );
return LENGTH_HEADER + m_lengthBody;
}
const char* Message::Body() const
{
Logger::__instance().LogLine( "Message::Body()" );
return LENGTH_HEADER + m_data;
}
char* Message::Body()
{
Logger::__instance().LogLine( "Message::Body()" );
return LENGTH_HEADER + m_data;
}
std::size_t Message::GetLengthBody() const
{
Logger::__instance().LogLine( "Message::GetLengthBody()" );
return m_lengthBody;
}
void Message::SetLengthBody( std::size_t length )
{
Logger::__instance().LogLine( "Message::SetLengthBody()" );
m_lengthBody = ( length > LENGTH_BODY_MAX ) ? LENGTH_BODY_MAX : length;
}
bool Message::DecodeHeader()
{
Logger::__instance().LogLine( "Message::DecodeHeader()" );
char header[ LENGTH_HEADER + 1 ] = "";
std::strncat( header,m_data,LENGTH_HEADER );
m_lengthBody = std::atoi( header );
if( m_lengthBody > LENGTH_BODY_MAX )
{
m_lengthBody = 0;
return false;
}
return true;
}
void Message::EncodeHeader()
{
Logger::__instance().LogLine( "Message::EncodeHeader()" );
char header[ LENGTH_HEADER + 1 ] = "";
std::sprintf( header,"%4d",static_cast<int>( m_lengthBody ) );
std::memcpy( m_data,header,LENGTH_HEADER );
}
void Message::Clear()
{
memset( m_data,0,sizeof m_data );
}