21 virtual void send_binary(
const std::string& msg) = 0;
22 virtual void send_text(
const std::string& msg) = 0;
23 virtual void close(
const std::string& msg =
"quit") = 0;
27 template <
typename Adaptor>
46 std::string magic = req.
get_header_value(
"Sec-WebSocket-Key") +
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
48 s.processBytes(magic.data(), magic.size());
50 s.getDigestBytes(digest);
54 template<
typename CompletionHandler>
57 adaptor_.get_io_service().dispatch(handler);
60 template<
typename CompletionHandler>
61 void post(CompletionHandler handler)
63 adaptor_.get_io_service().post(handler);
69 char buf[3] =
"\x8A\x00";
97 void close(
const std::string& msg)
override 118 char buf[2+8] =
"\x80\x00";
125 else if (size < 0x10000)
128 *(uint16_t*)(buf+2) = (uint16_t)size;
134 *(uint64_t*)(buf+2) = (uint64_t)size;
135 return {buf, buf+10};
141 static std::string header =
"HTTP/1.1 101 Switching Protocols\r\n" 142 "Upgrade: websocket\r\n" 143 "Connection: Upgrade\r\n" 144 "Sec-WebSocket-Accept: ";
145 static std::string crlf =
"\r\n";
165 [
this](
const boost::system::error_code& ec, std::size_t bytes_transferred)
169 #ifdef CROW_ENABLE_DEBUG 171 if (!ec && bytes_transferred != 2)
173 throw std::runtime_error(
"WebSocket:MiniHeader:async_read fail:asio bug?");
209 [
this](
const boost::system::error_code& ec, std::size_t bytes_transferred)
213 #ifdef CROW_ENABLE_DEBUG 214 if (!ec && bytes_transferred != 2)
216 throw std::runtime_error(
"WebSocket:Len16:async_read fail:asio bug?");
239 [
this](
const boost::system::error_code& ec, std::size_t bytes_transferred)
243 #ifdef CROW_ENABLE_DEBUG 244 if (!ec && bytes_transferred != 8)
246 throw std::runtime_error(
"WebSocket:Len16:async_read fail:asio bug?");
267 boost::asio::async_read(
adaptor_.socket(), boost::asio::buffer((
char*)&
mask_, 4),
268 [
this](
const boost::system::error_code& ec, std::size_t bytes_transferred)
271 #ifdef CROW_ENABLE_DEBUG 272 if (!ec && bytes_transferred != 4)
274 throw std::runtime_error(
"WebSocket:Mask:async_read fail:asio bug?");
294 size_t to_read =
buffer_.size();
297 adaptor_.socket().async_read_some( boost::asio::buffer(
buffer_, to_read),
298 [
this](
const boost::system::error_code& ec, std::size_t bytes_transferred)
419 std::vector<boost::asio::const_buffer> buffers;
423 buffers.emplace_back(boost::asio::buffer(
s));
425 boost::asio::async_write(
adaptor_.socket(), buffers,
426 [&](
const boost::system::error_code& ec, std::size_t )
477 std::function<void(crow::websocket::connection&, const std::string&, bool)>
message_handler_;
478 std::function<void(crow::websocket::connection&, const std::string&)>
close_handler_;
Definition: TinySHA1.hpp:30
bool has_recv_close_
Definition: websocket.h:471
std::function< void(crow::websocket::connection &, const std::string &)> close_handler_
Definition: websocket.h:478
boost::array< char, 4096 > buffer_
Definition: websocket.h:460
std::function< void(crow::websocket::connection &)> error_handler_
Definition: websocket.h:479
Definition: http_request.h:23
bool pong_received_
Definition: websocket.h:473
std::string base64encode(const char *data, size_t size, const char *key="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
Definition: utility.h:503
void do_write()
Definition: websocket.h:414
void post(CompletionHandler handler)
Definition: websocket.h:61
uint16_t mini_header_
Definition: websocket.h:469
WebSocketReadState state_
Definition: websocket.h:464
std::string fragment_
Definition: websocket.h:463
std::function< void(crow::websocket::connection &)> open_handler_
Definition: websocket.h:476
Definition: block_queue.cpp:41
virtual ~connection()
Definition: websocket.h:24
void dispatch(CompletionHandler handler)
Definition: websocket.h:55
void close(const std::string &msg) override
Definition: websocket.h:97
Definition: websocket.h:28
bool is_binary_
Definition: websocket.h:461
virtual void send_binary(const std::string &msg)=0
uint64_t remaining_length_
Definition: websocket.h:465
const std::string & get_header_value(const std::string &key) const
Definition: http_request.h:50
void check_destroy()
Definition: websocket.h:445
virtual void send_text(const std::string &msg)=0
bool is_reading
Definition: websocket.h:467
bool is_FIN()
Definition: websocket.h:326
std::vector< std::string > write_buffers_
Definition: websocket.h:458
int opcode()
Definition: websocket.h:331
uint32_t mask_
Definition: websocket.h:468
WebSocketReadState
Definition: websocket.h:10
Definition: websocket.h:19
void send_text(const std::string &msg) override
Definition: websocket.h:87
std::vector< std::string > sending_buffers_
Definition: websocket.h:457
bool has_sent_close_
Definition: websocket.h:470
Adaptor adaptor_
Definition: websocket.h:455
std::string message_
Definition: websocket.h:462
void handle_fragment()
Definition: websocket.h:336
void send_pong(const std::string &msg)
Definition: websocket.h:66
std::function< void(crow::websocket::connection &, const std::string &, bool)> message_handler_
Definition: websocket.h:477
Connection(const crow::request &req, Adaptor &&adaptor, std::function< void(crow::websocket::connection &)> open_handler, std::function< void(crow::websocket::connection &, const std::string &, bool)> message_handler, std::function< void(crow::websocket::connection &, const std::string &)> close_handler, std::function< void(crow::websocket::connection &)> error_handler)
Definition: websocket.h:31
bool close_connection_
Definition: websocket.h:466
bool error_occured_
Definition: websocket.h:472
void send_binary(const std::string &msg) override
Definition: websocket.h:77
void start(std::string &&hello)
Definition: websocket.h:139
std::string build_header(int opcode, size_t size)
Definition: websocket.h:116
virtual void close(const std::string &msg="quit")=0
#define s(x, c)
Definition: aesb.c:46
void do_read()
Definition: websocket.h:156
bool is_close_handler_called_
Definition: websocket.h:474