websocket_connection.cpp
Go to the documentation of this file.
2 
3 #include <boost/bind.hpp>
4 #include <boost/make_shared.hpp>
5 
6 #include <limits>
7 
8 namespace async_web_server_cpp
9 {
10 
12  : connection_(connection)
13 {
14 }
15 
17 {
18  handler_ = handler;
19 }
20 
21 bool WebsocketConnection::sendTextMessage(const std::string& content)
22 {
25  m.content = content;
26  return sendMessage(m);
27 }
28 bool WebsocketConnection::sendPingMessage(const std::string& content)
29 {
32  m.content = content;
33  return sendMessage(m);
34 }
35 
37 {
38  WebsocketFrame frame;
39  if (frame.fromMessage(message))
40  {
41  return sendFrame(frame);
42  }
43  return false;
44 }
45 
47 {
48  std::vector<unsigned char> buffer;
49  if (frame.serialize(buffer))
50  {
51  connection_->write_and_clear(buffer);
52  return true;
53  }
54  return false;
55 }
56 
58  WebsocketConnectionWeakPtr weak_this, const char* begin, const char* end)
59 {
60  WebsocketConnectionPtr _this = weak_this.lock();
61  if (_this)
62  _this->handle_read(begin, end);
63 }
64 void WebsocketConnection::handle_read(const char* begin, const char* end)
65 {
66  boost::tribool frame_result;
67  const char* parse_end = begin;
68  while (parse_end < end)
69  {
70  boost::tie(frame_result, parse_end) =
71  frame_parser_.parse(frame_, parse_end, end);
72  if (frame_result)
73  {
75  boost::tribool message_result =
77 
78  if (message_result)
79  {
80  if (handler_)
82  }
83  }
84  else if (!frame_result)
85  {
88  }
89  }
90  WebsocketConnectionWeakPtr this_weak(shared_from_this());
91  connection_->async_read(boost::bind(
92  &WebsocketConnection::static_handle_read, this_weak, _1, _2));
93 }
94 
95 } // namespace async_web_server_cpp
boost::function< void(const WebsocketMessage &message)> MessageHandler
bool fromMessage(const WebsocketMessage &message)
bool sendPingMessage(const std::string &content="")
boost::shared_ptr< WebsocketConnection > WebsocketConnectionPtr
boost::shared_ptr< HttpConnection > HttpConnectionPtr
void handle_read(const char *begin, const char *end)
boost::tribool consume(WebsocketMessage &message, WebsocketFrame &frame)
bool serialize(std::vector< unsigned char > &buffer)
static void static_handle_read(WebsocketConnectionWeakPtr weak_this, const char *begin, const char *end)
boost::weak_ptr< WebsocketConnection > WebsocketConnectionWeakPtr
bool sendMessage(const WebsocketMessage &message)
boost::tuple< boost::tribool, InputIterator > parse(WebsocketFrame &frame, InputIterator begin, InputIterator end)
void set_message_handler(MessageHandler &handler)
WebsocketConnection(HttpConnectionPtr connection)
bool sendTextMessage(const std::string &content)


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