abstractclient.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <memory>
  27. #include <functional>
  28. #include <type_traits>
  29. #include <QObject>
  30. #include <QByteArray>
  31. #include <qtprotobuflogging.h>
  32. #include "abstractchannel.h"
  33. #include "asyncreply.h"
  34. namespace qtprotobuf {
  35. class AbstractChannel;
  36. class AbstractClientPrivate;
  37. class AbstractClient : public QObject
  38. {
  39. public:
  40. void attachChannel(std::shared_ptr<AbstractChannel> channel);
  41. AbstractChannel::StatusCodes lastError() const;
  42. QString lastErrorString() const;
  43. protected:
  44. AbstractClient(const QString &service, QObject *parent = nullptr);
  45. virtual ~AbstractClient();
  46. template<typename A, typename R>
  47. bool call(const QString &method, const A &arg, R &ret) {
  48. QByteArray retData;
  49. if (call(method, arg.serialize(), retData)) {
  50. try {
  51. ret.deserialize(retData);
  52. } catch (std::invalid_argument &) {
  53. qProtoCritical() << "Response deserialization failed invalid field found";
  54. return false;
  55. } catch (std::out_of_range &) {
  56. qProtoCritical() << "Invalid size of received buffer";
  57. return false;
  58. } catch (...) {
  59. throw;
  60. }
  61. return true;
  62. }
  63. return false;
  64. }
  65. template<typename A>
  66. AsyncReply *call(const QString &method, const A &arg) {
  67. return call(method, arg.serialize());
  68. }
  69. template<typename A, typename R, typename C,
  70. typename std::enable_if_t<std::is_base_of<AbstractClient, C>::value, int> = 0>
  71. void subscribe(const QString &method, const A &arg, void(C::*signal)(const R &)) {
  72. subscribe_p(method, arg.serialize(), [this, signal](const QByteArray &data) {
  73. R ret;
  74. ret.deserialize(data);
  75. C* client = static_cast<C*>(this);
  76. (client->*signal)(ret);
  77. });
  78. }
  79. template<typename A, typename R>
  80. void subscribe(const QString &method, const A &arg, R &ret) {
  81. subscribe_p(method, arg.serialize(), [&ret](const QByteArray &data) {
  82. ret.deserialize(data);
  83. });
  84. }
  85. private:
  86. bool call(const QString &method, const QByteArray& arg, QByteArray& ret);
  87. AsyncReply *call(const QString &method, const QByteArray& arg);
  88. void subscribe_p(const QString &method, const QByteArray& arg, const std::function<void(const QByteArray&)> &handler);
  89. Q_DISABLE_COPY(AbstractClient)
  90. AbstractClientPrivate *d;
  91. };
  92. }