missing websocket_api.hpp changes
This commit is contained in:
parent
5a897c3b84
commit
614a0dad6b
1 changed files with 26 additions and 98 deletions
|
|
@ -1,108 +1,36 @@
|
|||
#pragma once
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <boost/any.hpp>
|
||||
#include <fc/network/ip.hpp>
|
||||
#include <fc/signals.hpp>
|
||||
#include <fc/rpc/api_connection.hpp>
|
||||
#include <fc/rpc/state.hpp>
|
||||
#include <fc/network/http/websocket.hpp>
|
||||
#include <fc/io/json.hpp>
|
||||
#include <fc/reflect/variant.hpp>
|
||||
|
||||
namespace fc { namespace http {
|
||||
namespace detail {
|
||||
class websocket_server_impl;
|
||||
class websocket_tls_server_impl;
|
||||
class websocket_client_impl;
|
||||
class websocket_tls_client_impl;
|
||||
} // namespace detail;
|
||||
namespace fc { namespace rpc {
|
||||
|
||||
class websocket_connection
|
||||
class websocket_api_connection : public api_connection
|
||||
{
|
||||
public:
|
||||
virtual ~websocket_connection(){}
|
||||
virtual void send_message( const std::string& message ) = 0;
|
||||
virtual void close( int64_t code, const std::string& reason ){};
|
||||
void on_message( const std::string& message ) { _on_message(message); }
|
||||
string on_http( const std::string& message ) { return _on_http(message); }
|
||||
websocket_api_connection(const std::shared_ptr<fc::http::websocket_connection> &c, uint32_t max_conversion_depth );
|
||||
~websocket_api_connection();
|
||||
|
||||
void on_message_handler( const std::function<void(const std::string&)>& h ) { _on_message = h; }
|
||||
void on_http_handler( const std::function<std::string(const std::string&)>& h ) { _on_http = h; }
|
||||
virtual variant send_call(
|
||||
api_id_type api_id,
|
||||
string method_name,
|
||||
variants args = variants() ) override;
|
||||
virtual variant send_callback(
|
||||
uint64_t callback_id,
|
||||
variants args = variants() ) override;
|
||||
virtual void send_notice(
|
||||
uint64_t callback_id,
|
||||
variants args = variants() ) override;
|
||||
|
||||
void set_session_data( boost::any d ){ _session_data = std::move(d); }
|
||||
boost::any& get_session_data() { return _session_data; }
|
||||
protected:
|
||||
std::string on_message(
|
||||
const std::string& message,
|
||||
bool send_message = true );
|
||||
|
||||
virtual std::string get_request_header(const std::string& key) = 0;
|
||||
|
||||
fc::signal<void()> closed;
|
||||
private:
|
||||
boost::any _session_data;
|
||||
std::function<void(const std::string&)> _on_message;
|
||||
std::function<string(const std::string&)> _on_http;
|
||||
};
|
||||
typedef std::shared_ptr<websocket_connection> websocket_connection_ptr;
|
||||
|
||||
typedef std::function<void(const websocket_connection_ptr&)> on_connection_handler;
|
||||
|
||||
class websocket_server
|
||||
{
|
||||
public:
|
||||
websocket_server();
|
||||
~websocket_server();
|
||||
|
||||
void on_connection( const on_connection_handler& handler);
|
||||
void listen( uint16_t port );
|
||||
void listen( const fc::ip::endpoint& ep );
|
||||
uint16_t get_listening_port();
|
||||
void start_accept();
|
||||
|
||||
void stop_listening();
|
||||
void close();
|
||||
private:
|
||||
friend class detail::websocket_server_impl;
|
||||
std::unique_ptr<detail::websocket_server_impl> my;
|
||||
std::shared_ptr<fc::http::websocket_connection> _connection;
|
||||
fc::rpc::state _rpc_state;
|
||||
};
|
||||
|
||||
|
||||
class websocket_tls_server
|
||||
{
|
||||
public:
|
||||
websocket_tls_server( const std::string& server_pem = std::string(),
|
||||
const std::string& ssl_password = std::string());
|
||||
~websocket_tls_server();
|
||||
|
||||
void on_connection( const on_connection_handler& handler);
|
||||
void listen( uint16_t port );
|
||||
void listen( const fc::ip::endpoint& ep );
|
||||
void start_accept();
|
||||
|
||||
private:
|
||||
friend class detail::websocket_tls_server_impl;
|
||||
std::unique_ptr<detail::websocket_tls_server_impl> my;
|
||||
};
|
||||
|
||||
class websocket_client
|
||||
{
|
||||
public:
|
||||
websocket_client( const std::string& ca_filename = "_default" );
|
||||
~websocket_client();
|
||||
|
||||
websocket_connection_ptr connect( const std::string& uri );
|
||||
websocket_connection_ptr secure_connect( const std::string& uri );
|
||||
|
||||
void close();
|
||||
void synchronous_close();
|
||||
|
||||
private:
|
||||
std::unique_ptr<detail::websocket_client_impl> my;
|
||||
std::unique_ptr<detail::websocket_tls_client_impl> smy;
|
||||
};
|
||||
class websocket_tls_client
|
||||
{
|
||||
public:
|
||||
websocket_tls_client( const std::string& ca_filename = "_default" );
|
||||
~websocket_tls_client();
|
||||
|
||||
websocket_connection_ptr connect( const std::string& uri );
|
||||
private:
|
||||
std::unique_ptr<detail::websocket_tls_client_impl> my;
|
||||
};
|
||||
|
||||
} }
|
||||
} } // namespace fc::rpc
|
||||
|
|
|
|||
Loading…
Reference in a new issue