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-​2022·​Christopher·​M.​·​Kohlhoff·​(chris·​at·​kohlhoff·​dot·​com)​5 /​/​·​Copyright·​(c)​·​2003-​2022·​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 ··​enum21 ··static·constexpr·std:​:​size_t·header_length·=·4;​
22 ··{22 ··static·constexpr·std:​:​size_t·max_body_length·=·512;​
23 ····header_length·=·4,​
24 ····max_body_length·=·512
25 ··};​
26 23
27 ··​chat_message()​24 ··​chat_message()​
28 ····​:​·​body_length_(0)​25 ····​:​·​body_length_(0)​
29 ··​{26 ··​{
30 ··​}27 ··​}
31 28
32 ··​const·​char*·​data()​·​const29 ··​const·​char*·​data()​·​const
33 ··​{30 ··​{
34 ····​return·​data_;​31 ····​return·​data_;​
35 ··​}32 ··​}
36 33
37 ··​char*·​data()​34 ··​char*·​data()​
38 ··​{35 ··​{
39 ····​return·​data_;​36 ····​return·​data_;​
40 ··​}37 ··​}
41 38
42 ··​size_t·​length()​·​const39 ··std:​:​size_t·​length()​·​const
43 ··​{40 ··​{
44 ····​return·​header_length·​+·​body_length_;​41 ····​return·​header_length·​+·​body_length_;​
45 ··​}42 ··​}
46 43
47 ··​const·​char*·​body()​·​const44 ··​const·​char*·​body()​·​const
48 ··​{45 ··​{
49 ····​return·​data_·​+·​header_length;​46 ····​return·​data_·​+·​header_length;​
50 ··​}47 ··​}
51 48
52 ··​char*·​body()​49 ··​char*·​body()​
53 ··​{50 ··​{
54 ····​return·​data_·​+·​header_length;​51 ····​return·​data_·​+·​header_length;​
55 ··​}52 ··​}
56 53
57 ··​size_t·​body_length()​·​const54 ··std:​:​size_t·​body_length()​·​const
58 ··​{55 ··​{
59 ····​return·​body_length_;​56 ····​return·​body_length_;​
60 ··​}57 ··​}
61 58
62 ··​void·​body_length(size_t·​new_length)​59 ··​void·​body_length(std:​:​size_t·​new_length)​
63 ··​{60 ··​{
64 ····​body_length_·​=·​new_length;​61 ····​body_length_·​=·​new_length;​
65 ····​if·​(body_length_·​>·​max_body_length)​62 ····​if·​(body_length_·​>·​max_body_length)​
66 ······​body_length_·​=·​max_body_length;​63 ······​body_length_·​=·​max_body_length;​
67 ··​}64 ··​}
68 65
69 ··​bool·​decode_header()​66 ··​bool·​decode_header()​
70 ··​{67 ··​{
71 ····​using·​namespace·​std;​·​/​/​·​For·​strncat·​and·​atoi.​
72 ····​char·​header[header_length·​+·​1]·​=·​"";​68 ····​char·​header[header_length·​+·​1]·​=·​"";​
73 ····​strncat(header,​·​data_,​·​header_length)​;​69 ····std:​:​strncat(header,​·​data_,​·​header_length)​;​
74 ····​body_length_·​=·​atoi(header)​;​70 ····​body_length_·​=·std:​:​atoi(header)​;​
75 ····​if·​(body_length_·​>·​max_body_length)​71 ····​if·​(body_length_·​>·​max_body_length)​
76 ····​{72 ····​{
77 ······​body_length_·​=·​0;​73 ······​body_length_·​=·​0;​
78 ······​return·​false;​74 ······​return·​false;​
79 ····​}75 ····​}
80 ····​return·​true;​76 ····​return·​true;​
81 ··​}77 ··​}
82 78
83 ··​void·​encode_header()​79 ··​void·​encode_header()​
84 ··​{80 ··​{
85 ····​using·​namespace·​std;​·​/​/​·​For·​sprintf·​and·​memcpy.​
86 ····​char·​header[header_length·​+·​1]·​=·​"";​81 ····​char·​header[header_length·​+·​1]·​=·​"";​
87 ····​sprintf(header,​·​"%4d",​·​static_cast<int>(body​_length_)​)​;​82 ····std:​:​sprintf(header,​·​"%4d",​·​static_cast<int>(body​_length_)​)​;​
88 ····​memcpy(data_,​·​header,​·​header_length)​;​83 ····std:​:​memcpy(data_,​·​header,​·​header_length)​;​
89 ··​}84 ··​}
90 85
91 private:​86 private:​
92 ··​char·​data_[header_length·​+·​max_body_length];​87 ··​char·​data_[header_length·​+·​max_body_length];​
93 ··​size_t·​body_length_;​88 ··std:​:​size_t·​body_length_;​
94 };​89 };​
95 90
96 #endif·​/​/​·​CHAT_MESSAGE_HPP91 #endif·​/​/​·​CHAT_MESSAGE_HPP