qgrpcasyncoperationbase_p.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "qabstractgrpcclient.h"
  31. #include "qtgrpcglobal.h"
  32. namespace QtProtobuf {
  33. /*!
  34. * \ingroup QtGrpc
  35. * \private
  36. * \brief The QGrpcAsyncOperationBase class implements common logic to
  37. * handle communication in Grpc channel.
  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 QGrpcCallReply
  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. auto client = static_cast<QAbstractGrpcClient*>(parent());
  52. if (client) {
  53. client->tryDeserialize(value, m_data);
  54. }
  55. return value;
  56. }
  57. /*!
  58. * \brief Interface for implementation of QAbstractGrpcChannel. Should be used to write raw data from channel to
  59. * reply
  60. * \param data Raw data received from channel
  61. */
  62. void setData(const QByteArray &data)
  63. {
  64. QMutexLocker locker(&m_asyncLock);
  65. m_data = data;
  66. }
  67. virtual void abort() = 0;
  68. signals:
  69. /*!
  70. * \brief The signal indicates the end of communication for this call. If signal emitted by
  71. * stream this means that stream is succesfully closed either by client or server.
  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(QAbstractGrpcClient *parent) : QObject(parent) {}
  82. //! \private
  83. virtual ~QGrpcAsyncOperationBase();
  84. private:
  85. QGrpcAsyncOperationBase();
  86. Q_DISABLE_COPY_MOVE(QGrpcAsyncOperationBase)
  87. friend class QAbstractGrpcClient;
  88. QByteArray m_data;
  89. QMutex m_asyncLock;
  90. };
  91. }