From cde82c809144edfc6bc6cff558d80091a06d840d Mon Sep 17 00:00:00 2001 From: "ssf.developer" Date: Wed, 14 Sep 2016 01:26:17 +0200 Subject: [PATCH] #26 Fix client on Linux --- src/core/client/client.h | 4 +- src/core/client/client.ipp | 16 ++--- .../data_link/basic_circuit_socket_service.h | 23 ++++++ .../proxy/basic_proxy_acceptor_service.h | 52 ++++++++++++-- .../layer/proxy/basic_proxy_socket_service.h | 70 +++++++++++++++++-- 5 files changed, 143 insertions(+), 22 deletions(-) diff --git a/src/core/client/client.h b/src/core/client/client.h index 3bc0c0fc..5e29a0c8 100644 --- a/src/core/client/client.h +++ b/src/core/client/client.h @@ -66,10 +66,10 @@ class SSFClient BaseUserServicePtr p_user_service, boost::system::error_code ec); private: - NetworkSocketPtr p_socket_; - AsyncEngine async_engine_; + NetworkSocketPtr p_socket_; + Demux fiber_demux_; std::vector user_services_; diff --git a/src/core/client/client.ipp b/src/core/client/client.ipp index f6fcf1e2..5c30ddf1 100644 --- a/src/core/client/client.ipp +++ b/src/core/client/client.ipp @@ -30,8 +30,8 @@ SSFClient::SSFClient(std::vector user_services, ClientCallback callback) : T( boost::bind(&SSFClient::DoSSFStart, this, _1, _2)), - p_socket_(nullptr), async_engine_(), + p_socket_(nullptr), fiber_demux_(async_engine_.get_io_service()), user_services_(user_services), services_config_(services_config), @@ -77,15 +77,16 @@ void SSFClient::Stop() { return; } + fiber_demux_.close(); + if (p_socket_.get() != nullptr) { boost::system::error_code close_ec; + p_socket_->shutdown(boost::asio::socket_base::shutdown_both, close_ec); p_socket_->close(close_ec); - p_socket_.reset(); } - fiber_demux_.close(); - async_engine_.Stop(); + p_socket_.reset(); } template class T> @@ -96,18 +97,13 @@ boost::asio::io_service& SSFClient::get_io_service() { template class T> void SSFClient::NetworkToTransport(const boost::system::error_code& ec) { if (!ec) { - this->DoSSFInitiate(std::move(p_socket_)); + this->DoSSFInitiate(p_socket_); return; } SSF_LOG(kLogError) << "client: error when connecting to server: " << ec.message(); - if (p_socket_) { - boost::system::error_code close_ec; - p_socket_->close(close_ec); - } - Notify(ssf::services::initialisation::NETWORK, nullptr, ec); } diff --git a/src/framework/ssf/layer/data_link/basic_circuit_socket_service.h b/src/framework/ssf/layer/data_link/basic_circuit_socket_service.h index 53af3965..e5e3e021 100644 --- a/src/framework/ssf/layer/data_link/basic_circuit_socket_service.h +++ b/src/framework/ssf/layer/data_link/basic_circuit_socket_service.h @@ -73,6 +73,12 @@ class basic_CircuitSocket_service boost::system::error_code open(implementation_type& impl, const protocol_type& protocol, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + return impl.p_next_layer_socket->open(typename protocol_type::next_layer_protocol(), ec); } @@ -87,6 +93,10 @@ class basic_CircuitSocket_service } bool is_open(const implementation_type& impl) const { + if (!impl.p_next_layer_socket) { + return false; + } + return impl.p_next_layer_socket->is_open(); } @@ -114,6 +124,12 @@ class basic_CircuitSocket_service boost::system::error_code close(implementation_type& impl, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + return impl.p_next_layer_socket->close(ec); } @@ -123,6 +139,11 @@ class basic_CircuitSocket_service bool at_mark(const implementation_type& impl, boost::system::error_code& ec) const { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return false; + } return impl.p_next_layer_socket->at_mark(ec); } @@ -140,6 +161,8 @@ class basic_CircuitSocket_service boost::system::error_code cancel(implementation_type& impl, boost::system::error_code& ec) { if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); return ec; } diff --git a/src/framework/ssf/layer/proxy/basic_proxy_acceptor_service.h b/src/framework/ssf/layer/proxy/basic_proxy_acceptor_service.h index 390e374a..57b152ed 100644 --- a/src/framework/ssf/layer/proxy/basic_proxy_acceptor_service.h +++ b/src/framework/ssf/layer/proxy/basic_proxy_acceptor_service.h @@ -97,6 +97,10 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base< } bool is_open(const implementation_type& impl) const { + if (!impl.p_next_layer_acceptor) { + return false; + } + return impl.p_next_layer_acceptor->is_open(); } @@ -113,6 +117,12 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base< boost::system::error_code close(implementation_type& impl, boost::system::error_code& ec) { + if (!impl.p_next_layer_acceptor) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + return impl.p_next_layer_acceptor->close(ec); } @@ -124,22 +134,38 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base< template boost::system::error_code set_option(implementation_type& impl, const SettableSocketOption& option, - boost::system::error_code& ec) { - if (impl.p_next_layer_acceptor) { - return impl.p_next_layer_acceptor->set_option(option, ec); + boost::system::error_code& ec) { + if (!impl.p_next_layer_acceptor) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; } - return ec; + + return impl.p_next_layer_acceptor->set_option(option, ec); + } boost::system::error_code bind(implementation_type& impl, const endpoint_type& endpoint, boost::system::error_code& ec) { + if (!impl.p_next_layer_acceptor) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + impl.p_local_endpoint = std::make_shared(endpoint); return impl.p_next_layer_acceptor->bind(endpoint.next_layer_endpoint(), ec); } boost::system::error_code listen(implementation_type& impl, int backlog, - boost::system::error_code& ec) { + boost::system::error_code& ec) { + if (!impl.p_next_layer_acceptor) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + return impl.p_next_layer_acceptor->listen(backlog, ec); } @@ -149,7 +175,13 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base< boost::asio::basic_socket& peer, endpoint_type* p_peer_endpoint, boost::system::error_code& ec, typename std::enable_if::value>::type* = 0) { + protocol_type, Protocol1>::value>::type* = 0) { + if (!impl.p_next_layer_acceptor) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + auto& peer_impl = peer.native_handle(); peer_impl.p_remote_endpoint = std::make_shared(); @@ -183,6 +215,14 @@ class basic_ProxyAcceptor_service : public boost::asio::detail::service_base< void(boost::system::error_code)> init(std::forward(handler)); + if (!impl.p_next_layer_acceptor) { + io::PostHandler( + this->get_io_service(), init.handler, + boost::system::error_code(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category())); + return init.result.get(); + } + auto& peer_impl = peer.native_handle(); peer_impl.p_local_endpoint = std::make_shared(); diff --git a/src/framework/ssf/layer/proxy/basic_proxy_socket_service.h b/src/framework/ssf/layer/proxy/basic_proxy_socket_service.h index 46eddb46..4daa23ef 100644 --- a/src/framework/ssf/layer/proxy/basic_proxy_socket_service.h +++ b/src/framework/ssf/layer/proxy/basic_proxy_socket_service.h @@ -86,6 +86,10 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< } bool is_open(const implementation_type& impl) const { + if (!impl.p_next_layer_socket) { + return false; + } + return impl.p_next_layer_socket->is_open(); } @@ -113,6 +117,11 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< boost::system::error_code close(implementation_type& impl, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::broken_pipe, ssf::error::get_ssf_category()); + return ec; + } + return impl.p_next_layer_socket->close(ec); } @@ -122,11 +131,22 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< boost::system::error_code cancel(implementation_type& impl, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return ec; + } + return impl.p_next_layer_socket->cancel(ec); } bool at_mark(const implementation_type& impl, boost::system::error_code& ec) const { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return false; + } return impl.p_next_layer_socket->at_mark(ec); } @@ -187,6 +207,12 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< const ConstBufferSequence& buffers, boost::asio::socket_base::message_flags flags, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::bad_file_descriptor, + ssf::error::get_ssf_category()); + return 0; + } + return impl.p_next_layer_socket->send(buffers, flags, ec); } @@ -196,8 +222,21 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< async_send(implementation_type& impl, const ConstBufferSequence& buffers, boost::asio::socket_base::message_flags flags, WriteHandler&& handler) { - return impl.p_next_layer_socket->async_send( - buffers, std::forward(handler)); + boost::asio::detail::async_result_init< + WriteHandler, void(boost::system::error_code, std::size_t)> + init(std::forward(handler)); + + if (!impl.p_next_layer_socket) { + io::PostHandler(this->get_io_service(), init.handler, + boost::system::error_code(ssf::error::broken_pipe, + ssf::error::get_ssf_category()), + 0); + return init.result.get(); + } + + impl.p_next_layer_socket->async_send(buffers, init.handler); + + return init.result.get(); } template @@ -205,6 +244,11 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< const MutableBufferSequence& buffers, boost::asio::socket_base::message_flags flags, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::broken_pipe, ssf::error::get_ssf_category()); + return 0; + } + return impl.p_next_layer_socket->receive(buffers, flags, ec); } @@ -215,13 +259,31 @@ class basic_ProxySocket_service : public boost::asio::detail::service_base< const MutableBufferSequence& buffers, boost::asio::socket_base::message_flags flags, ReadHandler&& handler) { - return impl.p_next_layer_socket->async_receive( - buffers, std::forward(handler)); + boost::asio::detail::async_result_init< + ReadHandler, void(boost::system::error_code, std::size_t)> + init(std::forward(handler)); + + if (!impl.p_next_layer_socket) { + io::PostHandler(this->get_io_service(), init.handler, + boost::system::error_code(ssf::error::broken_pipe, + ssf::error::get_ssf_category()), + 0); + return init.result.get(); + } + + impl.p_next_layer_socket->async_receive(buffers, init.handler); + + return init.result.get(); } boost::system::error_code shutdown( implementation_type& impl, boost::asio::socket_base::shutdown_type what, boost::system::error_code& ec) { + if (!impl.p_next_layer_socket) { + ec.assign(ssf::error::broken_pipe, ssf::error::get_ssf_category()); + return ec; + } + impl.p_next_layer_socket->shutdown(what, ec); return ec;