summaryrefslogtreecommitdiffstats
path: root/src/Net/Connection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/Connection.cpp')
-rw-r--r--src/Net/Connection.cpp30
1 files changed, 14 insertions, 16 deletions
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<boost::uint8_t> &data) {
@@ -93,7 +93,7 @@ void Connection::handleHeaderReceive(const boost::shared_array<boost::uint8_t> &
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<boost::uint8_t> &da
void Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred, std::size_t length, const boost::function1<void, const boost::shared_array<boost::uint8_t>& > &notify) {
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<void, con
receiving = true;
if(length > 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::function1<void, con
}
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());
+
{
boost::unique_lock<boost::shared_mutex> 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<boost::shared_mutex> 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<boost::shared_mutex> lock(connectionLock);
-
- socket->async_shutdown(boost::bind(&Connection::handleShutdown, this, boost::asio::placeholders::error));
-}
-
}
}