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 <QtCore/QThread>
6
7#include "qgrpcstream.h"
8
9QT_BEGIN_NAMESPACE
10
11/*!
12 \class QGrpcStream
13 \inmodule QtGrpc
14
15 \brief The QGrpcStream class implements logic to handle stream communication
16 in the Grpc channel.
17*/
18
19/*!
20 \fn void QGrpcStream::messageReceived()
21
22 The signal is emitted when the stream receives an updated value from server.
23*/
24
25QGrpcStream::QGrpcStream(QLatin1StringView method, QByteArrayView arg,
26 std::shared_ptr<QAbstractProtobufSerializer> serializer)
27 : QGrpcOperation(std::move(serializer)), m_method(method.data(), method.size()),
28 m_arg(arg.toByteArray())
29{
30}
31
32/*!
33 Destroys the QGrpcStream object.
34*/
35QGrpcStream::~QGrpcStream() = default;
36
37/*!
38 Cancel this stream and try to abort any call active on any channel
39 in the stream.
40*/
41void QGrpcStream::abort()
42{
43 if (thread() != QThread::currentThread())
44 QMetaObject::invokeMethod(object: this, function: &QGrpcStream::finished, type: Qt::BlockingQueuedConnection);
45 else
46 emit finished();
47}
48
49/*!
50 Returns the method for this stream.
51*/
52QLatin1StringView QGrpcStream::method() const
53{
54 return QLatin1StringView(m_method);
55}
56
57/*!
58 Returns serialized arguments for this stream.
59*/
60QByteArrayView QGrpcStream::arg() const
61{
62 return m_arg;
63}
64
65/*!
66 Sets underlying data field with \a data and emits QGrpcStream::messageReceived signal.
67
68 Should be used by QAbstractGrpcChannel implementations,
69 to update data in a stream and notify clients about stream updates.
70*/
71void QGrpcStream::updateData(const QByteArray &data)
72{
73 setData(QByteArray(data));
74 emit messageReceived();
75}
76
77QT_END_NAMESPACE
78
79#include "moc_qgrpcstream.cpp"
80

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