summaryrefslogtreecommitdiffstats
path: root/src/grpc/qgrpcoperationcontext.cpp
blob: a3a203e1bf317d8b1f7e3c96feb6c33cd053977f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtGrpc/private/qgrpccommonoptions_p.h>
#include <QtGrpc/private/qgrpcoperation_p.h>
#include <QtGrpc/private/qgrpcoperationcontext_p.h>
#include <QtGrpc/qgrpcoperation.h>
#include <QtGrpc/qgrpcoperationcontext.h>
#include <QtGrpc/qgrpcstatus.h>

#include <QtProtobuf/qprotobufserializer.h>

#include <QtCore/private/qobject_p.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qbytearrayview.h>
#include <QtCore/qlatin1stringview.h>

#include <utility>

QT_BEGIN_NAMESPACE

/*!
    \class QGrpcOperationContext
    \inmodule QtGrpc
    \since 6.7
    \brief The QGrpcOperationContext class provides context for communication
    between an individual \gRPC operation and a channel.

    QGrpcOperationContext is constructed internally when a remote procedure call
    (RPC) is requested, mediating interaction between the client's operation
    request and the channel's operation outcome.

    RPCs requested on the client interface return specializations of
    QGrpcOperation, such as QGrpcCallReply, QGrpcServerStream,
    QGrpcClientStream, or QGrpcBidiStream. These classes are implicitly
    integrated with the underlying QGrpcOperationContext counterpart.

    \note Don't use this class directly unless implementing a custom channel.

    The signals contained within this class are used to build the communication
    between the client-facing QGrpcOperation and the QAbstractGrpcChannel
    implementation. These operations are asynchronous in nature, and multiple
    RPCs can operate on the same channel concurrently. Channels typically treat
    all operations the same, and it is the channel's responsibility to
    correctly support and restrict the subset of features its RPC type
    supports.

    Signals which should only be emitted by the channel's implementation are:
    \list
        \li finished()
        \li messageReceived()
    \endlist

    Signals which will be emitted by QGrpcOperation and its specializations are:
    \list
        \li cancelRequested()
        \li writeMessageRequested()
        \li writesDoneRequested()
    \endlist

    Custom implementations of QAbstractGrpcChannel must handle the client's
    signals, as no default signal handling is provided, and emit their own
    signals accordingly.
*/

/*!
    \fn void QGrpcOperationContext::finished(const QGrpcStatus &status)

    This signal should be emitted by the channel when an RPC finishes.

    It usually means that the server sent a \a status for the requested RPC and
    closed the connection. Implementations of QAbstractGrpcChannel should
    detect this situation and emit the signal.

    After receiving this signal, no further communication should occur through
    this operation context. The client side may then safely delete the
    corresponding RPC object.

    \note This signal is implicitly connected to the QGrpcOperation counterpart.

    \sa QGrpcOperation::finished
*/

/*!
    \fn void QGrpcOperationContext::messageReceived(const QByteArray &data)

    This signal should be emitted by the channel when a new chunk of \a data is
    received from the server.

    For client streams and unary calls, this means that the single and final
    response has arrived and communication is about to finish.

    For server and bidirectional streams, this signal should be continuously
    emitted by the channel upon receiving each new messages.

    \note This signal is implicitly connected to the QGrpcOperation
    counterpart.

    \sa QGrpcServerStream::messageReceived
    \sa QGrpcBidiStream::messageReceived
*/

/*!
    \fn void QGrpcOperationContext::cancelRequested()

    This signal is emitted by QGrpcOperation when requesting cancellation of the
    communication.

    The channel is expected to connect its cancellation logic to this signal
    and attempt to cancel the RPC and finish it with a
    \l{QtGrpc::StatusCode::}{Cancelled} status code. Successful cancellation
    cannot be guaranteed. Further processing of the data received from a
    channel is not required and should be avoided.

    \sa QGrpcOperation::cancel
*/

/*!
    \fn void QGrpcOperationContext::writeMessageRequested(const QByteArray &data)

    This signal is emitted by QGrpcClientStream or QGrpcBidiStream when it has
    serialized \a data ready for a channel to deliver.

    The channel is expected to connect its writing logic to this signal and wrap
    the serialized data in channel-related headers before writing it to the
    wire.

    \sa QGrpcClientStream::writeMessage
    \sa QGrpcBidiStream::writeMessage
*/

