]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/asio/example/cpp03/chat/chat_message.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / chat / chat_message.hpp
1 //
2 // chat_message.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // 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)
9 //
10
11 #ifndef CHAT_MESSAGE_HPP
12 #define CHAT_MESSAGE_HPP
13
14 #include <cstdio>
15 #include <cstdlib>
16 #include <cstring>
17
18 class chat_message
19 {
20 public:
21 enum
22 {
23 header_length = 4,
24 max_body_length = 512
25 };
26
27 chat_message()
28 : body_length_(0)
29 {
30 }
31
32 const char* data() const
33 {
34 return data_;
35 }
36
37 char* data()
38 {
39 return data_;
40 }
41
42 size_t length() const
43 {
44 return header_length + body_length_;
45 }
46
47 const char* body() const
48 {
49 return data_ + header_length;
50 }
51
52 char* body()
53 {
54 return data_ + header_length;
55 }
56
57 size_t body_length() const
58 {
59 return body_length_;
60 }
61
62 void body_length(size_t new_length)
63 {
64 body_length_ = new_length;
65 if (body_length_ > max_body_length)
66 body_length_ = max_body_length;
67 }
68
69 bool decode_header()
70 {
71 using namespace std; // For strncat and atoi.
72 char header[header_length + 1] = "";
73 strncat(header, data_, header_length);
74 body_length_ = atoi(header);
75 if (body_length_ > max_body_length)
76 {
77 body_length_ = 0;
78 return false;
79 }
80 return true;
81 }
82
83 void encode_header()
84 {
85 using namespace std; // For sprintf and memcpy.
86 char header[header_length + 1] = "";
87 sprintf(header, "%4d", static_cast<int>(body_length_));
88 memcpy(data_, header, header_length);
89 }
90
91 private:
92 char data_[header_length + max_body_length];
93 size_t body_length_;
94 };
95
96 #endif // CHAT_MESSAGE_HPP