From dff2fe3b92bfcc2e59e0bcca43f8907590be75de Mon Sep 17 00:00:00 2001 From: neverchanje Date: Tue, 9 Oct 2018 23:43:28 +0800 Subject: [PATCH] rpc: fix pegasus-186 --- src/core/tools/common/asio_net_provider.cpp | 47 ++++++++++++--------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/src/core/tools/common/asio_net_provider.cpp b/src/core/tools/common/asio_net_provider.cpp index 8eb508e995..c6a544ac23 100644 --- a/src/core/tools/common/asio_net_provider.cpp +++ b/src/core/tools/common/asio_net_provider.cpp @@ -67,7 +67,9 @@ error_code asio_network_provider::start(rpc_channel channel, int port, bool clie task_worker::set_name(buffer); boost::asio::io_service::work work(_io_service); - _io_service.run(); + boost::system::error_code ec; + _io_service.run(ec); + dassert(false, "boost::asio::io_service run failed: err(%s)", ec.message().data()); }))); } @@ -127,21 +129,26 @@ void asio_network_provider::do_accept() _acceptor->async_accept(*socket, [this, socket](boost::system::error_code ec) { if (!ec) { - auto ip = socket->remote_endpoint().address().to_v4().to_ulong(); - auto port = socket->remote_endpoint().port(); - ::dsn::rpc_address client_addr(ip, port); - - message_parser_ptr null_parser; - rpc_session_ptr s = - new asio_rpc_session(*this, - client_addr, - (std::shared_ptr &)socket, - null_parser, - false); - on_server_session_accepted(s); - - // we should start read immediately after the rpc session is completely created. - s->start_read_next(); + auto remote = socket->remote_endpoint(ec); + if (ec) { + derror("failed to get the remote endpoint: %s", ec.message().data()); + } else { + auto ip = remote.address().to_v4().to_ulong(); + auto port = remote.port(); + ::dsn::rpc_address client_addr(ip, port); + + message_parser_ptr null_parser; + rpc_session_ptr s = + new asio_rpc_session(*this, + client_addr, + (std::shared_ptr &)socket, + null_parser, + false); + on_server_session_accepted(s); + + // we should start read immediately after the rpc session is completely created. + s->start_read_next(); + } } do_accept(); @@ -295,8 +302,8 @@ error_code asio_udp_provider::start(rpc_channel channel, int port, bool client_o // refactored _address.assign_ipv4(get_local_ipv4(), std::numeric_limits::max() - - rand::next_u64(std::numeric_limits::min(), - std::numeric_limits::max()) % + rand::next_u64(std::numeric_limits::min(), + std::numeric_limits::max()) % 5000); ::boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::address_v4::any(), _address.port()); @@ -350,7 +357,9 @@ error_code asio_udp_provider::start(rpc_channel channel, int port, bool client_o task_worker::set_name(buffer); boost::asio::io_service::work work(_io_service); - _io_service.run(); + boost::system::error_code ec; + _io_service.run(ec); + dassert(false, "boost::asio::io_service run failed: err(%s)", ec.message().data()); }))); }