5 // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef CHAT_MESSAGE_HPP
12 #define CHAT_MESSAGE_HPP
21 enum { header_length = 4 };
22 enum { max_body_length = 512 };
29 const char* data() const
41 return header_length + body_length_;
44 const char* body() const
46 return data_ + header_length;
51 return data_ + header_length;
54 size_t body_length() const
59 void body_length(size_t new_length)
61 body_length_ = new_length;
62 if (body_length_ > max_body_length)
63 body_length_ = max_body_length;
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)
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);
89 char data_[header_length + max_body_length];
93 #endif // CHAT_MESSAGE_HPP