From 59bec05e406a0cf55c52d13cecfe76dccf83cd19 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 27 Aug 2009 02:51:16 +0200 Subject: =?UTF-8?q?Net::Connection=20etc.:=20Einige=20Race=20Conditions=20?= =?UTF-8?q?gefixt=20Keine=20sporadischen=20Abst=C3=BCrze=20mehr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Net/Connection.cpp | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'src/Net/Connection.cpp') diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 036c3d8..fc917d4 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -29,8 +29,8 @@ namespace Net { Connection::~Connection() { - if(_isConnected()) - doDisconnect(); + doDisconnect(); + waitWhileConnected(); } void Connection::handleHandshake(const boost::system::error_code& error) { @@ -77,7 +77,7 @@ void Connection::enterReceiveLoop() { return; } - rawReceive(sizeof(Packet::Header), boost::bind(&Connection::handleHeaderReceive, this, _1)); + rawReceive(sizeof(Packet::Header), boost::bind(&Connection::handleHeaderReceive, thisPtr.lock(), _1)); } void Connection::handleHeaderReceive(const boost::shared_array &data) { @@ -93,7 +93,7 @@ void Connection::handleHeaderReceive(const boost::shared_array & enterReceiveLoop(); } else { - rawReceive(ntohs(header.length), boost::bind(&Connection::handleDataReceive, this, _1)); + rawReceive(ntohs(header.length), boost::bind(&Connection::handleDataReceive, thisPtr.lock(), _1)); } } @@ -109,7 +109,10 @@ void Connection::handleDataReceive(const boost::shared_array &da void Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred, std::size_t length, const boost::function1& > ¬ify) { if(error || (bytes_transferred+received) < length) { - application->logf(Core::LoggerBase::LOG_VERBOSE, "Read error: %s", error.message().c_str()); + if(error == boost::system::errc::operation_canceled) + return; + + application->logf(Core::LoggerBase::LOG_DEFAULT, "Read error: %s", error.message().c_str()); // TODO Error doDisconnect(); @@ -155,8 +158,8 @@ void Connection::rawReceive(std::size_t length, const boost::function1 received) { - boost::asio::async_read(*socket, boost::asio::buffer(receiveBuffer->data()+received, receiveBuffer->size()-received), boost::asio::transfer_at_least(length), - boost::bind(&Connection::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, + boost::asio::async_read(socket, boost::asio::buffer(receiveBuffer->data()+received, receiveBuffer->size()-received), boost::asio::transfer_at_least(length), + boost::bind(&Connection::handleRead, thisPtr.lock(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, length, notify)); return; @@ -169,6 +172,9 @@ void Connection::rawReceive(std::size_t length, const boost::function1logf(Core::LoggerBase::LOG_VERBOSE, "Write error: %s", error.message().c_str()); + { boost::unique_lock lock(connectionLock); @@ -182,8 +188,6 @@ void Connection::handleWrite(const boost::system::error_code& error, std::size_t } if(error) { - application->logf(Core::LoggerBase::LOG_VERBOSE, "Write error: %s", error.message().c_str()); - // TODO Error doDisconnect(); } @@ -199,7 +203,7 @@ void Connection::rawSend(const boost::uint8_t *data, std::size_t length) { boost::upgrade_to_unique_lock upgradeLock(lock); sending++; - boost::asio::async_write(*socket, Buffer(data, length), boost::bind(&Connection::handleWrite, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + boost::asio::async_write(socket, Buffer(data, length), boost::bind(&Connection::handleWrite, thisPtr.lock(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } } @@ -229,11 +233,5 @@ void Connection::disconnect() { doDisconnect(); } -void Connection::doDisconnect() { - boost::lock_guard lock(connectionLock); - - socket->async_shutdown(boost::bind(&Connection::handleShutdown, this, boost::asio::placeholders::error)); -} - } } -- cgit v1.2.3