websocket_message.hpp
Go to the documentation of this file.
1 #ifndef CPP_WEB_SERVER_WEBSOCKET_MESSAGE_HPP
2 #define CPP_WEB_SERVER_WEBSOCKET_MESSAGE_HPP
3 
4 #include <boost/asio.hpp>
5 #include <boost/logic/tribool.hpp>
6 #include <boost/shared_ptr.hpp>
7 #include <boost/tuple/tuple.hpp>
8 
9 #if defined(_MSC_VER)
10 # define PACKED_STRUCT(name) \
11  __pragma(pack(push, 1)) struct name __pragma(pack(pop))
12 #elif defined(__GNUC__)
13 # define PACKED_STRUCT(name) struct __attribute__((packed)) name
14 #else
15 # warning I don't know how to create a packed struct with your compiler
16 # define PACKED_STRUCT(name) struct name
17 #endif
18 
19 namespace async_web_server_cpp
20 {
21 
22 class WebsocketMessage;
23 
28 {
29 public:
30  PACKED_STRUCT(Header)
31  {
32  enum opcode
33  {
34  opcode_continuation = 0,
35  opcode_text = 1,
36  opcode_binary = 2,
37  opcode_close = 8,
38  opcode_ping = 9,
39  opcode_pong = 10,
40  } opcode : 4;
41  bool rsv3 : 1;
42  bool rsv2 : 1;
43  bool rsv1 : 1;
44  bool fin : 1;
45 
46  unsigned char len : 7;
47  bool mask : 1;
48  };
49  static_assert(sizeof(Header) == 2,
50  "the Header struct is not properly packed");
51  union
52  {
53  Header header;
54  char header_bytes[2];
55  };
56  uint64_t length;
57  unsigned char mask[4];
58  std::string content;
59 
60  bool fromMessage(const WebsocketMessage& message);
61  bool serialize(std::vector<unsigned char>& buffer);
62 };
63 
65 {
66 public:
68  void reset();
69  boost::tribool consume(WebsocketFrame& frame, char input);
70  template<typename InputIterator>
71  boost::tuple<boost::tribool, InputIterator>
72  parse(WebsocketFrame& frame, InputIterator begin, InputIterator end)
73  {
74  while (begin != end)
75  {
76  boost::tribool result = consume(frame, *begin++);
77  if (result || !result)
78  return boost::make_tuple(result, begin);
79  }
80  boost::tribool result = boost::indeterminate;
81  return boost::make_tuple(result, begin);
82  }
83 
84 private:
85  enum state
86  {
101  body
102  } state_;
103 };
104 
110 {
111 public:
113  enum type
114  {
121  } type;
122  std::string content;
123 };
124 
126 {
127 public:
128  boost::tribool consume(WebsocketMessage& message, WebsocketFrame& frame);
129 };
130 
131 } // namespace async_web_server_cpp
132 
133 #undef PACKED_STRUCT
134 
135 #endif
bool fromMessage(const WebsocketMessage &message)
bool serialize(std::vector< unsigned char > &buffer)
boost::tuple< boost::tribool, InputIterator > parse(WebsocketFrame &frame, InputIterator begin, InputIterator end)


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Mon Feb 28 2022 21:54:08