/*!
    \fn void QGrpcOperationContext::writesDoneRequested()

    This signal is emitted by QGrpcClientStream or QGrpcBidiStream to indicate
    that it's done writing messages. The channel should respond to this by
    half-closing the stream.

    \note After receiving this signal no more write operations are permitted
    for the streaming RPCs. The server can still send messages, which should be
    forwarded with messageReceived().

    \sa QGrpcClientStream::writesDone
    \sa QGrpcBidiStream::writesDone
*/

/*!
    \fn void QGrpcOperationContext::serverInitialMetadataReceived()

    \include qgrpcoperation.cpp serverInitialMetadataReceived

    \sa QGrpcOperation::serverInitialMetadataReceived
    \sa QGrpcBidiStream::writesDone
*/

/*!
    \internal

    Constructs an operation context with the given \a descriptor, \a options,
    and \a serializer. The \a parent is the QGrpcOperation that owns this
    context.

    \note This class can only be constructed by its parent QGrpcOperation.
*/
QGrpcOperationContext::QGrpcOperationContext(QtGrpc::RpcDescriptor descriptor,
                                             const QGrpcCallOptions &options,
                                             std::shared_ptr<QAbstractProtobufSerializer>
                                                 serializer,
                                             QGrpcOperation *parent, PrivateConstructor /*unused*/)
    : QObject(*new QGrpcOperationContextPrivate(std::move(descriptor), options,
                                                std::move(serializer)),
              parent)
{
    using namespace QtGrpc;
    Q_D(const QGrpcOperationContext);
    const auto type = d->descriptor.type;
    if (type == RpcType::UnaryCall || type == RpcType::ServerStreaming) {
        connect(this, &QGrpcOperationContext::writeMessageRequested, this, []() {
            Q_ASSERT_X(false, "QGrpcOperationContext::writeMessageRequested",
                       "This signal is disallowed for unary and server-streaming operations");
        });
        connect(this, &QGrpcOperationContext::writesDoneRequested, this, []() {
            Q_ASSERT_X(false, "QGrpcOperationContext::writesDoneRequested",
                       "This signal is disallowed for unary and server-streaming operations");
        });
    }
}

/*!
    Destroys the operation-context.
*/
QGrpcOperationContext::~QGrpcOperationContext() = default;

/*!
    Returns the method name of the service associated with this
    operation-context.
*/
QLatin1StringView QGrpcOperationContext::method() const noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->descriptor.method;
}

/*!
    Returns the service name associated with this operation-context.
*/
QLatin1StringView QGrpcOperationContext::service() const noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->descriptor.service;
}

QtGrpc::RpcDescriptor QGrpcOperationContext::descriptor() const noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->descriptor;
}

#if QT_DEPRECATED_SINCE(6, 11)
/*!
    \deprecated [6.11] Use the new QAbstractGrpcChannel virtual RPC methods instead.
    Returns an empty QByteArrayView.
*/
QByteArrayView QGrpcOperationContext::argument() const noexcept
{
    return {};
}
#endif // QT_DEPRECATED_SINCE(6, 11)

/*!
    Returns the call options that is utilized by this operation-context.

    For channel-wide options, see QGrpcChannelOptions.
*/
const QGrpcCallOptions &QGrpcOperationContext::callOptions() const & noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->options;
}

/*!
    Returns the serializer of this operation-context
*/
std::shared_ptr<const QAbstractProtobufSerializer>
QGrpcOperationContext::serializer() const
{
    Q_D(const QGrpcOperationContext);
    return d->serializer;
}

#if QT_DEPRECATED_SINCE(6, 13)

/*!
    \deprecated [6.13] Use serverInitialMetadata() and serverTrailingMetadata() instead.

    \include qgrpcoperation.cpp serverInitialMetadata
    \note This method is used implicitly by QGrpcOperation.

    \sa serverInitialMetadata() QGrpcOperation::serverInitialMetadata()
*/
const QHash<QByteArray, QByteArray> &QGrpcOperationContext::serverMetadata() const & noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->deprServerInitialMetadata;
}

