QtProtobuf  0.6
Protobuf plugin to generate Qt classes
qgrpcchannel_p.h
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2019 Giulio Girardi <giulio.girardi@protechgroup.it>
5  *
6  * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of this
9  * software and associated documentation files (the "Software"), to deal in the Software
10  * without restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
12  * to permit persons to whom the Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in all copies
16  * or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
19  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
20  * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
21  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 #include <QEventLoop>
27 #include <QThread>
28 
29 #include <grpcpp/channel.h>
30 #include <grpcpp/impl/codegen/byte_buffer.h>
31 #include <grpcpp/impl/codegen/client_context.h>
32 #include <grpcpp/impl/codegen/sync_stream.h>
33 #include <grpcpp/security/credentials.h>
34 
35 #include "qabstractgrpccredentials.h"
36 #include "qgrpcasyncreply.h"
37 #include "qgrpcsubscription.h"
38 #include "qabstractgrpcclient.h"
39 #include "qgrpccredentials.h"
40 #include "qprotobufserializerregistry_p.h"
41 #include "qtprotobuflogging.h"
42 
43 namespace QtProtobuf {
44 
46 class QGrpcChannelSubscription : public QObject {
48  Q_OBJECT;
49 
50 public:
51  QGrpcChannelSubscription(grpc::Channel *channel, const QString &method, const QByteArray &data, QObject *parent = nullptr);
52  ~QGrpcChannelSubscription();
53 
54  void cancel();
55  void start();
56 
57 signals:
58  void dataReady(const QByteArray &data);
59  void finished();
60 
61 public:
62  QGrpcStatus status;
63 
64 private:
65  QThread *thread;
66  grpc::ClientContext context;
67  grpc::ClientReader<grpc::ByteBuffer> *reader = nullptr;
68 };
69 
71 class QGrpcChannelCall : public QObject {
73  Q_OBJECT;
74 
75 public:
76  QGrpcChannelCall(grpc::Channel *channel, const QString &method, const QByteArray &data, QObject *parent = nullptr);
77  ~QGrpcChannelCall();
78 
79  void cancel();
80  void start();
81 
82 signals:
83  void finished();
84 
85 public:
86  QGrpcStatus status;
87  QByteArray response;
88 
89 private:
90  QThread *thread;
91  grpc::ClientContext context;
92 };
93 
95 struct QGrpcChannelPrivate {
97  std::shared_ptr<grpc::Channel> m_channel;
98 
99  QGrpcChannelPrivate(const QUrl &url, std::shared_ptr<grpc::ChannelCredentials> credentials);
100  ~QGrpcChannelPrivate();
101 
102  void call(const QString &method, const QString &service, const QByteArray &args, QGrpcAsyncReply *reply);
103  QGrpcStatus call(const QString &method, const QString &service, const QByteArray &args, QByteArray &ret);
104  void subscribe(QGrpcSubscription *subscription, const QString &service, QAbstractGrpcClient *client);
105 };
106 
107 };