summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swiften/Network/BoostConnection.cpp12
-rw-r--r--Swiften/Network/BoostConnection.h1
2 files changed, 9 insertions, 4 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp
index 043743e..534ebdb 100644
--- a/Swiften/Network/BoostConnection.cpp
+++ b/Swiften/Network/BoostConnection.cpp
@@ -45,19 +45,19 @@ class SharedBuffer {
private:
boost::shared_ptr< std::vector<char, SafeAllocator<char> > > data_;
boost::asio::const_buffer buffer_;
};
// -----------------------------------------------------------------------------
BoostConnection::BoostConnection(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) :
- eventLoop(eventLoop), ioService(ioService), socket_(*ioService), writing_(false) {
+ eventLoop(eventLoop), ioService(ioService), socket_(*ioService), writing_(false), closeSocketAfterNextWrite_(false) {
}
BoostConnection::~BoostConnection() {
}
void BoostConnection::listen() {
doRead();
}
@@ -69,29 +69,33 @@ void BoostConnection::connect(const HostAddressPort& addressPort) {
boost::bind(&BoostConnection::handleConnectFinished, shared_from_this(), boost::asio::placeholders::error));
}
void BoostConnection::disconnect() {
//MainEventLoop::removeEventsFromOwner(shared_from_this());
// Mac OS X apparently exhibits a problem where closing a socket during a write could potentially go into uninterruptable sleep.
// See e.g. http://bugs.python.org/issue7401
// We therefore wait until any pending write finishes, which hopefully should fix our hang on exit during close().
- while (writing_) {
- Swift::sleep(10);
+ if (writing_) {
+ closeSocketAfterNextWrite_ = true;
+ } else {
+ socket_.close();
}
- socket_.close();
}
void BoostConnection::write(const SafeByteArray& data) {
boost::lock_guard<boost::mutex> lock(writeMutex_);
if (!writing_) {
writing_ = true;
doWrite(data);
+ if (closeSocketAfterNextWrite_) {
+ socket_.close();
+ }
}
else {
append(writeQueue_, data);
}
}
void BoostConnection::doWrite(const SafeByteArray& data) {
boost::asio::async_write(socket_, SharedBuffer(data),
boost::bind(&BoostConnection::handleDataWritten, shared_from_this(), boost::asio::placeholders::error));
diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h
index 7d5ec60..2f0c7be 100644
--- a/Swiften/Network/BoostConnection.h
+++ b/Swiften/Network/BoostConnection.h
@@ -57,11 +57,12 @@ namespace Swift {
private:
EventLoop* eventLoop;
boost::shared_ptr<boost::asio::io_service> ioService;
boost::asio::ip::tcp::socket socket_;
boost::shared_ptr<SafeByteArray> readBuffer_;
boost::mutex writeMutex_;
bool writing_;
SafeByteArray writeQueue_;
+ bool closeSocketAfterNextWrite_;
};
}