BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
websocket.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <functional>
3 #include <memory>
4 #include <string>
5 #include <boost/any.hpp>
6 #include <fc/network/ip.hpp>
8 #include <fc/signals.hpp>
9 
10 namespace fc { namespace http {
11  namespace detail {
16  } // namespace detail
17 
19  {
20  public:
22  virtual void send_message( const std::string& message ) = 0;
23  virtual void close( int64_t code, const std::string& reason ){};
24  void on_message( const std::string& message ) { _on_message(message); }
25  fc::http::reply on_http( const std::string& message ) { return _on_http(message); }
26 
27  void on_message_handler( const std::function<void(const std::string&)>& h ) { _on_message = h; }
28  void on_http_handler( const std::function<fc::http::reply(const std::string&)>& h ) { _on_http = h; }
29 
30  void set_session_data( boost::any d ){ _session_data = std::move(d); }
31  boost::any& get_session_data() { return _session_data; }
32 
33  virtual std::string get_request_header(const std::string& key) = 0;
34 
36  private:
37  boost::any _session_data;
38  std::function<void(const std::string&)> _on_message;
39  std::function<fc::http::reply(const std::string&)> _on_http;
40  };
41  typedef std::shared_ptr<websocket_connection> websocket_connection_ptr;
42 
43  typedef std::function<void(const websocket_connection_ptr&)> on_connection_handler;
44 
46  {
47  public:
50 
51  void on_connection( const on_connection_handler& handler);
52  void listen( uint16_t port );
53  void listen( const fc::ip::endpoint& ep );
54  uint16_t get_listening_port();
55  void start_accept();
56 
57  void stop_listening();
58  void close();
59 
60  private:
62  std::unique_ptr<detail::websocket_server_impl> my;
63  };
64 
65 
67  {
68  public:
69  websocket_tls_server( const std::string& server_pem = std::string(),
70  const std::string& ssl_password = std::string());
72 
73  void on_connection( const on_connection_handler& handler);
74  void listen( uint16_t port );
75  void listen( const fc::ip::endpoint& ep );
76  void start_accept();
77 
78  private:
80  std::unique_ptr<detail::websocket_tls_server_impl> my;
81  };
82 
84  {
85  public:
86  websocket_client( const std::string& ca_filename = "_default" );
88 
89  websocket_connection_ptr connect( const std::string& uri );
90  websocket_connection_ptr secure_connect( const std::string& uri );
91 
92  void close();
93  void synchronous_close();
94  private:
95  std::unique_ptr<detail::websocket_client_impl> my;
96  std::unique_ptr<detail::websocket_tls_client_impl> smy;
97  };
99  {
100  public:
101  websocket_tls_client( const std::string& ca_filename = "_default" );
103 
104  websocket_connection_ptr connect( const std::string& uri );
105  private:
106  std::unique_ptr<detail::websocket_tls_client_impl> my;
107  };
108 
109 } }
void on_message_handler(const std::function< void(const std::string &)> &h)
Definition: websocket.hpp:27
void on_message(const std::string &message)
Definition: websocket.hpp:24
virtual void close(int64_t code, const std::string &reason)
Definition: websocket.hpp:23
boost::any & get_session_data()
Definition: websocket.hpp:31
void connect(AsyncSocket &sock, const EndpointType &ep)
wraps boost::asio::socket::async_connect
Definition: asio.hpp:263
std::shared_ptr< websocket_connection > websocket_connection_ptr
Definition: websocket.hpp:41
fc::signal< void()> closed
Definition: websocket.hpp:35
fc::http::reply on_http(const std::string &message)
Definition: websocket.hpp:25
std::function< void(const websocket_connection_ptr &)> on_connection_handler
Definition: websocket.hpp:43
void set_session_data(boost::any d)
Definition: websocket.hpp:30
void on_http_handler(const std::function< fc::http::reply(const std::string &)> &h)
Definition: websocket.hpp:28
Definition: api.hpp:15
boost::signals2::signal< T > signal
Definition: signals.hpp:20