1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
4
5#include <qtgrpcglobal_p.h>
6
7#include <QtCore/qpointer.h>
8#include <QtCore/private/qobject_p.h>
9
10#include "qgrpcoperation.h"
11
12QT_BEGIN_NAMESPACE
13
14/*!
15 \class QGrpcOperation
16 \inmodule QtGrpc
17 \brief The QGrpcOperation class implements common logic to
18 handle communication in Grpc channel.
19*/
20
21/*!
22 \fn template <typename T> T QGrpcOperation::read() const;
23
24 Reads message from raw byte array stored in QGrpcCallReply.
25
26 Returns a copy of the deserialized message or, on failure,
27 a default-constructed message.
28*/
29
30/*!
31 \fn void QGrpcOperation::finished()
32
33 This signal indicates the end of communication for this call.
34
35 If signal emitted by stream this means that stream is successfully
36 closed either by client or server.
37*/
38
39/*!
40 \fn void QGrpcOperation::errorOccurred(const QGrpcStatus &status)
41
42 This signal indicates the error occurred during serialization.
43
44 This signal is emitted when error with \a status occurs in channel
45 or during serialization.
46*/
47
48class QGrpcOperationPrivate : public QObjectPrivate
49{
50 Q_DECLARE_PUBLIC(QGrpcOperation)
51public:
52 QGrpcOperationPrivate(std::shared_ptr<QAbstractProtobufSerializer> _serializer)
53 : serializer(std::move(_serializer))
54 {
55 }
56
57 QByteArray data;
58 QGrpcMetadata metadata;
59 std::shared_ptr<QAbstractProtobufSerializer> serializer;
60};
61
62QGrpcOperation::QGrpcOperation(std::shared_ptr<QAbstractProtobufSerializer> serializer)
63 : QObject(*new QGrpcOperationPrivate(std::move(serializer)))
64{
65}
66
67QGrpcOperation::~QGrpcOperation() = default;
68
69/*!
70 Interface for implementation of QAbstractGrpcChannel.
71
72 Should be used to write raw data from channel to reply \a data raw data
73 received from channel.
74 */
75void QGrpcOperation::setData(const QByteArray &data)
76{
77 Q_D(QGrpcOperation);
78 d->data = data;
79}
80
81/*!
82 Interface for implementation of QAbstractGrpcChannel.
83
84 Should be used to write raw data from channel to reply \a data raw data
85 received from channel.
86*/
87void QGrpcOperation::setData(QByteArray &&data)
88{
89 Q_D(QGrpcOperation);
90 d->data = std::move(data);
91}
92
93/*!
94 \internal
95 Getter of the data received from the channel.
96*/
97QByteArray QGrpcOperation::data() const
98{
99 return d_func()->data;
100}
101
102/*!
103 Interface for implementation of QAbstractGrpcChannel.
104
105 Should be used to write metadata from channel to reply \a metadata received
106 from channel. For the HTTP2 channels it usually contains the HTTP
107 headers received from the server.
108*/
109void QGrpcOperation::setMetadata(const QGrpcMetadata &metadata)
110{
111 Q_D(QGrpcOperation);
112 d->metadata = metadata;
113}
114
115/*!
116 Interface for implementation of QAbstractGrpcChannel.
117
118 Should be used to write metadata from channel to reply \a metadata received
119 from channel. For the HTTP2 channels it usually contains the HTTP
120 headers received from the server.
121*/
122void QGrpcOperation::setMetadata(QGrpcMetadata &&metadata)
123{
124 Q_D(QGrpcOperation);
125 d->metadata = std::move(metadata);
126}
127
128/*!
129 Getter of the metadata received from the channel. For the HTTP2 channels it
130 usually contains the HTTP headers received from the server.
131*/
132QGrpcMetadata QGrpcOperation::metadata() const
133{
134 return d_func()->metadata;
135}
136
137/*!
138 \internal
139 Getter of the serializer that QGrpcOperation was constructed with.
140*/
141std::shared_ptr<QAbstractProtobufSerializer> QGrpcOperation::serializer() const
142{
143 return d_func()->serializer;
144}
145
146QT_END_NAMESPACE
147
148#include "moc_qgrpcoperation.cpp"
149

source code of qtgrpc/src/grpc/qgrpcoperation.cpp