BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
39  fc::signal<void()> closed;
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" );
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 } }
fc::http::detail::websocket_client_impl
Definition: websocket.cpp:551
fc::http::websocket_server::get_listening_port
uint16_t get_listening_port()
Definition: websocket.cpp:655
ip.hpp
fc::http::websocket_server
Definition: websocket.hpp:53
fc::http::websocket_server::~websocket_server
~websocket_server()
Definition: websocket.cpp:638
fc::http::websocket_connection::on_message_handler
void on_message_handler(const std::function< void(const std::string &)> &h)
Definition: websocket.hpp:29
fc::http::websocket_tls_server::on_connection
void on_connection(const on_connection_handler &handler)
Definition: websocket.cpp:685
fc::http::websocket_tls_server::close
void close()
Definition: websocket.cpp:715
fc::http::websocket_connection
Definition: websocket.hpp:20
fc
Definition: api.hpp:15
fc::http::websocket_tls_server::~websocket_tls_server
~websocket_tls_server()
Definition: websocket.cpp:683
fc::http::websocket_tls_server
Definition: websocket.hpp:76
fc::http::websocket_client::~websocket_client
~websocket_client()
Definition: websocket.cpp:729
fc::http::websocket_connection_ptr
std::shared_ptr< websocket_connection > websocket_connection_ptr
Definition: websocket.hpp:47
fc::signal
boost::signals2::signal< T > signal
Definition: signals.hpp:20
fc::http::detail::websocket_tls_server_impl
Definition: websocket.cpp:413
fc::http::websocket_connection::on_message
void on_message(const std::string &message)
Definition: websocket.hpp:26
fc::http::websocket_client::websocket_client
websocket_client(const std::string &ca_filename="_default")
Definition: websocket.cpp:724
fc::http::websocket_connection::close
virtual void close(int64_t code, const std::string &reason)
Definition: websocket.hpp:25
fc::http::websocket_connection::get_session_data
boost::any & get_session_data()
Definition: websocket.hpp:33
fc::http::headers
std::vector< header > headers
Definition: connection.hpp:21
fc::ip::endpoint
Definition: ip.hpp:65
fc::http::websocket_connection::on_http
fc::http::reply on_http(const std::string &message)
Definition: websocket.hpp:27
fc::http::websocket_server::start_accept
void start_accept()
Definition: websocket.cpp:661
fc::http::websocket_connection::closed
fc::signal< void()> closed
Definition: websocket.hpp:39
connection.hpp
signals.hpp
fc::http::websocket_connection::send_message
virtual void send_message(const std::string &message)=0
fc::http::websocket_tls_server::stop_listening
void stop_listening()
Definition: websocket.cpp:710
fc::http::websocket_tls_server::get_listening_port
uint16_t get_listening_port()
Definition: websocket.cpp:700
fc::http::websocket_client::append_header
void append_header(const std::string &key, const std::string &value)
Definition: websocket.cpp:763
fc::http::websocket_tls_server::start_accept
void start_accept()
Definition: websocket.cpp:706
fc::http::websocket_connection::set_session_data
void set_session_data(boost::any d)
Definition: websocket.hpp:32
fc::http::websocket_server::stop_listening
void stop_listening()
Definition: websocket.cpp:665
fc::http::websocket_connection::_remote_endpoint
std::string _remote_endpoint
Definition: websocket.hpp:41
fc::http::websocket_server::websocket_server
websocket_server(const std::string &forward_header_key)
Definition: websocket.cpp:636
fc::http::websocket_client::connect
websocket_connection_ptr connect(const std::string &uri)
Definition: websocket.cpp:731
fc::http::websocket_tls_server::websocket_tls_server
websocket_tls_server(const std::string &server_pem, const std::string &ssl_password, const std::string &forward_header_key)
Definition: websocket.cpp:678
fc::http::websocket_client::close
void close()
Definition: websocket.cpp:750
fc::http::websocket_connection::~websocket_connection
virtual ~websocket_connection()
Definition: websocket.hpp:23
fc::http::websocket_server::close
void close()
Definition: websocket.cpp:670
fc::http::websocket_connection::on_http_handler
void on_http_handler(const std::function< fc::http::reply(const std::string &)> &h)
Definition: websocket.hpp:30
fc::http::websocket_connection::get_request_header
virtual std::string get_request_header(const std::string &key)=0
fc::http::websocket_server::listen
void listen(uint16_t port)
Definition: websocket.cpp:645
fc::http::reply
Definition: connection.hpp:23
fc::http::websocket_connection::get_remote_endpoint_string
const std::string & get_remote_endpoint_string() const
Definition: websocket.hpp:37
fc::http::websocket_server::on_connection
void on_connection(const on_connection_handler &handler)
Definition: websocket.cpp:640
fc::http::websocket_client::synchronous_close
void synchronous_close()
Definition: websocket.cpp:756
fc::http::on_connection_handler
std::function< void(const websocket_connection_ptr &)> on_connection_handler
Definition: websocket.hpp:49
fc::http::websocket_tls_server::listen
void listen(uint16_t port)
Definition: websocket.cpp:690
fc::http::websocket_client::secure_connect
websocket_connection_ptr secure_connect(const std::string &uri)
Definition: websocket.cpp:745
fc::http::detail::websocket_tls_client_impl
Definition: websocket.cpp:554
fc::http::detail::websocket_server_impl
Definition: websocket.cpp:403
fc::http::websocket_client
Definition: websocket.hpp:98