qquickgrpcstream.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 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. #include "qquickgrpcstream_p.h"
  26. #include <QGrpcStream>
  27. #include <QJSEngine>
  28. #include <QQmlEngine>
  29. using namespace QtProtobuf;
  30. QQuickGrpcStream::QQuickGrpcStream(QObject *parent) : QObject(parent)
  31. , m_enabled(false)
  32. , m_returnValue(nullptr)
  33. {
  34. }
  35. QQuickGrpcStream::~QQuickGrpcStream()
  36. {
  37. if (m_stream) {
  38. m_stream->abort();
  39. }
  40. delete m_returnValue;
  41. }
  42. void QQuickGrpcStream::updateStream()
  43. {
  44. if (m_stream) {
  45. m_stream->abort();
  46. m_stream.reset();
  47. }
  48. if (m_returnValue != nullptr) {
  49. m_returnValue->deleteLater(); //TODO: probably need to take care about return value cleanup other way. It's just reminder about weak memory management.
  50. m_returnValue = nullptr;
  51. returnValueChanged();
  52. }
  53. if (m_client.isNull() || m_method.isEmpty() || !m_enabled || m_argument.isNull()) {
  54. return;
  55. }
  56. if (!stream()) {
  57. setEnabled(false);
  58. }
  59. }
  60. bool QQuickGrpcStream::stream()
  61. {
  62. QString uppercaseMethodName = m_method;
  63. uppercaseMethodName.replace(0, 1, m_method[0].toUpper());
  64. const QMetaObject *metaObject = m_client->metaObject();
  65. QMetaMethod method;
  66. for (int i = 0; i < metaObject->methodCount(); i++) {
  67. if (QString("qmlStream%1_p").arg(uppercaseMethodName) == metaObject->method(i).name()) {
  68. method = metaObject->method(i);
  69. break;
  70. }
  71. }
  72. QString errorString;
  73. if (!method.isValid()) {
  74. errorString = m_method + "is not either server or bidirectional stream.";
  75. qProtoWarning() << errorString;
  76. error({QGrpcStatus::Unimplemented, errorString});
  77. return false;
  78. }
  79. if (method.parameterCount() < 2) {
  80. errorString = QString("Unable to call ") + method.name() + ". Invalid arguments set.";
  81. qProtoWarning() << errorString;
  82. error({QGrpcStatus::InvalidArgument, errorString});
  83. return false;
  84. }
  85. QMetaType argumentPointerMetaType(method.parameterType(0));
  86. if (argumentPointerMetaType.metaObject() != m_argument->metaObject()) {
  87. errorString = QString("Unable to call ") + method.name() + ". Argument type mismatch: '" + method.parameterTypes().at(0) + "' expected, '" + m_argument->metaObject()->className() + "' provided";
  88. qProtoWarning() << errorString;
  89. error({QGrpcStatus::InvalidArgument, errorString});
  90. return false;
  91. }
  92. QMetaType argumentMetaType(QMetaType::type(m_argument->metaObject()->className()));
  93. if (!argumentMetaType.isValid()) {
  94. errorString = QString("Argument of type '") + m_argument->metaObject()->className() + "' is not registred in metatype system";
  95. qProtoWarning() << errorString;
  96. error({QGrpcStatus::InvalidArgument, errorString});
  97. return false;
  98. }
  99. QObject *argument = reinterpret_cast<QObject*>(argumentMetaType.create(m_argument));
  100. if (argument == nullptr) {
  101. errorString = "Unable to create argument copy. Unknown metatype system error";
  102. qProtoWarning() << errorString;
  103. error({QGrpcStatus::InvalidArgument, errorString});
  104. return false;
  105. }
  106. argument->deleteLater(); //TODO: probably need to take care about temporary argument value cleanup other way. It's just reminder about weak memory management.
  107. QMetaType returnPointerType(method.parameterType(1));
  108. if (!returnPointerType.isValid()) {
  109. errorString = QString("Return type argument of type '") + method.parameterTypes().at(1) + "' is not registred in metatype system";
  110. qProtoWarning() << errorString;
  111. error({QGrpcStatus::InvalidArgument, errorString});
  112. return false;
  113. }
  114. QMetaType returnMetaType(QMetaType::type(returnPointerType.metaObject()->className()));
  115. if (!returnMetaType.isValid()) {
  116. errorString = QString("Unable to allocate return value. '") + returnPointerType.metaObject()->className() + "' is not registred in metatype system";
  117. qProtoWarning() << errorString;
  118. error({QGrpcStatus::InvalidArgument, errorString});
  119. return false;
  120. }
  121. m_returnValue = reinterpret_cast<QObject*>(returnMetaType.create());
  122. qmlEngine(this)->setObjectOwnership(m_returnValue, QQmlEngine::CppOwnership);
  123. returnValueChanged();
  124. if (m_returnValue == nullptr) {
  125. errorString = "Unable to allocate return value. Unknown metatype system error";
  126. qProtoWarning() << errorString;
  127. error({QGrpcStatus::Unknown, errorString});
  128. return false;
  129. }
  130. QGrpcStreamShared stream = nullptr;
  131. bool ok = method.invoke(m_client, Qt::DirectConnection,
  132. QGenericReturnArgument("QtProtobuf::QGrpcStreamShared", static_cast<void *>(&stream)),
  133. QGenericArgument(method.parameterTypes().at(0).data(), static_cast<const void *>(&argument)),
  134. QGenericArgument(method.parameterTypes().at(1).data(), static_cast<const void *>(&m_returnValue)));
  135. if (!ok || stream == nullptr) {
  136. errorString = QString("Unable to call ") + m_method + " invalidate stream.";
  137. qProtoWarning() << errorString;
  138. error({QGrpcStatus::Unknown, errorString});
  139. return false;
  140. }
  141. m_stream = stream;
  142. connect(m_stream.get(), &QGrpcStream::messageReceived, this, [this](){
  143. messageReceived(qjsEngine(this)->toScriptValue(m_returnValue));
  144. });
  145. connect(m_stream.get(), &QGrpcStream::error, this, &QQuickGrpcStream::error);//TODO: Probably it's good idea to disable stream here
  146. connect(m_stream.get(), &QGrpcStream::finished, this, [this](){ m_stream.reset(); setEnabled(false); });
  147. return true;
  148. }