This commit is contained in:
Daniel Larimer 2016-03-11 12:33:41 -05:00
commit 0dca15c395
7 changed files with 323 additions and 320 deletions

View file

@ -142,6 +142,17 @@ find_package(OpenSSL REQUIRED)
set( CMAKE_FIND_LIBRARY_SUFFIXES ${ORIGINAL_LIB_SUFFIXES} )
# We are now building in support for deflate compression into our websockets layer by default,
# which requires zlib. Aside from that, all of fc compiles without zlib, so this could be
# made optional without much effort
# (important exception, apple: as of 10.10 yosemite, the OpenSSL static libraries shipped with
# os x have a dependency on zlib)
# On a side note, fc's fc::zlib_compress() function uses a separate implementation of zlib
# from the miniz library. If we're comfortable requiring an external zlib, we can
# reimplement fc::zlib_compress() to call the real zlib, and remove miniz.c from our
# repository.
find_package( ZLIB REQUIRED )
option( UNITY_BUILD OFF )
set( fc_sources
@ -473,14 +484,6 @@ if(WIN32)
endif(WIN32)
IF(APPLE)
# As of 10.10 yosemite, the OpenSSL static libraries shipped with os x have a dependency
# on zlib, so any time you link in openssl you also need to link zlib. . We really want to detect whether openssl was configured with the --no-zlib
# option or not when it was built, but that's difficult to do in practice, so we
# just always try to link it in on mac.
find_package( ZLIB REQUIRED )
ENDIF(APPLE)
SET(OPENSSL_CONF_TARGET )
IF(DEFINED CMAKE_RUNTIME_OUTPUT_DIRECTORY)
SET (OPENSSL_CONF_TARGET ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

View file

@ -120,7 +120,7 @@ namespace fc {
template<typename Stream, typename T>
inline void pack( Stream& s, const std::shared_ptr<T>& v)
{
pack( s, *v );
fc::raw::pack( s, *v );
}
template<typename Stream, typename T, size_t N>
@ -133,7 +133,7 @@ namespace fc {
inline void unpack( Stream& s, std::shared_ptr<T>& v)
{ try {
v = std::make_shared<T>();
unpack( s, *v );
fc::raw::unpack( s, *v );
} FC_RETHROW_EXCEPTIONS( warn, "std::shared_ptr<T>", ("type",fc::get_typename<T>::name()) ) }
template<typename Stream> inline void pack( Stream& s, const signed_int& v ) {
@ -180,55 +180,55 @@ namespace fc {
template<typename Stream, typename T> inline void unpack( Stream& s, const T& vi )
{
T tmp;
unpack( s, tmp );
fc::raw::unpack( s, tmp );
FC_ASSERT( vi == tmp );
}
template<typename Stream> inline void pack( Stream& s, const char* v ) { pack( s, fc::string(v) ); }
template<typename Stream> inline void pack( Stream& s, const char* v ) { fc::raw::pack( s, fc::string(v) ); }
template<typename Stream, typename T>
void pack( Stream& s, const safe<T>& v ) { pack( s, v.value ); }
void pack( Stream& s, const safe<T>& v ) { fc::raw::pack( s, v.value ); }
template<typename Stream, typename T>
void unpack( Stream& s, fc::safe<T>& v ) { unpack( s, v.value ); }
void unpack( Stream& s, fc::safe<T>& v ) { fc::raw::unpack( s, v.value ); }
template<typename Stream, typename T, unsigned int S, typename Align>
void pack( Stream& s, const fc::fwd<T,S,Align>& v ) {
pack( *v );
fc::raw::pack( *v );
}
template<typename Stream, typename T, unsigned int S, typename Align>
void unpack( Stream& s, fc::fwd<T,S,Align>& v ) {
unpack( *v );
fc::raw::unpack( *v );
}
template<typename Stream, typename T>
void pack( Stream& s, const fc::smart_ref<T>& v ) { pack( s, *v ); }
void pack( Stream& s, const fc::smart_ref<T>& v ) { fc::raw::pack( s, *v ); }
template<typename Stream, typename T>
void unpack( Stream& s, fc::smart_ref<T>& v ) { unpack( s, *v ); }
void unpack( Stream& s, fc::smart_ref<T>& v ) { fc::raw::unpack( s, *v ); }
// optional
template<typename Stream, typename T>
void pack( Stream& s, const fc::optional<T>& v ) {
pack( s, bool(!!v) );
if( !!v ) pack( s, *v );
fc::raw::pack( s, bool(!!v) );
if( !!v ) fc::raw::pack( s, *v );
}
template<typename Stream, typename T>
void unpack( Stream& s, fc::optional<T>& v )
{ try {
bool b; unpack( s, b );
if( b ) { v = T(); unpack( s, *v ); }
bool b; fc::raw::unpack( s, b );
if( b ) { v = T(); fc::raw::unpack( s, *v ); }
} FC_RETHROW_EXCEPTIONS( warn, "optional<${type}>", ("type",fc::get_typename<T>::name() ) ) }
// std::vector<char>
template<typename Stream> inline void pack( Stream& s, const std::vector<char>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
if( value.size() )
s.write( &value.front(), (uint32_t)value.size() );
}
template<typename Stream> inline void unpack( Stream& s, std::vector<char>& value ) {
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
FC_ASSERT( size.value < MAX_ARRAY_ALLOC_SIZE );
value.resize(size.value);
if( value.size() )
@ -237,23 +237,23 @@ namespace fc {
// fc::string
template<typename Stream> inline void pack( Stream& s, const fc::string& v ) {
pack( s, unsigned_int((uint32_t)v.size()));
fc::raw::pack( s, unsigned_int((uint32_t)v.size()));
if( v.size() ) s.write( v.c_str(), v.size() );
}
template<typename Stream> inline void unpack( Stream& s, fc::string& v ) {
std::vector<char> tmp; unpack(s,tmp);
std::vector<char> tmp; fc::raw::unpack(s,tmp);
if( tmp.size() )
v = fc::string(tmp.data(),tmp.data()+tmp.size());
else v = fc::string();
}
// bool
template<typename Stream> inline void pack( Stream& s, const bool& v ) { pack( s, uint8_t(v) ); }
template<typename Stream> inline void pack( Stream& s, const bool& v ) { fc::raw::pack( s, uint8_t(v) ); }
template<typename Stream> inline void unpack( Stream& s, bool& v )
{
uint8_t b;
unpack( s, b );
fc::raw::unpack( s, b );
FC_ASSERT( (b & ~1) == 0 );
v=(b!=0);
}
@ -267,7 +267,7 @@ namespace fc {
template<typename T, typename C, T(C::*p)>
void operator()( const char* name )const {
raw::pack( s, c.*p );
fc::raw::pack( s, c.*p );
}
private:
const Class& c;
@ -282,7 +282,7 @@ namespace fc {
template<typename T, typename C, T(C::*p)>
inline void operator()( const char* name )const
{ try {
raw::unpack( s, c.*p );
fc::raw::unpack( s, c.*p );
} FC_RETHROW_EXCEPTIONS( warn, "Error unpacking field ${field}", ("field",name) ) }
private:
Class& c;
@ -361,7 +361,7 @@ namespace fc {
template<typename Stream, typename T>
inline void pack( Stream& s, const std::unordered_set<T>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
@ -371,7 +371,7 @@ namespace fc {
}
template<typename Stream, typename T>
inline void unpack( Stream& s, std::unordered_set<T>& value ) {
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
value.clear();
FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE );
value.reserve(size.value);
@ -386,19 +386,19 @@ namespace fc {
template<typename Stream, typename K, typename V>
inline void pack( Stream& s, const std::pair<K,V>& value ) {
pack( s, value.first );
pack( s, value.second );
fc::raw::pack( s, value.first );
fc::raw::pack( s, value.second );
}
template<typename Stream, typename K, typename V>
inline void unpack( Stream& s, std::pair<K,V>& value )
{
unpack( s, value.first );
unpack( s, value.second );
fc::raw::unpack( s, value.first );
fc::raw::unpack( s, value.second );
}
template<typename Stream, typename K, typename V>
inline void pack( Stream& s, const std::unordered_map<K,V>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
@ -409,7 +409,7 @@ namespace fc {
template<typename Stream, typename K, typename V>
inline void unpack( Stream& s, std::unordered_map<K,V>& value )
{
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
value.clear();
FC_ASSERT( size.value*(sizeof(K)+sizeof(V)) < MAX_ARRAY_ALLOC_SIZE );
value.reserve(size.value);
@ -422,7 +422,7 @@ namespace fc {
}
template<typename Stream, typename K, typename V>
inline void pack( Stream& s, const std::map<K,V>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
@ -433,7 +433,7 @@ namespace fc {
template<typename Stream, typename K, typename V>
inline void unpack( Stream& s, std::map<K,V>& value )
{
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
value.clear();
FC_ASSERT( size.value*(sizeof(K)+sizeof(V)) < MAX_ARRAY_ALLOC_SIZE );
for( uint32_t i = 0; i < size.value; ++i )
@ -446,7 +446,7 @@ namespace fc {
template<typename Stream, typename T>
inline void pack( Stream& s, const std::deque<T>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
@ -457,7 +457,7 @@ namespace fc {
template<typename Stream, typename T>
inline void unpack( Stream& s, std::deque<T>& value ) {
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE );
value.resize(size.value);
auto itr = value.begin();
@ -470,7 +470,7 @@ namespace fc {
template<typename Stream, typename T>
inline void pack( Stream& s, const std::vector<T>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
@ -481,7 +481,7 @@ namespace fc {
template<typename Stream, typename T>
inline void unpack( Stream& s, std::vector<T>& value ) {
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE );
value.resize(size.value);
auto itr = value.begin();
@ -494,7 +494,7 @@ namespace fc {
template<typename Stream, typename T>
inline void pack( Stream& s, const std::set<T>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
@ -505,11 +505,11 @@ namespace fc {
template<typename Stream, typename T>
inline void unpack( Stream& s, std::set<T>& value ) {
unsigned_int size; unpack( s, size );
unsigned_int size; fc::raw::unpack( s, size );
for( uint64_t i = 0; i < size.value; ++i )
{
T tmp;
unpack( s, tmp );
fc::raw::unpack( s, tmp );
value.insert( std::move(tmp) );
}
}
@ -530,19 +530,19 @@ namespace fc {
inline size_t pack_size( const T& v )
{
datastream<size_t> ps;
raw::pack(ps,v );
fc::raw::pack(ps,v );
return ps.tellp();
}
template<typename T>
inline std::vector<char> pack( const T& v ) {
datastream<size_t> ps;
raw::pack(ps,v );
fc::raw::pack(ps,v );
std::vector<char> vec(ps.tellp());
if( vec.size() ) {
datastream<char*> ds( vec.data(), size_t(vec.size()) );
raw::pack(ds,v);
fc::raw::pack(ds,v);
}
return vec;
}
@ -553,7 +553,7 @@ namespace fc {
T tmp;
if( s.size() ) {
datastream<const char*> ds( s.data(), size_t(s.size()) );
raw::unpack(ds,tmp);
fc::raw::unpack(ds,tmp);
}
return tmp;
} FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename<T>::name() ) ) }
@ -563,14 +563,14 @@ namespace fc {
{ try {
if( s.size() ) {
datastream<const char*> ds( s.data(), size_t(s.size()) );
raw::unpack(ds,tmp);
fc::raw::unpack(ds,tmp);
}
} FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename<T>::name() ) ) }
template<typename T>
inline void pack( char* d, uint32_t s, const T& v ) {
datastream<char*> ds(d,s);
raw::pack(ds,v );
fc::raw::pack(ds,v );
}
template<typename T>
@ -578,7 +578,7 @@ namespace fc {
{ try {
T v;
datastream<const char*> ds( d, s );
raw::unpack(ds,v);
fc::raw::unpack(ds,v);
return v;
} FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename<T>::name() ) ) }
@ -586,7 +586,7 @@ namespace fc {
inline void unpack( const char* d, uint32_t s, T& v )
{ try {
datastream<const char*> ds( d, s );
raw::unpack(ds,v);
fc::raw::unpack(ds,v);
return v;
} FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename<T>::name() ) ) }
@ -599,7 +599,7 @@ namespace fc {
typedef void result_type;
template<typename T> void operator()( const T& v )const
{
pack( stream, v );
fc::raw::pack( stream, v );
}
};
@ -612,7 +612,7 @@ namespace fc {
typedef void result_type;
template<typename T> void operator()( T& v )const
{
unpack( stream, v );
fc::raw::unpack( stream, v );
}
};
@ -620,14 +620,14 @@ namespace fc {
template<typename Stream, typename... T>
void pack( Stream& s, const static_variant<T...>& sv )
{
pack( s, unsigned_int(sv.which()) );
fc::raw::pack( s, unsigned_int(sv.which()) );
sv.visit( pack_static_variant<Stream>(s) );
}
template<typename Stream, typename... T> void unpack( Stream& s, static_variant<T...>& sv )
{
unsigned_int w;
unpack( s, w );
fc::raw::unpack( s, w );
sv.set_which(w.value);
sv.visit( unpack_static_variant<Stream>(s) );
}

View file

@ -8,8 +8,7 @@
namespace fc { namespace http {
namespace detail {
class websocket_server_impl;
class websocket_tls_server_impl;
class abstract_websocket_server;
class websocket_client_impl;
class websocket_tls_client_impl;
} // namespace detail;
@ -42,7 +41,7 @@ namespace fc { namespace http {
class websocket_server
{
public:
websocket_server();
websocket_server(bool enable_permessage_deflate = true);
~websocket_server();
void on_connection( const on_connection_handler& handler);
@ -51,16 +50,16 @@ namespace fc { namespace http {
void start_accept();
private:
friend class detail::websocket_server_impl;
std::unique_ptr<detail::websocket_server_impl> my;
std::unique_ptr<detail::abstract_websocket_server> my;
};
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(const std::string& server_pem = std::string(),
const std::string& ssl_password = std::string(),
bool enable_permessage_deflate = true);
~websocket_tls_server();
void on_connection( const on_connection_handler& handler);
@ -69,8 +68,7 @@ namespace fc { namespace http {
void start_accept();
private:
friend class detail::websocket_tls_server_impl;
std::unique_ptr<detail::websocket_tls_server_impl> my;
std::unique_ptr<detail::abstract_websocket_server> my;
};
class websocket_client

View file

@ -21,6 +21,8 @@ namespace fc {
class optional
{
public:
typedef T value_type;
optional():_valid(false){}
~optional(){ reset(); }

View file

@ -3,6 +3,7 @@
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/logger/stub.hpp>
@ -19,12 +20,11 @@
namespace fc { namespace http {
namespace detail {
struct asio_with_stub_log : public websocketpp::config::asio {
typedef asio_with_stub_log type;
typedef asio base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
@ -34,14 +34,7 @@ namespace fc { namespace http {
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
/// Custom Logging policies
/*typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::elevel> elog_type;
typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::alevel> alog_type;
*/
//typedef base::alog_type alog_type;
//typedef base::elog_type elog_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
@ -60,13 +53,15 @@ namespace fc { namespace http {
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
// override default value of 5 sec timeout
static const long timeout_open_handshake = 0;
};
struct asio_tls_with_stub_log : public websocketpp::config::asio_tls {
typedef asio_with_stub_log type;
typedef asio_tls base;
struct asio_with_stub_log_and_deflate : public websocketpp::config::asio {
typedef asio_with_stub_log_and_deflate type;
typedef asio base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
@ -76,14 +71,7 @@ namespace fc { namespace http {
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
/// Custom Logging policies
/*typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::elevel> elog_type;
typedef websocketpp::log::syslog<concurrency_type,
websocketpp::log::alevel> alog_type;
*/
//typedef base::alog_type alog_type;
//typedef base::elog_type elog_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
@ -95,18 +83,27 @@ namespace fc { namespace http {
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::tls_socket::endpoint socket_type;
typedef websocketpp::transport::asio::basic_socket::endpoint
socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
/// enable the permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
// override default value of 5 sec timeout
static const long timeout_open_handshake = 0;
};
struct asio_tls_stub_log : public websocketpp::config::asio_tls {
typedef asio_tls_stub_log type;
typedef asio_tls base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
@ -116,8 +113,7 @@ namespace fc { namespace http {
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
//typedef base::alog_type alog_type;
//typedef base::elog_type elog_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
@ -136,13 +132,45 @@ namespace fc { namespace http {
transport_type;
};
struct asio_tls_stub_log_and_deflate : public websocketpp::config::asio_tls {
typedef asio_tls_stub_log_and_deflate type;
typedef asio_tls base;
//// All boilerplate copying the base class's config, except as noted
typedef base::concurrency_type concurrency_type;
typedef base::request_type request_type;
typedef base::response_type response_type;
typedef base::message_type message_type;
typedef base::con_msg_manager_type con_msg_manager_type;
typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
/// Custom Logging policies, use do-nothing log::stub instead of log::basic
typedef websocketpp::log::stub elog_type;
typedef websocketpp::log::stub alog_type;
typedef base::rng_type rng_type;
struct transport_config : public base::transport_config {
typedef type::concurrency_type concurrency_type;
typedef type::alog_type alog_type;
typedef type::elog_type elog_type;
typedef type::request_type request_type;
typedef type::response_type response_type;
typedef websocketpp::transport::asio::tls_socket::endpoint socket_type;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
/// enable the permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
};
using websocketpp::connection_hdl;
typedef websocketpp::server<asio_with_stub_log> websocket_server_type;
typedef websocketpp::server<asio_tls_stub_log> websocket_tls_server_type;
template<typename T>
class websocket_connection_impl : public websocket_connection
@ -173,7 +201,19 @@ namespace fc { namespace http {
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
class websocket_server_impl
class abstract_websocket_server
{
public:
virtual ~abstract_websocket_server() {}
virtual void on_connection( const on_connection_handler& handler) = 0;
virtual void listen( uint16_t port ) = 0;
virtual void listen( const fc::ip::endpoint& ep ) = 0;
virtual void start_accept() = 0;
};
template <typename config>
class websocket_server_impl : public abstract_websocket_server
{
public:
websocket_server_impl()
@ -185,15 +225,15 @@ namespace fc { namespace http {
_server.set_reuse_addr(true);
_server.set_open_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto new_con = std::make_shared<websocket_connection_impl<websocket_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
websocket_connection_ptr new_con = std::make_shared<websocket_connection_impl<typename websocketpp::server<config>::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( _connections[hdl] = new_con );
}).wait();
});
_server.set_message_handler( [&]( connection_hdl hdl, websocket_server_type::message_ptr msg ){
_server.set_message_handler( [&]( connection_hdl hdl, typename websocketpp::server<config>::message_ptr msg ){
_server_thread.async( [&](){
auto current_con = _connections.find(hdl);
assert( current_con != _connections.end() );
wdump(("server")(msg->get_payload()));
//wdump(("server")(msg->get_payload()));
//std::cerr<<"recv: "<<msg->get_payload()<<"\n";
auto payload = msg->get_payload();
std::shared_ptr<websocket_connection> con = current_con->second;
@ -206,13 +246,13 @@ namespace fc { namespace http {
_server.set_http_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto current_con = std::make_shared<websocket_connection_impl<websocket_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
auto current_con = std::make_shared<websocket_connection_impl<typename websocketpp::server<config>::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( current_con );
auto con = _server.get_con_from_hdl(hdl);
con->defer_http_response();
std::string request_body = con->get_request_body();
wdump(("server")(request_body));
//wdump(("server")(request_body));
fc::async([current_con, request_body, con] {
std::string response = current_con->on_http(request_body);
@ -274,25 +314,43 @@ namespace fc { namespace http {
if( _closed ) _closed->wait();
}
void on_connection( const on_connection_handler& handler ) override
{
_on_connection = handler;
}
void listen( uint16_t port ) override
{
_server.listen(port);
}
void listen( const fc::ip::endpoint& ep ) override
{
_server.listen( boost::asio::ip::tcp::endpoint( boost::asio::ip::address_v4(uint32_t(ep.get_address())),ep.port()) );
}
void start_accept() override
{
_server.start_accept();
}
typedef std::map<connection_hdl, websocket_connection_ptr,std::owner_less<connection_hdl> > con_map;
con_map _connections;
fc::thread& _server_thread;
websocket_server_type _server;
websocketpp::server<config> _server;
on_connection_handler _on_connection;
fc::promise<void>::ptr _closed;
uint32_t _pending_messages = 0;
};
class websocket_tls_server_impl
template <typename config>
class websocket_tls_server_impl : public websocket_server_impl<config>
{
public:
websocket_tls_server_impl( const string& server_pem, const string& ssl_password )
:_server_thread( fc::thread::current() )
{
//if( server_pem.size() )
{
_server.set_tls_init_handler( [=]( websocketpp::connection_hdl hdl ) -> context_ptr {
this->_server.set_tls_init_handler( [=]( websocketpp::connection_hdl hdl ) -> context_ptr {
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv1);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
@ -308,98 +366,10 @@ namespace fc { namespace http {
return ctx;
});
}
_server.clear_access_channels( websocketpp::log::alevel::all );
_server.init_asio(&fc::asio::default_io_service());
_server.set_reuse_addr(true);
_server.set_open_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto new_con = std::make_shared<websocket_connection_impl<websocket_tls_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
_on_connection( _connections[hdl] = new_con );
}).wait();
});
_server.set_message_handler( [&]( connection_hdl hdl, websocket_server_type::message_ptr msg ){
_server_thread.async( [&](){
auto current_con = _connections.find(hdl);
assert( current_con != _connections.end() );
auto received = msg->get_payload();
std::shared_ptr<websocket_connection> con = current_con->second;
fc::async([con,received](){ con->on_message( received ); });
}).wait();
});
_server.set_http_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
auto current_con = std::make_shared<websocket_connection_impl<websocket_tls_server_type::connection_ptr>>( _server.get_con_from_hdl(hdl) );
try{
_on_connection( current_con );
auto con = _server.get_con_from_hdl(hdl);
wdump(("server")(con->get_request_body()));
auto response = current_con->on_http( con->get_request_body() );
con->set_body( response );
con->set_status( websocketpp::http::status_code::ok );
} catch ( const fc::exception& e )
{
edump((e.to_detail_string()));
}
current_con->closed();
}).wait();
});
_server.set_close_handler( [&]( connection_hdl hdl ){
_server_thread.async( [&](){
_connections[hdl]->closed();
_connections.erase( hdl );
}).wait();
});
_server.set_fail_handler( [&]( connection_hdl hdl ){
if( _server.is_listening() )
{
_server_thread.async( [&](){
if( _connections.find(hdl) != _connections.end() )
{
_connections[hdl]->closed();
_connections.erase( hdl );
}
}).wait();
}
});
}
~websocket_tls_server_impl()
{
if( _server.is_listening() )
_server.stop_listening();
auto cpy_con = _connections;
for( auto item : cpy_con )
_server.close( item.first, 0, "server exit" );
}
typedef std::map<connection_hdl, websocket_connection_ptr,std::owner_less<connection_hdl> > con_map;
con_map _connections;
fc::thread& _server_thread;
websocket_tls_server_type _server;
on_connection_handler _on_connection;
fc::promise<void>::ptr _closed;
};
typedef websocketpp::client<asio_with_stub_log> websocket_client_type;
typedef websocketpp::client<asio_tls_stub_log> websocket_tls_client_type;
@ -542,57 +512,63 @@ namespace fc { namespace http {
} // namespace detail
websocket_server::websocket_server():my( new detail::websocket_server_impl() ) {}
websocket_server::websocket_server(bool enable_permessage_deflate /* = true */) :
my( enable_permessage_deflate ?
(detail::abstract_websocket_server*)new detail::websocket_server_impl<detail::asio_with_stub_log_and_deflate> :
(detail::abstract_websocket_server*)new detail::websocket_server_impl<detail::asio_with_stub_log> )
{}
websocket_server::~websocket_server(){}
void websocket_server::on_connection( const on_connection_handler& handler )
{
my->_on_connection = handler;
my->on_connection(handler);
}
void websocket_server::listen( uint16_t port )
{
my->_server.listen(port);
my->listen(port);
}
void websocket_server::listen( const fc::ip::endpoint& ep )
{
my->_server.listen( boost::asio::ip::tcp::endpoint( boost::asio::ip::address_v4(uint32_t(ep.get_address())),ep.port()) );
my->listen(ep);
}
void websocket_server::start_accept() {
my->_server.start_accept();
my->start_accept();
}
websocket_tls_server::websocket_tls_server( const string& server_pem, const string& ssl_password ):my( new detail::websocket_tls_server_impl(server_pem, ssl_password) ) {}
websocket_tls_server::websocket_tls_server(const string& server_pem,
const string& ssl_password,
bool enable_permessage_deflate /* = true */) :
my( enable_permessage_deflate ?
(detail::abstract_websocket_server*)new detail::websocket_tls_server_impl<detail::asio_tls_stub_log_and_deflate>(server_pem, ssl_password) :
(detail::abstract_websocket_server*)new detail::websocket_tls_server_impl<detail::asio_tls_stub_log>(server_pem, ssl_password) )
{}
websocket_tls_server::~websocket_tls_server(){}
void websocket_tls_server::on_connection( const on_connection_handler& handler )
{
my->_on_connection = handler;
my->on_connection(handler);
}
void websocket_tls_server::listen( uint16_t port )
{
my->_server.listen(port);
my->listen(port);
}
void websocket_tls_server::listen( const fc::ip::endpoint& ep )
{
my->_server.listen( boost::asio::ip::tcp::endpoint( boost::asio::ip::address_v4(uint32_t(ep.get_address())),ep.port()) );
my->listen(ep);
}
void websocket_tls_server::start_accept() {
my->_server.start_accept();
void websocket_tls_server::start_accept()
{
my->start_accept();
}
websocket_tls_client::websocket_tls_client():my( new detail::websocket_tls_client_impl() ) {}
websocket_tls_client::~websocket_tls_client(){ }
websocket_client::websocket_client():my( new detail::websocket_client_impl() ),smy(new detail::websocket_tls_client_impl()) {}
websocket_client::~websocket_client(){ }

View file

@ -63,7 +63,31 @@ namespace fc {
_enqueue_thread();
}
std::exception_ptr e;
try { thread::current().wait_until( ptr(this,true), timeout_us ); }
//
// Create shared_ptr to take ownership of this; i.e. this will
// be deleted when p_this goes out of scope. Consequently,
// it would be Very Bad to let p_this go out of scope
// before we're done reading/writing instance variables!
// See https://github.com/cryptonomex/graphene/issues/597
//
ptr p_this = ptr( this, true );
try
{
//
// We clone p_this here because the wait_until() API requires us
// to use std::move(). I.e. wait_until() takes ownership of any
// pointer passed to it. Since we want to keep ownership ourselves,
// we need to have two shared_ptr's to this:
//
// - p_this to keep this alive until the end of the current function
// - p_this2 to be owned by wait_until() as the wait_until() API requires
//
ptr p_this2 = p_this;
thread::current().wait_until( std::move( p_this2 ), timeout_us );
}
catch (...) { e = std::current_exception(); }
_dequeue_thread();

2
vendor/websocketpp vendored

@ -1 +1 @@
Subproject commit c5510d6de04917812b910a8dd44735c1f17061d9
Subproject commit 378437aecdcb1dfe62096ffd5d944bf1f640ccc3