src/​examples/​cpp03/​chat/​chat_message.​hppsrc/​examples/​cpp11/​chat/​chat_message.​hpp
1 /​/​1 /​/​
2 /​/​·​chat_message.​hpp2 /​/​·​chat_message.​hpp
3 /​/​·​~~~~~~~~~~~~~~~~3 /​/​·​~~~~~~~~~~~~~~~~
4 /​/​4 /​/​
5 /​/​·​Copyright·​(c)​·​2003-​2020·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2020·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​
6 /​/​6 /​/​
7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying7 /​/​·​Distributed·​under·​the·​Boost·​Software·​License,​·​Version·​1.​0.​·​(See·​accompanying
8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​8 /​/​·​file·​LICENSE_1_0.​txt·​or·​copy·​at·​http:​/​/​www.​boost.​org/​LICENSE_1_0.​txt)​
9 /​/​9 /​/​
10 10
11 #ifndef·​CHAT_MESSAGE_HPP11 #ifndef·​CHAT_MESSAGE_HPP
12 #define·​CHAT_MESSAGE_HPP12 #define·​CHAT_MESSAGE_HPP
13 13
14 #include·​<cstdio>14 #include·​<cstdio>
15 #include·​<cstdlib>15 #include·​<cstdlib>
16 #include·​<cstring>16 #include·​<cstring>
17 17
18 class·​chat_message18 class·​chat_message
19 {19 {
20 public:​20 public:​
21 ··​enum·​{·​header_length·​=·​4·​};​21 ··​enum·​{·​header_length·​=·​4·​};​
22 ··​enum·​{·​max_body_length·​=·​512·​};​22 ··​enum·​{·​max_body_length·​=·​512·​};​
23 23
24 ··​chat_message()​24 ··​chat_message()​
25 ····​:​·​body_length_(0)​25 ····​:​·​body_length_(0)​
26 ··​{26 ··​{
27 ··​}27 ··​}
28 28
29 ··​const·​char*·​data()​·​const29 ··​const·​char*·​data()​·​const
30 ··​{30 ··​{
31 ····​return·​data_;​31 ····​return·​data_;​
32 ··​}32 ··​}
33 33
34 ··​char*·​data()​34 ··​char*·​data()​
35 ··​{35 ··​{
36 ····​return·​data_;​36 ····​return·​data_;​
37 ··​}37 ··​}
38 38
39 ··​size_t·​length()​·​const39 ··std:​:​size_t·​length()​·​const
40 ··​{40 ··​{
41 ····​return·​header_length·​+·​body_length_;​41 ····​return·​header_length·​+·​body_length_;​
42 ··​}42 ··​}
43 43
44 ··​const·​char*·​body()​·​const44 ··​const·​char*·​body()​·​const
45 ··​{45 ··​{
46 ····​return·​data_·​+·​header_length;​46 ····​return·​data_·​+·​header_length;​
47 ··​}47 ··​}
48 48
49 ··​char*·​body()​49 ··​char*·​body()​
50 ··​{50 ··​{
51 ····​return·​data_·​+·​header_length;​51 ····​return·​data_·​+·​header_length;​
52 ··​}52 ··​}
53 53
54 ··​size_t·​body_length()​·​const54 ··std:​:​size_t·​body_length()​·​const
55 ··​{55 ··​{
56 ····​return·​body_length_;​56 ····​return·​body_length_;​
57 ··​}57 ··​}
58 58
59 ··​void·​body_length(size_t·​new_length)​59 ··​void·​body_length(std:​:​size_t·​new_length)​
60 ··​{60 ··​{
61 ····​body_length_·​=·​new_length;​61 ····​body_length_·​=·​new_length;​
62 ····​if·​(body_length_·​>·​max_body_length)​62 ····​if·​(body_length_·​>·​max_body_length)​
63 ······​body_length_·​=·​max_body_length;​63 ······​body_length_·​=·​max_body_length;​
64 ··​}64 ··​}
65 65
66 ··​bool·​decode_header()​66 ··​bool·​decode_header()​
67 ··​{67 ··​{
68 ····​using·​namespace·​std;​·​/​/​·​For·​strncat·​and·​atoi.​
69 ····​char·​header[header_length·​+·​1]·​=·​"";​68 ····​char·​header[header_length·​+·​1]·​=·​"";​
70 ····​strncat(header,​·​data_,​·​header_length)​;​69 ····std:​:​strncat(header,​·​data_,​·​header_length)​;​
71 ····​body_length_·​=·​atoi(header)​;​70 ····​body_length_·​=·std:​:​atoi(header)​;​
72 ····​if·​(body_length_·​>·​max_body_length)​71 ····​if·​(body_length_·​>·​max_body_length)​
73 ····​{72 ····​{
74 ······​body_length_·​=·​0;​73 ······​body_length_·​=·​0;​
75 ······​return·​false;​74 ······​return·​false;​
76 ····​}75 ····​}
77 ····​return·​true;​76 ····​return·​true;​
78 ··​}77 ··​}
79 78
80 ··​void·​encode_header()​79 ··​void·​encode_header()​
81 ··​{80 ··​{
82 ····​using·​namespace·​std;​·​/​/​·​For·​sprintf·​and·​memcpy.​
83 ····​char·​header[header_length·​+·​1]·​=·​"";​81 ····​char·​header[header_length·​+·​1]·​=·​"";​
84 ····​sprintf(header,​·​"%4d",​·​static_cast<int>(body​_length_)​)​;​82 ····std:​:​sprintf(header,​·​"%4d",​·​static_cast<int>(body​_length_)​)​;​
85 ····​memcpy(data_,​·​header,​·​header_length)​;​83 ····std:​:​memcpy(data_,​·​header,​·​header_length)​;​
86 ··​}84 ··​}
87 85
88 private:​86 private:​
89 ··​char·​data_[header_length·​+·​max_body_length];​87 ··​char·​data_[header_length·​+·​max_body_length];​
90 ··​size_t·​body_length_;​88 ··std:​:​size_t·​body_length_;​
91 };​89 };​
92 90
93 #endif·​/​/​·​CHAT_MESSAGE_HPP91 #endif·​/​/​·​CHAT_MESSAGE_HPP