/*!
    \fn void QGrpcOperationContext::setServerMetadata(const QHash<QByteArray, QByteArray> &metadata)
    \fn void QGrpcOperationContext::setServerMetadata(QHash<QByteArray, QByteArray> &&metadata)
    \deprecated [6.13] Use setServerInitialMetadata() instead.

    Sets the metadata received from the server at the start of the RPC.

    \sa setServerInitialMetadata()
*/
void QGrpcOperationContext::setServerMetadata(const QHash<QByteArray, QByteArray> &metadata)
{
    Q_D(QGrpcOperationContext);
    if (d->deprServerInitialMetadata == metadata)
        return;
    d->deprServerInitialMetadata = metadata;
    d->serverInitialMetadata = QMultiHash<QByteArray, QByteArray>(metadata);
}
void QGrpcOperationContext::setServerMetadata(QHash<QByteArray, QByteArray> &&metadata)
{
    Q_D(QGrpcOperationContext);
    if (d->deprServerInitialMetadata == metadata)
        return;
    d->deprServerInitialMetadata = std::move(metadata);
    d->serverInitialMetadata = QMultiHash<QByteArray, QByteArray>(d->deprServerInitialMetadata);
}

#endif // QT_DEPRECATED_SINCE(6, 13)

/*!
    \since 6.10

    \include qgrpcoperation.cpp serverInitialMetadata
    \note This method is used implicitly by QGrpcOperation.

    \sa QGrpcOperation::serverInitialMetadata() serverTrailingMetadata()
*/
const QMultiHash<QByteArray, QByteArray> &
QGrpcOperationContext::serverInitialMetadata() const & noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->serverInitialMetadata;
}

/*!
    \since 6.10

    Sets the \a metadata received from the server at the start of the RPC.

    \sa serverInitialMetadata()
*/
void QGrpcOperationContext::setServerInitialMetadata(QMultiHash<QByteArray, QByteArray> &&metadata)
{
    Q_D(QGrpcOperationContext);
    if (auto channel = operation().d_func()->channel.lock()) {
        auto &engine = QAbstractGrpcChannelPrivate::get(channel.get())->interceptorEngine;
        if (engine.hasHandlerFor(QtGrpcPrivate::InterceptorCapability::InitialMetadata))
            engine.onInitialMetadata(*this, metadata);
    }
    d->serverInitialMetadata = std::move(metadata);
#if QT_DEPRECATED_SINCE(6, 13)
    d->deprServerInitialMetadata = QtGrpcPrivate::toHash(d->serverInitialMetadata);
#endif
    emit serverInitialMetadataReceived();
}

/*!
    \since 6.10

    \include qgrpcoperation.cpp serverTrailingMetadata
    \note This method is used implicitly by QGrpcOperation.

    \sa QGrpcOperation::serverTrailingMetadata() setServerTrailingMetadata()
*/
const QMultiHash<QByteArray, QByteArray> &
QGrpcOperationContext::serverTrailingMetadata() const & noexcept
{
    Q_D(const QGrpcOperationContext);
    return d->serverTrailingMetadata;
}

/*!
    \since 6.10

    Sets the trailing \a metadata received from the server after all response
    messages.

    \sa serverTrailingMetadata()
*/
void QGrpcOperationContext::setServerTrailingMetadata(QMultiHash<QByteArray, QByteArray> &&metadata)
{
    Q_D(QGrpcOperationContext);
    if (auto channel = operation().d_func()->channel.lock()) {
        auto &engine = QAbstractGrpcChannelPrivate::get(channel.get())->interceptorEngine;
        if (engine.hasHandlerFor(QtGrpcPrivate::InterceptorCapability::TrailingMetadata))
            engine.onTrailingMetadata(*this, metadata);
    }
    if (d->serverTrailingMetadata == metadata)
        return;
    d->serverTrailingMetadata = std::move(metadata);
}

/*!
    Returns the meta type of the RPC result message.
 */
QMetaType QGrpcOperationContext::responseMetaType() const
{
    Q_D(const QGrpcOperationContext);
    return d->responseMetaType;
}

/*!
    Stores the \a metaType of the RPC result message.
*/
void QGrpcOperationContext::setResponseMetaType(QMetaType metaType)
{
    Q_D(QGrpcOperationContext);
    d->responseMetaType = metaType;
}

const QGrpcOperation &QGrpcOperationContext::operation() const
{
    // Guaranteed by construction & parent<>child relationship
    const auto *p = parent();
    Q_ASSERT(p);
    return static_cast<const QGrpcOperation &>(*p);
}

// For future extensions
bool QGrpcOperationContext::event(QEvent *event)
{
    return QObject::event(event);
}

QT_END_NAMESPACE

#include "moc_qgrpcoperationcontext.cpp"