Realistic 3D camera system
3D camera system components
chat_message.hpp
Go to the documentation of this file.
1 //
2 // chat_message.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 
19 {
20 public:
21  enum { header_length = 4 };
22  enum { max_body_length = 512 };
23 
25  : body_length_(0)
26  {
27  }
28 
29  const char* data() const
30  {
31  return data_;
32  }
33 
34  char* data()
35  {
36  return data_;
37  }
38 
39  size_t length() const
40  {
41  return header_length + body_length_;
42  }
43 
44  const char* body() const
45  {
46  return data_ + header_length;
47  }
48 
49  char* body()
50  {
51  return data_ + header_length;
52  }
53 
54  size_t body_length() const
55  {
56  return body_length_;
57  }
58 
59  void body_length(size_t new_length)
60  {
61  body_length_ = new_length;
62  if (body_length_ > max_body_length)
63  body_length_ = max_body_length;
64  }
65 
67  {
68  using namespace std; // For strncat and atoi.
69  char header[header_length + 1] = "";
70  strncat(header, data_, header_length);
71  body_length_ = atoi(header);
72  if (body_length_ > max_body_length)
73  {
74  body_length_ = 0;
75  return false;
76  }
77  return true;
78  }
79 
81  {
82  using namespace std; // For sprintf and memcpy.
83  char header[header_length + 1] = "";
84  sprintf(header, "%4d", static_cast<int>(body_length_));
85  memcpy(data_, header, header_length);
86  }
87 
88 private:
89  char data_[header_length + max_body_length];
90  size_t body_length_;
91 };
92 
93 #endif // CHAT_MESSAGE_HPP
const char * data() const
char * data()
const char * body() const
STL namespace.
bool decode_header()
void body_length(size_t new_length)
void encode_header()
size_t body_length() const
char * body()
size_t length() const