qgrpcasyncoperationbase_p.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  26. #include <QObject>
  27. #include <QMutex>
  28. #include <functional>
  29. #include <memory>
  30. #include "qabstractgrpcchannel.h"
  31. #include "qabstractgrpcclient.h"
  32. #include "qtgrpcglobal.h"
  33. namespace QtProtobuf {
  34. /*!
  35. * \ingroup QtGrpc
  36. * \private
  37. * \brief The QGrpcAsyncOperationBase class implements subscription logic
  38. */
  39. class Q_GRPC_EXPORT QGrpcAsyncOperationBase : public QObject
  40. {
  41. Q_OBJECT
  42. public:
  43. /*!
  44. * \brief Reads message from raw byte array stored in QGrpcAsyncReply
  45. * \return Copy of deserialized message or non-initialized message in case of exceptional situation
  46. */
  47. template <typename T>
  48. T read() {
  49. QMutexLocker locker(&m_asyncLock);
  50. T value;
  51. try {
  52. value.deserialize(static_cast<QAbstractGrpcClient*>(parent())->serializer(), m_data);
  53. } catch (std::invalid_argument &) {
  54. static const QLatin1String invalidArgumentErrorMessage("Response deserialization failed invalid field found");
  55. error({QGrpcStatus::InvalidArgument, invalidArgumentErrorMessage});
  56. } catch (std::out_of_range &) {
  57. static const QLatin1String outOfRangeErrorMessage("Invalid size of received buffer");
  58. error({QGrpcStatus::OutOfRange, outOfRangeErrorMessage});
  59. } catch (...) {
  60. error({QGrpcStatus::Internal, QLatin1String("Unknown exception caught during deserialization")});
  61. }
  62. return value;
  63. }
  64. /*!
  65. * \brief Interface for implementation of QAbstractGrpcChannel. Should be used to write raw data from channel to
  66. * reply
  67. * \param data Raw data received from channel
  68. */
  69. void setData(const QByteArray &data)
  70. {
  71. QMutexLocker locker(&m_asyncLock);
  72. m_data = data;
  73. }
  74. signals:
  75. /*!
  76. * \brief The signal is emitted when reply is ready for read. Usualy called by channel when all chunks of data
  77. * recevied
  78. */
  79. void finished();
  80. /*!
  81. * \brief The signal is emitted when error happend in channel or during serialization
  82. * \param[out] status received from gRPC channel
  83. */
  84. void error(const QGrpcStatus &status);
  85. protected:
  86. //! \private
  87. QGrpcAsyncOperationBase(const std::shared_ptr<QAbstractGrpcChannel> &channel, QAbstractGrpcClient *parent) : QObject(parent)
  88. , m_channel(channel) {}
  89. //! \private
  90. virtual ~QGrpcAsyncOperationBase();
  91. std::shared_ptr<QAbstractGrpcChannel> m_channel;
  92. private:
  93. QGrpcAsyncOperationBase();
  94. Q_DISABLE_COPY_MOVE(QGrpcAsyncOperationBase)
  95. friend class QAbstractGrpcClient;
  96. QByteArray m_data;
  97. QMutex m_asyncLock;
  98. };
  99. }