qgrpcasyncoperationbase_p.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 common logic to
  38. * handle communication in Grpc channel.
  39. */
  40. class Q_GRPC_EXPORT QGrpcAsyncOperationBase : public QObject
  41. {
  42. Q_OBJECT
  43. public:
  44. /*!
  45. * \brief Reads message from raw byte array stored in QGrpcCallReply
  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. auto client = static_cast<QAbstractGrpcClient*>(parent());
  53. if (client) {
  54. client->tryDeserialize(value, m_data);
  55. }
  56. return value;
  57. }
  58. /*!
  59. * \brief Interface for implementation of QAbstractGrpcChannel. Should be used to write raw data from channel to
  60. * reply
  61. * \param data Raw data received from channel
  62. */
  63. void setData(const QByteArray &data)
  64. {
  65. QMutexLocker locker(&m_asyncLock);
  66. m_data = data;
  67. }
  68. signals:
  69. /*!
  70. * \brief The signal is emitted when reply is ready for read. Usualy called by channel when all chunks of data
  71. * recevied
  72. */
  73. void finished();
  74. /*!
  75. * \brief The signal is emitted when error happend in channel or during serialization
  76. * \param[out] status received from gRPC channel
  77. */
  78. void error(const QtProtobuf::QGrpcStatus &status);
  79. protected:
  80. //! \private
  81. QGrpcAsyncOperationBase(const std::shared_ptr<QAbstractGrpcChannel> &channel, QAbstractGrpcClient *parent) : QObject(parent)
  82. , m_channel(channel) {}
  83. //! \private
  84. virtual ~QGrpcAsyncOperationBase();
  85. std::shared_ptr<QAbstractGrpcChannel> m_channel;
  86. private:
  87. QGrpcAsyncOperationBase();
  88. Q_DISABLE_COPY_MOVE(QGrpcAsyncOperationBase)
  89. friend class QAbstractGrpcClient;
  90. QByteArray m_data;
  91. QMutex m_asyncLock;
  92. };
  93. }