qabstractgrpcchannel.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  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. #pragma once //QAbstractGrpcChannel
  26. #include <QString>
  27. #include <QByteArray>
  28. #include <functional>
  29. #include <memory>
  30. #include "qgrpcstatus.h"
  31. #include "qgrpccredentials.h"
  32. #include "qtgrpcglobal.h"
  33. class QThread;
  34. namespace QtProtobuf {
  35. class QGrpcAsyncReply;
  36. class QGrpcStream;
  37. class QAbstractGrpcClient;
  38. class QAbstractProtobufSerializer;
  39. struct QAbstractGrpcChannelPrivate;
  40. /*!
  41. * \ingroup QtGrpc
  42. * \brief The QAbstractGrpcChannel class is interface that represents common gRPC channel functionality.
  43. * You may implement this interface to create own channels for gRPC transport.
  44. */
  45. class Q_GRPC_EXPORT QAbstractGrpcChannel
  46. {
  47. public:
  48. /*!
  49. * \brief Calls \p method synchronously with given serialized message \p args and write result of call to \p ret.
  50. * \note This method is synchronous, that means it doesn't return until call is completed or aborted by timeout if it's
  51. * implemented in inherited channel.
  52. * \note This method should not be called directly.
  53. * \param[in] method remote method is called
  54. * \param[in] service service identified in URL path format
  55. * \param[in] args serialized argument message
  56. * \param[out] ret output bytearray to collect returned message
  57. * \return returns gRPC QAbstractGrpcChannel::Status code for operation
  58. */
  59. virtual QGrpcStatus call(const QString &method, const QString &service, const QByteArray &args, QByteArray &ret) = 0;
  60. /*!
  61. * \brief Calls \p method asynchronously with given serialized messge \p args. Result of method call is written to QGrpcAsyncReply.
  62. * \note This method is asynchronous, that means it returns control imediately after it is called.
  63. * \note This method should not be called directly.
  64. * \param[in] method remote method is called
  65. * \param[in] service service identified in URL path format
  66. * \param[in] args serialized argument message
  67. * \param[out] ret QGrpcAsyncReply that will be returned to end-point user to read data once call complete.
  68. * QGrpcAsyncReply lifecycle is managed by QAbstractGrpcClient only.
  69. * \see QGrpcAsyncReply for details
  70. */
  71. virtual void call(const QString &method, const QString &service, const QByteArray &args, QtProtobuf::QGrpcAsyncReply *ret) = 0;
  72. /*!
  73. * \brief Streams to server-side stream to receive updates for given \p method.
  74. * \note This method should not be called directly.
  75. * \param[in] method remote method is called
  76. * \param[in] service service identified in URL path format
  77. * \param[in] args serialized argument message
  78. * \param[in] handler callback that will be called when message recevied from the server-stream
  79. */
  80. virtual void stream(QGrpcStream *stream, const QString &service, QAbstractGrpcClient *client) = 0;
  81. virtual std::shared_ptr<QAbstractProtobufSerializer> serializer() const = 0;
  82. const QThread *thread() const;
  83. protected:
  84. //! \private
  85. QAbstractGrpcChannel();
  86. //! \private
  87. virtual ~QAbstractGrpcChannel();
  88. /*!
  89. * \private
  90. * \brief Aborts async call for given \p reply
  91. * \param[in] reply returned by asynchronous QAbstractGrpcChannel::call() method
  92. */
  93. virtual void abort(QGrpcAsyncReply *reply);
  94. /*!
  95. * \private
  96. * \brief Cancels \p stream
  97. * \param[in] stream returned by QAbstractGrpcChannel::stream() method
  98. */
  99. virtual void cancel(QGrpcStream *stream);
  100. friend class QGrpcAsyncReply;
  101. friend class QGrpcStream;
  102. private:
  103. Q_DISABLE_COPY(QAbstractGrpcChannel)
  104. std::unique_ptr<QAbstractGrpcChannelPrivate> dPtr;
  105. };
  106. }