2012-01-02 18:21:07 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Note: this file is licensed under the Apache License instead of GPL because
|
|
|
|
// it is used by the Spotify blob which links against libspotify and is not GPL
|
|
|
|
// compatible.
|
|
|
|
|
|
|
|
#ifndef MESSAGEHANDLER_H
|
|
|
|
#define MESSAGEHANDLER_H
|
|
|
|
|
|
|
|
#include <QBuffer>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QObject>
|
2012-01-06 22:27:02 +01:00
|
|
|
#include <QSemaphore>
|
2012-01-02 18:21:07 +01:00
|
|
|
#include <QThread>
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/messagereply.h"
|
|
|
|
|
2012-01-02 18:21:07 +01:00
|
|
|
class QAbstractSocket;
|
|
|
|
class QIODevice;
|
|
|
|
class QLocalSocket;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#define QStringFromStdString(x) QString::fromUtf8(x.data(), x.size())
|
|
|
|
#define DataCommaSizeFromQString(x) x.toUtf8().constData(), x.toUtf8().length()
|
2012-01-02 18:21:07 +01:00
|
|
|
|
|
|
|
// Reads and writes uint32 length encoded protobufs to a socket.
|
|
|
|
// This base QObject is separate from AbstractMessageHandler because moc can't
|
|
|
|
// handle templated classes. Use AbstractMessageHandler instead.
|
|
|
|
class _MessageHandlerBase : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2012-01-08 00:30:33 +01:00
|
|
|
// device can be NULL, in which case you must call SetDevice before writing
|
|
|
|
// any messages.
|
2012-01-02 18:21:07 +01:00
|
|
|
_MessageHandlerBase(QIODevice* device, QObject* parent);
|
|
|
|
|
2012-01-08 00:30:33 +01:00
|
|
|
void SetDevice(QIODevice* device);
|
|
|
|
|
2012-01-22 19:42:40 +01:00
|
|
|
// After this is true, messages cannot be sent to the handler any more.
|
|
|
|
bool is_device_closed() const { return is_device_closed_; }
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected slots:
|
2012-01-02 18:21:07 +01:00
|
|
|
void WriteMessage(const QByteArray& data);
|
|
|
|
void DeviceReadyRead();
|
2012-01-22 19:42:40 +01:00
|
|
|
virtual void DeviceClosed();
|
2012-01-02 18:21:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2012-01-08 00:30:33 +01:00
|
|
|
virtual bool RawMessageArrived(const QByteArray& data) = 0;
|
2012-01-22 19:42:40 +01:00
|
|
|
virtual void AbortAll() = 0;
|
2012-01-02 18:21:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2012-01-02 18:21:07 +01:00
|
|
|
typedef bool (QAbstractSocket::*FlushAbstractSocket)();
|
|
|
|
typedef bool (QLocalSocket::*FlushLocalSocket)();
|
|
|
|
|
|
|
|
QIODevice* device_;
|
|
|
|
FlushAbstractSocket flush_abstract_socket_;
|
|
|
|
FlushLocalSocket flush_local_socket_;
|
|
|
|
|
|
|
|
bool reading_protobuf_;
|
|
|
|
quint32 expected_length_;
|
|
|
|
QBuffer buffer_;
|
2012-01-22 19:42:40 +01:00
|
|
|
|
|
|
|
bool is_device_closed_;
|
2012-01-02 18:21:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Reads and writes uint32 length encoded MessageType messages to a socket.
|
|
|
|
// You should subclass this and implement the MessageArrived(MessageType)
|
|
|
|
// method.
|
2012-01-22 19:41:26 +01:00
|
|
|
template <typename MT>
|
2012-01-02 18:21:07 +01:00
|
|
|
class AbstractMessageHandler : public _MessageHandlerBase {
|
2014-02-07 16:34:20 +01:00
|
|
|
public:
|
2012-01-02 18:21:07 +01:00
|
|
|
AbstractMessageHandler(QIODevice* device, QObject* parent);
|
2012-01-22 19:42:40 +01:00
|
|
|
~AbstractMessageHandler() { AbortAll(); }
|
2012-01-02 18:21:07 +01:00
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
typedef MT MessageType;
|
|
|
|
typedef MessageReply<MT> ReplyType;
|
2012-01-02 18:21:07 +01:00
|
|
|
|
|
|
|
// Serialises the message and writes it to the socket. This version MUST be
|
|
|
|
// called from the thread in which the AbstractMessageHandler was created.
|
|
|
|
void SendMessage(const MessageType& message);
|
|
|
|
|
|
|
|
// Serialises the message and writes it to the socket. This version may be
|
|
|
|
// called from any thread.
|
|
|
|
void SendMessageAsync(const MessageType& message);
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
// Sends the request message inside and takes ownership of the MessageReply.
|
|
|
|
// The MessageReply's Finished() signal will be emitted when a reply arrives
|
|
|
|
// with the same ID. Must be called from my thread.
|
|
|
|
void SendRequest(ReplyType* reply);
|
2012-01-02 18:21:07 +01:00
|
|
|
|
|
|
|
// Sets the "id" field of reply to the same as the request, and sends the
|
|
|
|
// reply on the socket. Used on the worker side.
|
|
|
|
void SendReply(const MessageType& request, MessageType* reply);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
protected:
|
2012-01-02 18:21:07 +01:00
|
|
|
// Called when a message is received from the socket.
|
|
|
|
virtual void MessageArrived(const MessageType& message) {}
|
|
|
|
|
|
|
|
// _MessageHandlerBase
|
2012-01-08 00:30:33 +01:00
|
|
|
bool RawMessageArrived(const QByteArray& data);
|
2012-01-22 19:42:40 +01:00
|
|
|
void AbortAll();
|
2012-01-02 18:21:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
private:
|
2012-01-02 18:21:07 +01:00
|
|
|
QMap<int, ReplyType*> pending_replies_;
|
|
|
|
};
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
|
|
|
AbstractMessageHandler<MT>::AbstractMessageHandler(QIODevice* device,
|
|
|
|
QObject* parent)
|
|
|
|
: _MessageHandlerBase(device, parent) {}
|
2012-01-02 18:21:07 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
2012-01-22 19:41:26 +01:00
|
|
|
void AbstractMessageHandler<MT>::SendMessage(const MessageType& message) {
|
2012-01-02 18:21:07 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
|
|
|
|
|
|
|
std::string data = message.SerializeAsString();
|
|
|
|
WriteMessage(QByteArray(data.data(), data.size()));
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
2012-01-22 19:41:26 +01:00
|
|
|
void AbstractMessageHandler<MT>::SendMessageAsync(const MessageType& message) {
|
2012-01-02 18:21:07 +01:00
|
|
|
std::string data = message.SerializeAsString();
|
2014-02-07 16:34:20 +01:00
|
|
|
metaObject()->invokeMethod(
|
|
|
|
this, "WriteMessage", Qt::QueuedConnection,
|
|
|
|
Q_ARG(QByteArray, QByteArray(data.data(), data.size())));
|
2012-01-02 18:21:07 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
2012-01-22 19:41:26 +01:00
|
|
|
void AbstractMessageHandler<MT>::SendRequest(ReplyType* reply) {
|
|
|
|
pending_replies_[reply->id()] = reply;
|
|
|
|
SendMessage(reply->request_message());
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
2012-01-22 19:41:26 +01:00
|
|
|
void AbstractMessageHandler<MT>::SendReply(const MessageType& request,
|
|
|
|
MessageType* reply) {
|
2012-01-02 18:21:07 +01:00
|
|
|
reply->set_id(request.id());
|
|
|
|
SendMessage(*reply);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
2012-01-22 19:41:26 +01:00
|
|
|
bool AbstractMessageHandler<MT>::RawMessageArrived(const QByteArray& data) {
|
2012-01-02 18:21:07 +01:00
|
|
|
MessageType message;
|
|
|
|
if (!message.ParseFromArray(data.constData(), data.size())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:41:26 +01:00
|
|
|
ReplyType* reply = pending_replies_.take(message.id());
|
2012-01-02 18:21:07 +01:00
|
|
|
|
|
|
|
if (reply) {
|
|
|
|
// This is a reply to a message that we created earlier.
|
|
|
|
reply->SetReply(message);
|
|
|
|
} else {
|
|
|
|
MessageArrived(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
template <typename MT>
|
2012-01-22 19:42:40 +01:00
|
|
|
void AbstractMessageHandler<MT>::AbortAll() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (ReplyType* reply : pending_replies_) {
|
|
|
|
reply->Abort();
|
|
|
|
}
|
2012-01-02 18:21:07 +01:00
|
|
|
pending_replies_.clear();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
#endif // MESSAGEHANDLER_H
|