Cadabra
Computer algebra system for field theory problems
websocket_client.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/beast/core.hpp>
4 #include <boost/beast/websocket.hpp>
5 #include <boost/beast/ssl.hpp>
6 #include <boost/asio/connect.hpp>
7 #include <boost/asio/ip/tcp.hpp>
8 #include <boost/asio/ssl/stream.hpp>
9 #include <functional>
10 #include <memory>
11 #include <string>
12 #include <queue>
13 #include <regex>
14 
16  public:
17  // Callback handlers
18  using message_handler = std::function<void(const std::string&)>;
19  using connect_handler = std::function<void()>;
20  using close_handler = std::function<void()>;
21  using fail_handler = std::function<void(const boost::beast::error_code&)>;
22 
25 
26  // No copying
29 
30  // Set handlers (all optional)
35 
36  // Async operations (all return immediately)
37  void connect(const std::string& uri); // ws:// or wss://
38  void send(const std::string& message);
39  void close();
40 
41  void run();
42  void stop();
43 
44  private:
45  void on_resolve(const boost::beast::error_code& ec,
46  boost::asio::ip::tcp::resolver::results_type results);
47  void on_connect(const boost::beast::error_code& ec);
48  void on_ssl_handshake(const boost::beast::error_code& ec);
49  void on_handshake(const boost::beast::error_code& ec);
50  void on_write(const boost::beast::error_code& ec, std::size_t bytes_transferred);
51  void on_read(const boost::beast::error_code& ec, std::size_t bytes_transferred);
52  void on_close(const boost::beast::error_code& ec);
53  void do_read();
54  void do_write();
55  void fail(const boost::beast::error_code& ec);
56 
57  // State
58  boost::asio::io_context ioc_;
59  boost::asio::ssl::context ssl_ctx_;
60  boost::asio::ip::tcp::resolver resolver_;
61  std::unique_ptr<boost::beast::websocket::stream<boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>> wss_stream_;
62  std::unique_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>> ws_stream_;
63  boost::beast::flat_buffer buffer_;
64  bool is_ssl_;
65  std::string host_, port_, path_;
66 
67  // Handlers
72 
73  // Message queue.
74  struct queued_message {
75  std::string data;
76  std::shared_ptr<boost::beast::flat_buffer> buffer;
77  };
78  std::queue<queued_message> message_queue_;
79 
80  bool writing_{false};
81 };
82 
83 
84 class Uri {
85  public:
86  Uri(const std::string& uri);
87 
88  std::string to_string() const;
89 
90  std::string protocol;
91  std::string host;
92  std::string port;
93  std::string path;
94 
95 };
Definition: websocket_client.hh:84
std::string path
Definition: websocket_client.hh:93
std::string port
Definition: websocket_client.hh:92
std::string to_string() const
Definition: websocket_client.cc:284
std::string host
Definition: websocket_client.hh:91
std::string protocol
Definition: websocket_client.hh:90
Uri(const std::string &uri)
Definition: websocket_client.cc:270
Definition: websocket_client.hh:15
websocket_client(const websocket_client &)=delete
bool is_ssl_
Definition: websocket_client.hh:64
boost::beast::flat_buffer buffer_
Definition: websocket_client.hh:63
void set_message_handler(message_handler h)
Definition: websocket_client.cc:31
~websocket_client()
Definition: websocket_client.cc:13
void on_connect(const boost::beast::error_code &ec)
Definition: websocket_client.cc:94
void do_read()
Definition: websocket_client.cc:195
void on_ssl_handshake(const boost::beast::error_code &ec)
Definition: websocket_client.cc:113
void connect(const std::string &uri)
Definition: websocket_client.cc:36
boost::asio::io_context ioc_
Definition: websocket_client.hh:58
void fail(const boost::beast::error_code &ec)
Definition: websocket_client.cc:252
void on_read(const boost::beast::error_code &ec, std::size_t bytes_transferred)
Definition: websocket_client.cc:213
void on_handshake(const boost::beast::error_code &ec)
Definition: websocket_client.cc:123
void on_close(const boost::beast::error_code &ec)
Definition: websocket_client.cc:243
close_handler close_handler_
Definition: websocket_client.hh:70
std::string host_
Definition: websocket_client.hh:65
std::string port_
Definition: websocket_client.hh:65
void set_close_handler(close_handler h)
Definition: websocket_client.cc:33
std::queue< queued_message > message_queue_
Definition: websocket_client.hh:78
std::function< void()> close_handler
Definition: websocket_client.hh:20
void on_resolve(const boost::beast::error_code &ec, boost::asio::ip::tcp::resolver::results_type results)
Definition: websocket_client.cc:69
std::function< void()> connect_handler
Definition: websocket_client.hh:19
std::string path_
Definition: websocket_client.hh:65
std::unique_ptr< boost::beast::websocket::stream< boost::beast::ssl_stream< boost::asio::ip::tcp::socket > > > wss_stream_
Definition: websocket_client.hh:61
websocket_client & operator=(const websocket_client &)=delete
void run()
Definition: websocket_client.cc:259
websocket_client()
Definition: websocket_client.cc:4
void on_write(const boost::beast::error_code &ec, std::size_t bytes_transferred)
Definition: websocket_client.cc:153
bool writing_
Definition: websocket_client.hh:80
void close()
Definition: websocket_client.cc:225
boost::asio::ssl::context ssl_ctx_
Definition: websocket_client.hh:59
std::unique_ptr< boost::beast::websocket::stream< boost::asio::ip::tcp::socket > > ws_stream_
Definition: websocket_client.hh:62
message_handler message_handler_
Definition: websocket_client.hh:68
void set_fail_handler(fail_handler h)
Definition: websocket_client.cc:34
std::function< void(const std::string &)> message_handler
Definition: websocket_client.hh:18
fail_handler fail_handler_
Definition: websocket_client.hh:71
connect_handler connect_handler_
Definition: websocket_client.hh:69
void do_write()
Definition: websocket_client.cc:169
std::function< void(const boost::beast::error_code &)> fail_handler
Definition: websocket_client.hh:21
void set_connect_handler(connect_handler h)
Definition: websocket_client.cc:32
void send(const std::string &message)
Definition: websocket_client.cc:136
boost::asio::ip::tcp::resolver resolver_
Definition: websocket_client.hh:60
void stop()
Definition: websocket_client.cc:264
Definition: websocket_client.hh:74
std::string data
Definition: websocket_client.hh:75
std::shared_ptr< boost::beast::flat_buffer > buffer
Definition: websocket_client.hh:76