2018-02-27 18:06:05 +01:00
|
|
|
/* This file is part of Strawberry.
|
|
|
|
Copyright 2011, David Sansome <me@davidsansome.com>
|
2021-04-10 07:32:38 +02:00
|
|
|
Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef MESSAGEHANDLER_H
|
|
|
|
#define MESSAGEHANDLER_H
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <QtGlobal>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QThread>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QBuffer>
|
|
|
|
#include <QByteArray>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QString>
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <QAbstractSocket>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
#include "core/messagereply.h"
|
|
|
|
|
2020-02-08 03:40:30 +01:00
|
|
|
class QIODevice;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Reads and writes uint32 length encoded protobufs to a socket.
|
2019-04-12 19:55:19 +02:00
|
|
|
// This base QObject is separate from AbstractMessageHandler because moc can't handle templated classes.
|
|
|
|
// Use AbstractMessageHandler instead.
|
2018-02-27 18:06:05 +01:00
|
|
|
class _MessageHandlerBase : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
2020-11-19 18:22:57 +01:00
|
|
|
public:
|
2019-02-20 21:29:14 +01:00
|
|
|
// device can be nullptr, in which case you must call SetDevice before writing any messages.
|
2018-05-01 00:41:33 +02:00
|
|
|
_MessageHandlerBase(QIODevice *device, QObject *parent);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
void SetDevice(QIODevice *device);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// After this is true, messages cannot be sent to the handler any more.
|
|
|
|
bool is_device_closed() const { return is_device_closed_; }
|
|
|
|
|
2020-11-19 18:22:57 +01:00
|
|
|
protected slots:
|
2018-05-01 00:41:33 +02:00
|
|
|
void WriteMessage(const QByteArray &data);
|
2018-02-27 18:06:05 +01:00
|
|
|
void DeviceReadyRead();
|
|
|
|
virtual void DeviceClosed();
|
|
|
|
|
2020-11-19 18:22:57 +01:00
|
|
|
protected:
|
2018-05-01 00:41:33 +02:00
|
|
|
virtual bool RawMessageArrived(const QByteArray &data) = 0;
|
2018-02-27 18:06:05 +01:00
|
|
|
virtual void AbortAll() = 0;
|
|
|
|
|
2020-11-19 18:22:57 +01:00
|
|
|
protected:
|
2018-02-27 18:06:05 +01:00
|
|
|
typedef bool (QAbstractSocket::*FlushAbstractSocket)();
|
|
|
|
typedef bool (QLocalSocket::*FlushLocalSocket)();
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
QIODevice *device_;
|
2018-02-27 18:06:05 +01:00
|
|
|
FlushAbstractSocket flush_abstract_socket_;
|
|
|
|
FlushLocalSocket flush_local_socket_;
|
|
|
|
|
|
|
|
bool reading_protobuf_;
|
|
|
|
quint32 expected_length_;
|
|
|
|
QBuffer buffer_;
|
|
|
|
|
|
|
|
bool is_device_closed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Reads and writes uint32 length encoded MessageType messages to a socket.
|
2019-04-12 19:55:19 +02:00
|
|
|
// You should subclass this and implement the MessageArrived(MessageType) method.
|
2022-03-22 21:09:05 +01:00
|
|
|
template<typename MT>
|
2018-02-27 18:06:05 +01:00
|
|
|
class AbstractMessageHandler : public _MessageHandlerBase {
|
2020-11-19 18:22:57 +01:00
|
|
|
public:
|
2018-05-01 00:41:33 +02:00
|
|
|
AbstractMessageHandler(QIODevice *device, QObject *parent);
|
2021-03-21 04:37:30 +01:00
|
|
|
~AbstractMessageHandler() override { AbstractMessageHandler::AbortAll(); }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2022-10-13 22:39:31 +02:00
|
|
|
using MessageType = MT;
|
|
|
|
using ReplyType = MessageReply<MT>;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-12 19:55:19 +02:00
|
|
|
// Serialises the message and writes it to the socket.
|
|
|
|
// This version MUST be called from the thread in which the AbstractMessageHandler was created.
|
2018-05-01 00:41:33 +02:00
|
|
|
void SendMessage(const MessageType &message);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-12 19:55:19 +02:00
|
|
|
// Serialises the message and writes it to the socket.
|
|
|
|
// This version may be called from any thread.
|
2018-05-01 00:41:33 +02:00
|
|
|
void SendMessageAsync(const MessageType &message);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Sends the request message inside and takes ownership of the MessageReply.
|
2019-04-12 19:55:19 +02:00
|
|
|
// The MessageReply's Finished() signal will be emitted when a reply arrives with the same ID. Must be called from my thread.
|
2018-05-01 00:41:33 +02:00
|
|
|
void SendRequest(ReplyType *reply);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-04-12 19:55:19 +02: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.
|
2018-05-01 00:41:33 +02:00
|
|
|
void SendReply(const MessageType &request, MessageType *reply);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-11-19 18:22:57 +01:00
|
|
|
protected:
|
2018-02-27 18:06:05 +01:00
|
|
|
// Called when a message is received from the socket.
|
2019-09-15 20:27:32 +02:00
|
|
|
virtual void MessageArrived(const MessageType &message) { Q_UNUSED(message); }
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// _MessageHandlerBase
|
2020-06-26 23:01:57 +02:00
|
|
|
bool RawMessageArrived(const QByteArray &data) override;
|
|
|
|
void AbortAll() override;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-11-19 18:22:57 +01:00
|
|
|
private:
|
2021-02-20 16:16:17 +01:00
|
|
|
QMap<int, ReplyType*> pending_replies_;
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
2022-03-22 21:09:05 +01:00
|
|
|
template<typename MT>
|
2018-05-01 00:41:33 +02:00
|
|
|
AbstractMessageHandler<MT>::AbstractMessageHandler(QIODevice *device, QObject *parent)
|
2018-02-27 18:06:05 +01:00
|
|
|
: _MessageHandlerBase(device, parent) {}
|
|
|
|
|
2022-03-22 21:09:05 +01:00
|
|
|
template<typename MT>
|
2018-05-01 00:41:33 +02:00
|
|
|
void AbstractMessageHandler<MT>::SendMessage(const MessageType &message) {
|
2018-02-27 18:06:05 +01:00
|
|
|
Q_ASSERT(QThread::currentThread() == thread());
|
|
|
|
|
|
|
|
std::string data = message.SerializeAsString();
|
|
|
|
WriteMessage(QByteArray(data.data(), data.size()));
|
|
|
|
}
|
|
|
|
|
2022-03-22 21:09:05 +01:00
|
|
|
template<typename MT>
|
2018-05-01 00:41:33 +02:00
|
|
|
void AbstractMessageHandler<MT>::SendMessageAsync(const MessageType &message) {
|
2018-02-27 18:06:05 +01:00
|
|
|
std::string data = message.SerializeAsString();
|
2021-09-09 21:53:14 +02:00
|
|
|
QMetaObject::invokeMethod(this, "WriteMessage", Qt::QueuedConnection, Q_ARG(QByteArray, QByteArray(data.data(), data.size())));
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MT>
|
2018-05-01 00:41:33 +02:00
|
|
|
void AbstractMessageHandler<MT>::SendRequest(ReplyType *reply) {
|
2018-02-27 18:06:05 +01:00
|
|
|
pending_replies_[reply->id()] = reply;
|
|
|
|
SendMessage(reply->request_message());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MT>
|
2018-05-01 00:41:33 +02:00
|
|
|
void AbstractMessageHandler<MT>::SendReply(const MessageType &request, MessageType *reply) {
|
2018-02-27 18:06:05 +01:00
|
|
|
reply->set_id(request.id());
|
|
|
|
SendMessage(*reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MT>
|
2018-05-01 00:41:33 +02:00
|
|
|
bool AbstractMessageHandler<MT>::RawMessageArrived(const QByteArray &data) {
|
2020-11-19 18:22:57 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
MessageType message;
|
|
|
|
if (!message.ParseFromArray(data.constData(), data.size())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-01 19:20:57 +02:00
|
|
|
if (pending_replies_.contains(message.id())) {
|
2018-02-27 18:06:05 +01:00
|
|
|
// This is a reply to a message that we created earlier.
|
2019-07-01 19:20:57 +02:00
|
|
|
ReplyType *reply = pending_replies_.take(message.id());
|
2018-02-27 18:06:05 +01:00
|
|
|
reply->SetReply(message);
|
2019-04-12 19:55:19 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-02-27 18:06:05 +01:00
|
|
|
MessageArrived(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-11-19 18:22:57 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MT>
|
|
|
|
void AbstractMessageHandler<MT>::AbortAll() {
|
2020-11-19 18:22:57 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
for (ReplyType *reply : pending_replies_) {
|
2018-02-27 18:06:05 +01:00
|
|
|
reply->Abort();
|
|
|
|
}
|
|
|
|
pending_replies_.clear();
|
2020-11-19 18:22:57 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // MESSAGEHANDLER_H
|