qgrpcasyncreply.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 //QGrpcAsyncReply
  26. #include <functional>
  27. #include <QPointer>
  28. #include <QMutex>
  29. #include <memory>
  30. #include "qabstractgrpcchannel.h"
  31. #include "qabstractgrpcclient.h"
  32. #include "qtgrpcglobal.h"
  33. namespace QtProtobuf {
  34. /*!
  35. * \ingroup QtGrpc
  36. * \brief The QGrpcAsyncReply class contains data for asynchronous call of gRPC client API. It's owned by client class, that
  37. * created it. QGrpcAsyncReply coul be used by QAbstractGrpcChannel implementations to control call work flow and
  38. * abort calls if possible in case if QGrpcAsyncReply::abort method called by library user.
  39. */
  40. class Q_GRPC_EXPORT QGrpcAsyncReply final : public QObject
  41. {
  42. Q_OBJECT
  43. public:
  44. /*!
  45. * \brief Reads message from raw byte array stored in QGrpcAsyncReply
  46. * \return Copy of deserialized message or non-initialized message in case of exceptional situation
  47. */
  48. template <typename T>
  49. T read() {
  50. QMutexLocker locker(&m_asyncLock);
  51. T value;
  52. try {
  53. value.deserialize(static_cast<QAbstractGrpcClient*>(parent())->serializer(), m_data);
  54. } catch (std::invalid_argument &) {
  55. static const QLatin1String invalidArgumentErrorMessage("Response deserialization failed invalid field found");
  56. error({QGrpcStatus::InvalidArgument, invalidArgumentErrorMessage});
  57. } catch (std::out_of_range &) {
  58. static const QLatin1String outOfRangeErrorMessage("Invalid size of received buffer");
  59. error({QGrpcStatus::OutOfRange, outOfRangeErrorMessage});
  60. } catch (...) {
  61. error({QGrpcStatus::Internal, QLatin1String("Unknown exception caught during deserialization")});
  62. }
  63. return value;
  64. }
  65. /*!
  66. * \brief Interface for implementation of QAbstractGrpcChannel. Should be used to write raw data from channel to
  67. * reply
  68. * \param data Raw data received from channel
  69. */
  70. void setData(const QByteArray &data)
  71. {
  72. QMutexLocker locker(&m_asyncLock);
  73. m_data = data;
  74. }
  75. /*!
  76. * \brief Aborts this reply and try to abort call in channel
  77. */
  78. void abort() {
  79. m_channel->abort(this);
  80. }
  81. /*!
  82. * \brief Subscribe to QGrpcAsyncReply signals
  83. */
  84. template <typename Func1, typename Func2>
  85. inline void subscribe(QObject *receiver, Func1 finishCallback, Func2 errorCallback,
  86. Qt::ConnectionType type = Qt::AutoConnection)
  87. {
  88. QObject::connect(this, &QGrpcAsyncReply::finished, receiver, finishCallback, type);
  89. QObject::connect(this, &QGrpcAsyncReply::error, receiver, errorCallback, type);
  90. }
  91. /*!
  92. * \brief Overloaded QGrpcAsyncReply::subscribe method, to subscribe to finished signal
  93. * only
  94. */
  95. template <typename Func1>
  96. inline void subscribe(QObject *receiver, Func1 finishCallback,
  97. Qt::ConnectionType type = Qt::AutoConnection)
  98. {
  99. QObject::connect(this, &QGrpcAsyncReply::finished, receiver, finishCallback, type);
  100. }
  101. signals:
  102. /*!
  103. * \brief The signal is emitted when reply is ready for read. Usualy called by channel when all chunks of data
  104. * recevied
  105. */
  106. void finished();
  107. /*!
  108. * \brief The signal is emitted when error happend in channel or during serialization
  109. * \param code gRPC channel QGrpcStatus::StatusCode
  110. * \param errorMessage Description of error occured
  111. */
  112. void error(const QGrpcStatus &status);
  113. protected:
  114. //! \private
  115. QGrpcAsyncReply(const std::shared_ptr<QAbstractGrpcChannel> &channel, QAbstractGrpcClient *parent = nullptr) : QObject(parent)
  116. , m_channel(channel) {}
  117. //! \private
  118. ~QGrpcAsyncReply();
  119. private:
  120. QGrpcAsyncReply();
  121. Q_DISABLE_COPY_MOVE(QGrpcAsyncReply)
  122. friend class QAbstractGrpcClient;
  123. std::shared_ptr<QAbstractGrpcChannel> m_channel;
  124. QByteArray m_data;
  125. QMutex m_asyncLock;
  126. };
  127. }