BitShares-Core  6.1.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 
18  // TODO refactor code, move stuff used by server or client only to derived class(es),
19  // E.G. it seems get_request_header and on_http* are for server only.
21  {
22  public:
24  virtual void send_message( const std::string& message ) = 0;
25  virtual void close( int64_t code, const std::string& reason ){};
26  void on_message( const std::string& message ) { _on_message(message); }
27  fc::http::reply on_http( const std::string& message ) { return _on_http(message); }
28 
29  void on_message_handler( const std::function<void(const std::string&)>& h ) { _on_message = h; }
30  void on_http_handler( const std::function<fc::http::reply(const std::string&)>& h ) { _on_http = h; }
31 
32  void set_session_data( boost::any d ){ _session_data = std::move(d); }
33  boost::any& get_session_data() { return _session_data; }
34 
35  virtual std::string get_request_header(const std::string& key) = 0;
36 
37  const std::string& get_remote_endpoint_string()const { return _remote_endpoint; }
38 
40  protected:
41  std::string _remote_endpoint; // for logging
42  private:
43  boost::any _session_data;
44  std::function<void(const std::string&)> _on_message;
45  std::function<fc::http::reply(const std::string&)> _on_http;
46  };
47  typedef std::shared_ptr<websocket_connection> websocket_connection_ptr;
48 
49  typedef std::function<void(const websocket_connection_ptr&)> on_connection_handler;
50 
51  // TODO websocket_tls_server and websocket_server have almost the same interface and implementation,
52  // better refactor to remove duplicate code and to avoid undesired or unnecessary differences
54  {
55  public:
56  websocket_server(const std::string& forward_header_key);
58 
59  void on_connection( const on_connection_handler& handler);
60  void listen( uint16_t port );
61  void listen( const fc::ip::endpoint& ep );
62  uint16_t get_listening_port();
63  void start_accept();
64 
65  void stop_listening();
66  void close();
67 
68  private:
70  std::unique_ptr<detail::websocket_server_impl> my;
71  };
72 
73 
74  // TODO websocket_tls_server and websocket_server have almost the same interface and implementation,
75  // better refactor to remove duplicate code and to avoid undesired or unnecessary differences
77  {
78  public:
79  websocket_tls_server( const std::string& server_pem,
80  const std::string& ssl_password,
81  const std::string& forward_header_key );
83 
84  void on_connection( const on_connection_handler& handler);
85  void listen( uint16_t port );
86  void listen( const fc::ip::endpoint& ep );
87  uint16_t get_listening_port();
88  void start_accept();
89 
90  void stop_listening();
91  void close();
92 
93  private:
95  std::unique_ptr<detail::websocket_tls_server_impl> my;
96  };
97 
99  {
100  public:
101  websocket_client( const std::string& ca_filename = "_default" );
102  ~websocket_client();
103 
104  websocket_connection_ptr connect( const std::string& uri );
105  websocket_connection_ptr secure_connect( const std::string& uri );
106 
107  void close();
108  void synchronous_close();
109  void append_header(const std::string& key, const std::string& value);
110  private:
111  std::unique_ptr<detail::websocket_client_impl> my;
112  std::unique_ptr<detail::websocket_tls_client_impl> smy;
113  fc::http::headers _headers;
114  };
115 
116 } }
void on_message_handler(const std::function< void(const std::string &)> &h)
Definition: websocket.hpp:29
void on_message(const std::string &message)
Definition: websocket.hpp:26
virtual void close(int64_t code, const std::string &reason)
Definition: websocket.hpp:25
boost::any & get_session_data()
Definition: websocket.hpp:33
void connect(AsyncSocket &sock, const EndpointType &ep)
wraps boost::asio::socket::async_connect
Definition: asio.hpp:262
std::shared_ptr< websocket_connection > websocket_connection_ptr
Definition: websocket.hpp:47
fc::signal< void()> closed
Definition: websocket.hpp:39
fc::http::reply on_http(const std::string &message)
Definition: websocket.hpp:27
std::function< void(const websocket_connection_ptr &)> on_connection_handler
Definition: websocket.hpp:49
void set_session_data(boost::any d)
Definition: websocket.hpp:32
void on_http_handler(const std::function< fc::http::reply(const std::string &)> &h)
Definition: websocket.hpp:30
Definition: api.hpp:15
std::vector< header > headers
Definition: connection.hpp:21
const std::string & get_remote_endpoint_string() const
Definition: websocket.hpp:37
boost::signals2::signal< T > signal
Definition: signals.hpp:20