30 #include <type_traits>
36 #include <qtprotobuflogging.h>
37 #include <qabstractprotobufserializer.h>
39 #include "qabstractgrpcchannel.h"
41 #include "qtgrpcglobal.h"
47 namespace QtProtobuf {
49 class QGrpcAsyncReply;
50 class QGrpcSubscription;
51 class QGrpcAsyncOperationBase;
52 class QAbstractGrpcChannel;
53 class QAbstractGrpcClientPrivate;
58 using SubscriptionHandler = std::function<void(
const QByteArray&)>;
78 void attachChannel(
const std::shared_ptr<QAbstractGrpcChannel> &channel);
99 template<
typename A,
typename R>
100 QGrpcStatus call(
const QString &method,
const A &arg,
const QPointer<R> &ret) {
103 static const QString errorString(
"Unable to call method: %1. Pointer to return data is null");
106 qProtoCritical() << errorString.arg(method);
111 status = call(method, arg.serialize(serializer()), retData);
112 if (status == QGrpcStatus::StatusCode::Ok) {
113 return tryDeserialize(*ret, retData);
125 QGrpcAsyncReplyShared call(
const QString &method,
const A &arg) {
126 return call(method, arg.serialize(serializer()));
138 QGrpcSubscriptionShared subscribe(
const QString &method,
const A &arg) {
139 return subscribe(method, arg.serialize(serializer()));
152 template<
typename A,
typename R>
153 QGrpcSubscriptionShared subscribe(
const QString &method,
const A &arg,
const QPointer<R> &ret) {
155 static const QString nullPointerError(
"Unable to subscribe method: %1. Pointer to return data is null");
157 qProtoCritical() << nullPointerError.arg(method);
161 return subscribe(method, arg.serialize(serializer()), [ret,
this](
const QByteArray &data) {
163 tryDeserialize(*ret, data);
165 static const QLatin1String nullPointerError(
"Pointer to return data is null while subscription update received");
166 error({QGrpcStatus::InvalidArgument, nullPointerError});
167 qProtoCritical() << nullPointerError;
184 friend class QGrpcAsyncOperationBase;
187 QGrpcStatus call(
const QString &method,
const QByteArray &arg, QByteArray &ret);
190 QGrpcAsyncReplyShared call(
const QString &method,
const QByteArray &arg);
193 QGrpcSubscriptionShared subscribe(
const QString &method,
const QByteArray &arg,
const QtProtobuf::SubscriptionHandler &handler = {});
200 QGrpcStatus tryDeserialize(R &ret,
const QByteArray &retData) {
203 ret.deserialize(serializer(), retData);
204 }
catch (std::invalid_argument &) {
205 static const QLatin1String invalidArgumentErrorMessage(
"Response deserialization failed invalid field found");
208 qProtoCritical() << invalidArgumentErrorMessage;
209 }
catch (std::out_of_range &) {
210 static const QLatin1String outOfRangeErrorMessage(
"Invalid size of received buffer");
213 qProtoCritical() << outOfRangeErrorMessage;
221 Q_DISABLE_COPY_MOVE(QAbstractGrpcClient)
223 std::unique_ptr<QAbstractGrpcClientPrivate> dPtr;
The QAbstractGrpcClient class is bridge between gRPC clients and channels.
Definition: qabstractgrpcclient.h:67
void cancel(const QString &method)
Canceles all subscriptions for specified method.
void error(const QGrpcStatus &status)
error signal is emited by client when error occured in channel or while serialization/deserialization
The QAbstractProtobufSerializer class is interface that represents basic functions for serialization/...
Definition: qabstractprotobufserializer.h:72
The QGrpcStatus class contains information about last gRPC operation.
Definition: qgrpcstatus.h:58
@ Ok
No error.
Definition: qgrpcstatus.h:70
@ OutOfRange
The operation was attempted past the valid range.
Definition: qgrpcstatus.h:82
@ Internal
Internal errors.
Definition: qgrpcstatus.h:84
@ InvalidArgument
The client specified an invalid argument.
Definition: qgrpcstatus.h:73