diff --git a/ext/clementine-spotifyblob/spotifyclient.cpp b/ext/clementine-spotifyblob/spotifyclient.cpp index 3bae1c4ed..2af08a28d 100644 --- a/ext/clementine-spotifyblob/spotifyclient.cpp +++ b/ext/clementine-spotifyblob/spotifyclient.cpp @@ -963,8 +963,8 @@ void SpotifyClient::AlbumBrowseComplete(sp_albumbrowse* result, void* userdata) sp_albumbrowse_release(result); } -void SpotifyClient::SocketClosed() { - AbstractMessageHandler::SocketClosed(); +void SpotifyClient::DeviceClosed() { + AbstractMessageHandler::DeviceClosed(); qApp->exit(); } diff --git a/ext/clementine-spotifyblob/spotifyclient.h b/ext/clementine-spotifyblob/spotifyclient.h index 5ddd7c1c6..d4ebcf6c7 100644 --- a/ext/clementine-spotifyblob/spotifyclient.h +++ b/ext/clementine-spotifyblob/spotifyclient.h @@ -50,7 +50,7 @@ public: protected: void MessageArrived(const pb::spotify::Message& message); - void SocketClosed(); + void DeviceClosed(); private slots: void ProcessEvents(); diff --git a/ext/clementine-tagreader/tagreaderworker.cpp b/ext/clementine-tagreader/tagreaderworker.cpp index d0b6c3ff8..42fd8404d 100644 --- a/ext/clementine-tagreader/tagreaderworker.cpp +++ b/ext/clementine-tagreader/tagreaderworker.cpp @@ -559,8 +559,8 @@ QByteArray TagReaderWorker::LoadEmbeddedArt(const QString& filename) const { return QByteArray(); } -void TagReaderWorker::SocketClosed() { - AbstractMessageHandler::SocketClosed(); +void TagReaderWorker::DeviceClosed() { + AbstractMessageHandler::DeviceClosed(); qApp->exit(); } diff --git a/ext/clementine-tagreader/tagreaderworker.h b/ext/clementine-tagreader/tagreaderworker.h index acb2a1f4e..021600bb0 100644 --- a/ext/clementine-tagreader/tagreaderworker.h +++ b/ext/clementine-tagreader/tagreaderworker.h @@ -41,7 +41,7 @@ public: protected: void MessageArrived(const pb::tagreader::Message& message); - void SocketClosed(); + void DeviceClosed(); private: void ReadFile(const QString& filename, pb::tagreader::SongMetadata* song) const; diff --git a/ext/libclementine-common/core/messagehandler.cpp b/ext/libclementine-common/core/messagehandler.cpp index c22cd4d6c..e21fc2974 100644 --- a/ext/libclementine-common/core/messagehandler.cpp +++ b/ext/libclementine-common/core/messagehandler.cpp @@ -31,7 +31,8 @@ _MessageHandlerBase::_MessageHandlerBase(QIODevice* device, QObject* parent) flush_abstract_socket_(NULL), flush_local_socket_(NULL), reading_protobuf_(false), - expected_length_(0) { + expected_length_(0), + is_device_closed_(false) { if (device) { SetDevice(device); } @@ -47,10 +48,10 @@ void _MessageHandlerBase::SetDevice(QIODevice* device) { // Yeah I know. if (QAbstractSocket* socket = qobject_cast(device)) { flush_abstract_socket_ = &QAbstractSocket::flush; - connect(socket, SIGNAL(disconnected()), SLOT(SocketClosed())); + connect(socket, SIGNAL(disconnected()), SLOT(DeviceClosed())); } else if (QLocalSocket* socket = qobject_cast(device)) { flush_local_socket_ = &QLocalSocket::flush; - connect(socket, SIGNAL(disconnected()), SLOT(SocketClosed())); + connect(socket, SIGNAL(disconnected()), SLOT(DeviceClosed())); } else { qFatal("Unsupported device type passed to _MessageHandlerBase"); } @@ -100,4 +101,7 @@ void _MessageHandlerBase::WriteMessage(const QByteArray& data) { } } +void _MessageHandlerBase::DeviceClosed() { + is_device_closed_ = true; + AbortAll(); } diff --git a/ext/libclementine-common/core/messagehandler.h b/ext/libclementine-common/core/messagehandler.h index 1d2cdc2ff..cd2ec285b 100644 --- a/ext/libclementine-common/core/messagehandler.h +++ b/ext/libclementine-common/core/messagehandler.h @@ -56,13 +56,17 @@ public: void SetDevice(QIODevice* device); + // After this is true, messages cannot be sent to the handler any more. + bool is_device_closed() const { return is_device_closed_; } + protected slots: void WriteMessage(const QByteArray& data); void DeviceReadyRead(); - virtual void SocketClosed() {} + virtual void DeviceClosed(); protected: virtual bool RawMessageArrived(const QByteArray& data) = 0; + virtual void AbortAll() = 0; protected: typedef bool (QAbstractSocket::*FlushAbstractSocket)(); @@ -75,6 +79,8 @@ protected: bool reading_protobuf_; quint32 expected_length_; QBuffer buffer_; + + bool is_device_closed_; }; @@ -85,6 +91,7 @@ template class AbstractMessageHandler : public _MessageHandlerBase { public: AbstractMessageHandler(QIODevice* device, QObject* parent); + ~AbstractMessageHandler() { AbortAll(); } typedef MT MessageType; typedef MessageReply ReplyType; @@ -112,7 +119,7 @@ protected: // _MessageHandlerBase bool RawMessageArrived(const QByteArray& data); - void SocketClosed(); + void AbortAll(); private: QMap pending_replies_; @@ -174,9 +181,7 @@ bool AbstractMessageHandler::RawMessageArrived(const QByteArray& data) { } template -void AbstractMessageHandler::SocketClosed() { - QMutexLocker l(&mutex_); - +void AbstractMessageHandler::AbortAll() { foreach (ReplyType* reply, pending_replies_) { reply->Abort(); } diff --git a/ext/libclementine-common/core/workerpool.h b/ext/libclementine-common/core/workerpool.h index b499dd516..3df6d65c3 100644 --- a/ext/libclementine-common/core/workerpool.h +++ b/ext/libclementine-common/core/workerpool.h @@ -390,7 +390,8 @@ HandlerType* WorkerPool::NextHandler() const { for (int i=0 ; iis_device_closed()) { next_worker_ = (worker_index + 1) % workers_.count(); return workers_[worker_index].handler_; }