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 <QtGrpc/qgrpcoperationcontext.h>
6#include <QtGrpc/qgrpcstream.h>
7
8QT_BEGIN_NAMESPACE
9
10/*!
11 \class QGrpcServerStream
12 \inmodule QtGrpc
13 \since 6.7
14 \brief The QGrpcServerStream class provides access in handling server-streaming RPCs.
15
16 The QGrpcServerStream class provides the interface for handling
17 server-streaming remote procedure calls (RPCs), which is one of the four
18 \gRPC \l{Service Methods}{service methods}.
19
20 For a high-level overview, refer to the \l {Server Streaming} {Qt GRPC
21 Client Guide}.
22
23 \include qtgrpc-shared.qdocinc rpc-lifetime-note
24*/
25
26/*!
27 \fn void QGrpcServerStream::messageReceived()
28
29//! [message-received-desc]
30 This signal is emitted when the streaming RPC has received a new message
31 from the server. The read() methods can then be used to deserialize the
32 received message.
33//! [message-received-desc]
34*/
35
36/*!
37 \internal
38
39 Constructs a QGrpcServerStream using \a operationContext to communicate
40 with the underlying channel and sets \a parent as the owner.
41*/
42QGrpcServerStream::QGrpcServerStream(std::shared_ptr<QGrpcOperationContext> operationContext,
43 QObject *parent)
44 : QGrpcOperation(std::move(operationContext), parent)
45{
46 QObject::connect(sender: &QGrpcOperation::context(), signal: &QGrpcOperationContext::messageReceived, context: this,
47 slot: &QGrpcServerStream::messageReceived);
48}
49
50/*!
51 Destroys the QGrpcServerStream.
52*/
53QGrpcServerStream::~QGrpcServerStream() = default;
54
55bool QGrpcServerStream::event(QEvent *event)
56{
57 return QGrpcOperation::event(event);
58}
59
60/*!
61 \class QGrpcClientStream
62 \inmodule QtGrpc
63 \since 6.7
64 \brief The QGrpcClientStream class provides access in handling client-streaming RPCs.
65
66 The QGrpcClientStream class provides the interface for handling
67 client-streaming remote procedure calls (RPCs), which is one of the four
68 \gRPC \l{Service Methods}{service methods}.
69
70 For a high-level overview, refer to the \l {Client Streaming} {Qt GRPC
71 Client Guide}.
72
73 \include qtgrpc-shared.qdocinc rpc-lifetime-note
74*/
75
76/*!
77 \internal
78
79 Constructs a QGrpcServerStream using \a operationContext to communicate
80 with the underlying channel and sets \a parent as the owner.
81*/
82QGrpcClientStream::QGrpcClientStream(std::shared_ptr<QGrpcOperationContext> operationContext,
83 QObject *parent)
84 : QGrpcOperation(std::move(operationContext), parent)
85{
86}
87
88/*!
89 Destroys the QGrpcClientStream.
90*/
91QGrpcClientStream::~QGrpcClientStream() = default;
92
93/*!
94//! [write-message-desc]
95 Serializes \a message and sends it to the server.
96//! [write-message-desc]
97*/
98void QGrpcClientStream::writeMessage(const QProtobufMessage &message)
99{
100 QByteArray data = QGrpcOperation::context().serializer()->serialize(message: &message);
101 emit QGrpcOperation::context().writeMessageRequested(data);
102}
103
104/*!
105 \since 6.8
106//! [writes-done-desc]
107 Ends the stream from the client side (half-closing). The server is still allowed to send
108 responses after this call.
109//! [writes-done-desc]
110*/
111void QGrpcClientStream::writesDone()
112{
113 emit QGrpcOperation::context().writesDoneRequested();
114}
115
116bool QGrpcClientStream::event(QEvent *event)
117{
118 return QGrpcOperation::event(event);
119}
120
121/*!
122 \class QGrpcBidiStream
123 \inmodule QtGrpc
124 \since 6.7
125 \brief The QGrpcBidiStream class provides access in handling
126 bidirectional-streaming RPCs.
127
128 The QGrpcBidiStream class provides the interface for handling
129 bidirectional-streaming remote procedure calls (RPCs), which is one of the
130 four \gRPC \l{Service Methods}{service methods}.
131
132 For a high-level overview, refer to the \l {Bidirectional Streaming} {Qt
133 GRPC Client Guide}.
134
135 \include qtgrpc-shared.qdocinc rpc-lifetime-note
136*/
137
138/*!
139 \fn void QGrpcBidiStream::messageReceived()
140
141 \include qgrpcstream.cpp message-received-desc
142*/
143
144/*!
145 \internal
146
147 Constructs a QGrpcBidiStream using \a operationContext to communicate
148 with the underlying channel and sets \a parent as the owner.
149*/
150QGrpcBidiStream::QGrpcBidiStream(std::shared_ptr<QGrpcOperationContext> operationContext,
151 QObject *parent)
152 : QGrpcOperation(std::move(operationContext), parent)
153{
154 QObject::connect(sender: &QGrpcOperation::context(), signal: &QGrpcOperationContext::messageReceived, context: this,
155 slot: &QGrpcBidiStream::messageReceived);
156}
157
158/*!
159 Destroys the QGrpcBidiStream.
160*/
161QGrpcBidiStream::~QGrpcBidiStream() = default;
162
163/*!
164 \include qgrpcstream.cpp write-message-desc
165*/
166void QGrpcBidiStream::writeMessage(const QProtobufMessage &message)
167{
168 QByteArray data = QGrpcOperation::context().serializer()->serialize(message: &message);
169 emit QGrpcOperation::context().writeMessageRequested(data);
170}
171
172/*!
173 \since 6.8
174 \include qgrpcstream.cpp writes-done-desc
175*/
176void QGrpcBidiStream::writesDone()
177{
178 emit QGrpcOperation::context().writesDoneRequested();
179}
180
181bool QGrpcBidiStream::event(QEvent *event)
182{
183 return QGrpcOperation::event(event);
184}
185
186QT_END_NAMESPACE
187
188#include "moc_qgrpcstream.cpp"
189

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