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
async_web_server_cpp::WebsocketConnection::sendTextMessage
bool sendTextMessage(const std::string &content)
Definition: websocket_connection.cpp:21
async_web_server_cpp::WebsocketFrame::fromMessage
bool fromMessage(const WebsocketMessage &message)
Definition: websocket_message.cpp:6
async_web_server_cpp::WebsocketConnection::handler_
MessageHandler handler_
Definition: websocket_connection.hpp:49
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::WebsocketConnection::static_handle_read
static void static_handle_read(WebsocketConnectionWeakPtr weak_this, const char *begin, const char *end)
Definition: websocket_connection.cpp:57
async_web_server_cpp::WebsocketConnection::frame_parser_
WebsocketFrameParser frame_parser_
Definition: websocket_connection.hpp:53
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::WebsocketConnection::frame_buffer_
WebsocketFrameBuffer frame_buffer_
Definition: websocket_connection.hpp:54
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::WebsocketConnectionWeakPtr
boost::weak_ptr< WebsocketConnection > WebsocketConnectionWeakPtr
Definition: websocket_connection.hpp:20
async_web_server_cpp::WebsocketConnectionPtr
boost::shared_ptr< WebsocketConnection > WebsocketConnectionPtr
Definition: websocket_connection.hpp:18
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::WebsocketConnection::handle_read
void handle_read(const char *begin, const char *end)
Definition: websocket_connection.cpp:64
async_web_server_cpp::WebsocketConnection::frame_
WebsocketFrame frame_
Definition: websocket_connection.hpp:51
async_web_server_cpp::WebsocketMessage::type_unknown
@ type_unknown
Definition: websocket_message.hpp:115
async_web_server_cpp::HttpConnectionPtr
boost::shared_ptr< HttpConnection > HttpConnectionPtr
Definition: http_connection.hpp:17
async_web_server_cpp::WebsocketConnection::WebsocketConnection
WebsocketConnection(HttpConnectionPtr connection)
Definition: websocket_connection.cpp:11
async_web_server_cpp::WebsocketConnection::message_
WebsocketMessage message_
Definition: websocket_connection.hpp:52
async_web_server_cpp::WebsocketMessage
Definition: websocket_message.hpp:109
async_web_server_cpp::WebsocketFrameBuffer::consume
boost::tribool consume(WebsocketMessage &message, WebsocketFrame &frame)
Definition: websocket_message.cpp:208
async_web_server_cpp::WebsocketMessage::content
std::string content
Definition: websocket_message.hpp:122
async_web_server_cpp::WebsocketMessage::type_text
@ type_text
Definition: websocket_message.hpp:116
websocket_connection.hpp
async_web_server_cpp::WebsocketConnection::sendFrame
bool sendFrame(WebsocketFrame &frame)
Definition: websocket_connection.cpp:46
async_web_server_cpp::WebsocketConnection::MessageHandler
boost::function< void(const WebsocketMessage &message)> MessageHandler
Definition: websocket_connection.hpp:34
async_web_server_cpp::WebsocketConnection::set_message_handler
void set_message_handler(MessageHandler &handler)
Definition: websocket_connection.cpp:16
async_web_server_cpp::WebsocketConnection::connection_
HttpConnectionPtr connection_
Definition: websocket_connection.hpp:46
async_web_server_cpp::WebsocketConnection::sendPingMessage
bool sendPingMessage(const std::string &content="")
Definition: websocket_connection.cpp:28
async_web_server_cpp::WebsocketConnection::sendMessage
bool sendMessage(const WebsocketMessage &message)
Definition: websocket_connection.cpp:36


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