From 7f606328a16a14e6e1190a440caa2cb2e619127b Mon Sep 17 00:00:00 2001 From: Helge Norberg Date: Tue, 4 Apr 2017 17:21:29 +0200 Subject: [PATCH] [AsyncEventServer] Fixed bug where server expected to be the one closing the socket after a BYE command, when in fact the client might just as well be the one that closes first. --- protocol/util/AsyncEventServer.cpp | 46 +++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/protocol/util/AsyncEventServer.cpp b/protocol/util/AsyncEventServer.cpp index d404becdb..cdfe53834 100644 --- a/protocol/util/AsyncEventServer.cpp +++ b/protocol/util/AsyncEventServer.cpp @@ -40,17 +40,17 @@ using boost::asio::ip::tcp; namespace caspar { namespace IO { - + class connection; typedef std::set> connection_set; class connection : public spl::enable_shared_from_this -{ +{ typedef tbb::concurrent_hash_map> lifecycle_map_type; typedef tbb::concurrent_queue send_queue; - const spl::shared_ptr socket_; + const spl::shared_ptr socket_; std::shared_ptr service_; const std::wstring listen_port_; const spl::shared_ptr connection_set_; @@ -142,7 +142,7 @@ public: { return u16(socket_->local_endpoint().address().to_string()); } - + std::wstring ipv4_address() const { return socket_->is_open() ? u16(socket_->remote_endpoint().address().to_string()) : L"no-address"; @@ -198,18 +198,12 @@ private: CASPAR_LOG(info) << print() << L" Client " << ipv4_address() << L" disconnected (" << connection_set_->size() << L" connections)."; - try - { - socket_->cancel(); - socket_->close(); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - } + boost::system::error_code ec; + socket_->shutdown(boost::asio::socket_base::shutdown_type::shutdown_both, ec); + socket_->close(ec); } - connection(const std::shared_ptr& service, const spl::shared_ptr& socket, const protocol_strategy_factory::ptr& protocol_factory, const spl::shared_ptr& connection_set) + connection(const std::shared_ptr& service, const spl::shared_ptr& socket, const protocol_strategy_factory::ptr& protocol_factory, const spl::shared_ptr& connection_set) : socket_(socket) , service_(service) , listen_port_(socket_->is_open() ? boost::lexical_cast(socket_->local_endpoint().port()) : L"no-port") @@ -219,9 +213,9 @@ private: { CASPAR_LOG(info) << print() << L" Accepted connection from " << ipv4_address() << L" (" << (connection_set_->size() + 1) << L" connections)."; } - + void handle_read(const boost::system::error_code& error, size_t bytes_transferred) //always called from the asio-service-thread - { + { if(!error) { try @@ -234,11 +228,11 @@ private: { CASPAR_LOG_CURRENT_EXCEPTION(); } - + read_some(); - } + } else if (error != boost::asio::error::operation_aborted) - stop(); + stop(); } void handle_write(const spl::shared_ptr& str, const boost::system::error_code& error, size_t bytes_transferred) //always called from the asio-service-thread @@ -256,7 +250,7 @@ private: do_write(); } } - else if (error != boost::asio::error::operation_aborted && socket_->is_open()) + else if (error != boost::asio::error::operation_aborted && socket_->is_open()) stop(); } @@ -264,7 +258,7 @@ private: { socket_->async_read_some(boost::asio::buffer(data_.data(), data_.size()), std::bind(&connection::handle_read, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); } - + void write_some(std::string&& data) //always called from the asio-service-thread { is_writing_ = true; @@ -315,18 +309,18 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_thisstop(); }); } - - void start_accept() + + void start_accept() { spl::shared_ptr socket(new tcp::socket(*service_)); acceptor_.async_accept(*socket, std::bind(&implementation::handle_accept, shared_from_this(), socket, std::placeholders::_1)); } - void handle_accept(const spl::shared_ptr& socket, const boost::system::error_code& error) + void handle_accept(const spl::shared_ptr& socket, const boost::system::error_code& error) { if (!acceptor_.is_open()) return; - + if (!error) { boost::system::error_code ec; @@ -334,7 +328,7 @@ struct AsyncEventServer::implementation : public spl::enable_shared_from_thisinsert(conn); -- 2.39.2