qabstractgrpcchannel.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "qtgrpcglobal.h"
  32. namespace QtProtobuf {
  33. class QGrpcAsyncReply;
  34. class QAbstractGrpcClient;
  35. class QAbstractProtobufSerializer;
  36. /*!
  37. * \ingroup QtGrpc
  38. * \brief The QAbstractGrpcChannel class is interface that represents common gRPC channel functionality.
  39. * You may implement this interface to create own channels for gRPC transport.
  40. */
  41. class Q_GRPC_EXPORT QAbstractGrpcChannel
  42. {
  43. public:
  44. /*!
  45. * \brief Calls \p method synchronously with given serialized messge \p args and write result of call to \p ret.
  46. * \note This method is synchronous, that means it doesn't returns until call complete or aborted by timeout if it's
  47. * implemented in inherited channel.
  48. * \param[in] method remote method is called
  49. * \param[in] service service identified in URL path format
  50. * \param[in] args serialized argument message
  51. * \param[out] ret output bytearray to collect returned message
  52. * \return returns gRPC QAbstractGrpcChannel::Status code for operation
  53. */
  54. virtual QGrpcStatus call(const QString &method, const QString &service, const QByteArray &args, QByteArray &ret) = 0;
  55. /*!
  56. * \brief Calls \p method asynchronously with given serialized messge \p args. Result of method call is written to QGrpcAsyncReply.
  57. * \note This method is asynchronous, that means it returns control imediately after it is called.
  58. * \param[in] method remote method is called
  59. * \param[in] service service identified in URL path format
  60. * \param[in] args serialized argument message
  61. * \param[out] ret QGrpcAsyncReply that will be returned to end-point user to read data once call complete.
  62. * QGrpcAsyncReply lifecycle is managed by QAbstractGrpcClient only.
  63. * \see QGrpcAsyncReply for details
  64. */
  65. virtual void call(const QString &method, const QString &service, const QByteArray &args, QtProtobuf::QGrpcAsyncReply *ret) = 0;
  66. /*!
  67. * \brief Subscribes to server-side stream to receive updates for given \p method.
  68. * \param[in] method remote method is called
  69. * \param[in] service service identified in URL path format
  70. * \param[in] args serialized argument message
  71. * \param[in] handler callback that will be called when message recevied from the server-stream
  72. */
  73. virtual void subscribe(const QString &method, const QString &service, const QByteArray &args, QAbstractGrpcClient *client, const std::function<void(const QByteArray &)> &handler) = 0;
  74. virtual std::shared_ptr<QAbstractProtobufSerializer> serializer() const = 0;
  75. protected:
  76. QAbstractGrpcChannel() = default;
  77. virtual ~QAbstractGrpcChannel() = default;
  78. /*!
  79. * \brief aborts async call for given \p reply
  80. * \note by default abort is explicitly not supported by QAbstractGrpcChannel and throws assert when called
  81. * \param[in] reply returned by asynchronous QAbstractGrpcChannel::call() method
  82. */
  83. virtual void abort(QGrpcAsyncReply *reply) {
  84. Q_UNUSED(reply)
  85. assert("Abort is not supported by used channel");
  86. }
  87. friend class QGrpcAsyncReply;
  88. private:
  89. Q_DISABLE_COPY(QAbstractGrpcChannel)
  90. };
  91. }