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  {
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
async_web_server_cpp::WebsocketFrame::fromMessage
bool fromMessage(const WebsocketMessage &message)
Definition: websocket_message.cpp:6
async_web_server_cpp::WebsocketFrame::serialize
bool serialize(std::vector< unsigned char > &buffer)
Definition: websocket_message.cpp:37
async_web_server_cpp::WebsocketFrameParser::reset
void reset()
Definition: websocket_message.cpp:91
async_web_server_cpp::WebsocketMessage::type_binary
@ type_binary
Definition: websocket_message.hpp:117
async_web_server_cpp::WebsocketFrame::header_bytes
char header_bytes[2]
Definition: websocket_message.hpp:54
async_web_server_cpp::WebsocketFrameParser::mask_byte4
@ mask_byte4
Definition: websocket_message.hpp:100
async_web_server_cpp::WebsocketFrameParser::state_
enum async_web_server_cpp::WebsocketFrameParser::state state_
async_web_server_cpp::WebsocketFrameParser::state
state
Definition: websocket_message.hpp:85
async_web_server_cpp::WebsocketFrameParser::length_7bytes_left
@ length_7bytes_left
Definition: websocket_message.hpp:90
async_web_server_cpp::WebsocketMessage::type_ping
@ type_ping
Definition: websocket_message.hpp:119
async_web_server_cpp::WebsocketFrame
Definition: websocket_message.hpp:27
async_web_server_cpp::WebsocketFrameParser::body
@ body
Definition: websocket_message.hpp:101
async_web_server_cpp::WebsocketFrame::length
uint64_t length
Definition: websocket_message.hpp:56
async_web_server_cpp::WebsocketMessage::WebsocketMessage
WebsocketMessage()
Definition: websocket_message.cpp:206
async_web_server_cpp::WebsocketFrameBuffer
Definition: websocket_message.hpp:125
async_web_server_cpp::WebsocketFrameParser::length_5bytes_left
@ length_5bytes_left
Definition: websocket_message.hpp:92
async_web_server_cpp
Definition: http_connection.hpp:14
async_web_server_cpp::WebsocketMessage::type
type
Definition: websocket_message.hpp:113
async_web_server_cpp::WebsocketFrame::content
std::string content
Definition: websocket_message.hpp:58
async_web_server_cpp::WebsocketFrameParser::length_6bytes_left
@ length_6bytes_left
Definition: websocket_message.hpp:91
async_web_server_cpp::WebsocketMessage::type_pong
@ type_pong
Definition: websocket_message.hpp:120
async_web_server_cpp::WebsocketFrameParser::length_4bytes_left
@ length_4bytes_left
Definition: websocket_message.hpp:93
async_web_server_cpp::WebsocketMessage::type_close
@ type_close
Definition: websocket_message.hpp:118
async_web_server_cpp::WebsocketFrame::header
Header header
Definition: websocket_message.hpp:53
async_web_server_cpp::WebsocketFrameParser::parse
boost::tuple< boost::tribool, InputIterator > parse(WebsocketFrame &frame, InputIterator begin, InputIterator end)
Definition: websocket_message.hpp:72
async_web_server_cpp::WebsocketFrameParser::consume
boost::tribool consume(WebsocketFrame &frame, char input)
Definition: websocket_message.cpp:96
async_web_server_cpp::WebsocketFrameParser::length_3bytes_left
@ length_3bytes_left
Definition: websocket_message.hpp:94
async_web_server_cpp::WebsocketMessage::type_unknown
@ type_unknown
Definition: websocket_message.hpp:115
async_web_server_cpp::WebsocketFrameParser::header_byte1
@ header_byte1
Definition: websocket_message.hpp:87
async_web_server_cpp::WebsocketFrameParser::header_byte2
@ header_byte2
Definition: websocket_message.hpp:88
async_web_server_cpp::WebsocketFrameParser::length_2bytes_left
@ length_2bytes_left
Definition: websocket_message.hpp:95
async_web_server_cpp::WebsocketMessage
Definition: websocket_message.hpp:109
async_web_server_cpp::WebsocketFrame::PACKED_STRUCT
PACKED_STRUCT(Header)
Definition: websocket_message.hpp:30
async_web_server_cpp::WebsocketFrameParser::mask_byte1
@ mask_byte1
Definition: websocket_message.hpp:97
async_web_server_cpp::WebsocketFrameParser::mask_byte2
@ mask_byte2
Definition: websocket_message.hpp:98
async_web_server_cpp::WebsocketFrameBuffer::consume
boost::tribool consume(WebsocketMessage &message, WebsocketFrame &frame)
Definition: websocket_message.cpp:208
async_web_server_cpp::WebsocketFrameParser
Definition: websocket_message.hpp:64
async_web_server_cpp::WebsocketFrameParser::mask_byte3
@ mask_byte3
Definition: websocket_message.hpp:99
async_web_server_cpp::WebsocketMessage::content
std::string content
Definition: websocket_message.hpp:122
async_web_server_cpp::WebsocketFrameParser::length_1bytes_left
@ length_1bytes_left
Definition: websocket_message.hpp:96
async_web_server_cpp::WebsocketMessage::type_text
@ type_text
Definition: websocket_message.hpp:116
async_web_server_cpp::WebsocketFrameParser::WebsocketFrameParser
WebsocketFrameParser()
Definition: websocket_message.cpp:86
async_web_server_cpp::WebsocketFrame::mask
unsigned char mask[4]
Definition: websocket_message.hpp:57
async_web_server_cpp::WebsocketFrameParser::length_8bytes_left
@ length_8bytes_left
Definition: websocket_message.hpp:89


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Tue Mar 1 2022 23:49:06