packio
websocket.h
Go to the documentation of this file.
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4 
5 #ifndef PACKIO_EXTRA_WEBSOCKET_H
6 #define PACKIO_EXTRA_WEBSOCKET_H
7 
14 
15 #include "../internal/config.h"
16 #include "../internal/utils.h"
17 
18 #include <boost/beast/core.hpp>
19 #include <boost/beast/websocket.hpp>
20 
21 namespace packio {
22 namespace extra {
23 
28 template <typename Websocket, bool kBinary = false>
29 class websocket_adapter : public Websocket {
30 public:
31  using protocol_type = typename Websocket::next_layer_type::protocol_type;
32 
33  template <typename... Args>
34  websocket_adapter(Args&&... args) : Websocket(std::forward<Args>(args)...)
35  {
36  Websocket::binary(kBinary);
37  }
38 
40  template <typename Buffer, typename Handler>
41  auto async_write_some(Buffer&& buffer, Handler&& handler)
42  {
43  internal::set_no_delay(boost::beast::get_lowest_layer(*this).socket());
44  return Websocket::async_write(
45  std::forward<Buffer>(buffer), std::forward<Handler>(handler));
46  }
47 
50  template <typename... Args>
51  auto close(Args&&... args)
52  {
53  return boost::beast::get_lowest_layer(*this).socket().close(
54  std::forward<Args>(args)...);
55  }
56 
59  template <typename... Args>
60  auto cancel(Args&&...)
61  {
62  static_assert(
63  internal::always_false_v<Args...>,
64  "websockets do not support cancel operations");
65  }
66 };
67 
73 template <typename Acceptor, typename WebsocketAdapter>
74 class websocket_acceptor_adapter : public Acceptor {
75 public:
76  using Acceptor::Acceptor;
77 
79  WebsocketAdapter accept();
80 
85  template <typename Handler>
86  void async_accept(Handler&& handler)
87  {
88  Acceptor::async_accept([handler = std::forward<Handler>(handler)](
89  auto ec, auto sock) mutable {
90  if (ec) {
91  handler(ec, WebsocketAdapter(std::move(sock)));
92  return;
93  }
94  auto ws = std::make_unique<WebsocketAdapter>(std::move(sock));
95  ws->set_option(boost::beast::websocket::stream_base::timeout::suggested(
97  ws->async_accept([handler = std::forward<Handler>(handler),
98  ws = std::move(ws)](auto ec) mutable {
99  handler(ec, std::move(*ws));
100  });
101  });
102  }
103 };
104 
105 } // extra
106 } // packio
107 
108 #endif // PACKIO_EXTRA_WEBSOCKET_H
Adapter class to support websockets servers.
Definition: websocket.h:74
void async_accept(Handler &&handler)
Accept the low level connection and configure the recommended timeout settings.
Definition: websocket.h:86
WebsocketAdapter accept()
Don't need definition, but declaration is used to determine the socket type.
Adapter class to support websockets.
Definition: websocket.h:29
auto cancel(Args &&...)
Cancel is not possible on websockets, raise a compile-time error is the user tries to use it.
Definition: websocket.h:60
auto async_write_some(Buffer &&buffer, Handler &&handler)
Set the no_delay option on the lowest layer and Write a websocket message.
Definition: websocket.h:41
auto close(Args &&... args)
Close the lowest layer, closing a websocket is a blocking operation which packio does not expect.
Definition: websocket.h:51
::packio::server< rpc, Acceptor, Dispatcher > server
The server for JSON-RPC.
Definition: json_rpc.h:42
The packio namespace.
Definition: arg.h:14