diff --git a/.gitignore b/.gitignore index 50ac7720a..62bc0adfb 100644 --- a/.gitignore +++ b/.gitignore @@ -20,12 +20,10 @@ CMakeCache.txt compile_commands.json # Autogenerated by flatbuffers -sources/flatbufserver/hyperhdr_reply_generated.h -sources/flatbufserver/hyperhdr_request_generated.h +include/flatbuffers/parser/hyperhdr_reply_generated.h +include/flatbuffers/parser/hyperhdr_request_generated.h -external/windows - # Kdevelop project files *.kdev* diff --git a/include/base/HyperHdrInstance.h b/include/base/HyperHdrInstance.h index 50a9fb586..86a8bafe4 100644 --- a/include/base/HyperHdrInstance.h +++ b/include/base/HyperHdrInstance.h @@ -121,6 +121,7 @@ public slots: void SignalSmoothingRestarted(int suggestedInterval); void SignalRawColorsChanged(const std::vector& ledValues); void SignalInstanceJustStarted(); + void SignalColorIsSet(ColorRgb color, int duration); private slots: void handleVisibleComponentChanged(hyperhdr::Components comp); diff --git a/include/base/NetworkForwarder.h b/include/base/NetworkForwarder.h index 045c80ed3..1ce76b7f0 100644 --- a/include/base/NetworkForwarder.h +++ b/include/base/NetworkForwarder.h @@ -23,7 +23,7 @@ // Forward declaration class HyperHdrInstance; class QTcpSocket; -class FlatBufferConnection; +class FlatBuffersClient; class NetworkForwarder : public QObject { @@ -41,6 +41,7 @@ class NetworkForwarder : public QObject public slots: void startedHandler(); + void signalColorIsSetHandler(ColorRgb color, int duration); void signalForwardImageHandler(); void handlerInstanceImageUpdated(const Image& ret); @@ -60,7 +61,7 @@ private slots: const int _priority; - QList _forwardClients; + QList _forwardClients; std::atomic _hasImage; Image _image; }; diff --git a/include/flatbufserver/FlatBufferConnection.h b/include/flatbuffers/client/FlatBuffersClient.h similarity index 74% rename from include/flatbufserver/FlatBufferConnection.h rename to include/flatbuffers/client/FlatBuffersClient.h index b975ad2f3..ce4914f6d 100644 --- a/include/flatbufserver/FlatBufferConnection.h +++ b/include/flatbuffers/client/FlatBuffersClient.h @@ -11,23 +11,16 @@ #include #include -#include - -namespace hyperhdrnet -{ - struct Reply; -} - #define HYPERHDR_DOMAIN_SERVER QStringLiteral("hyperhdr-domain") -class FlatBufferConnection : public QObject +class FlatBuffersClient : public QObject { Q_OBJECT public: - FlatBufferConnection(QObject* parent, const QString& origin, const QString& address, int priority, bool skipReply); - ~FlatBufferConnection() override; + FlatBuffersClient(QObject* parent, const QString& origin, const QString& address, int priority, bool skipReply); + ~FlatBuffersClient() override; void setSkipReply(bool skip); void setRegister(const QString& origin, int priority); @@ -38,6 +31,7 @@ class FlatBufferConnection : public QObject public slots: void sendImage(const Image& image); + void setColorHandler(ColorRgb color, int duration); private slots: void connectToHost(); @@ -45,9 +39,10 @@ private slots: signals: void SignalImageToSend(const Image& image); + void SignalSetColor(ColorRgb color, int duration); private: - bool parseReply(const hyperhdrnet::Reply* reply); + bool initParserLibrary(); QTcpSocket* _socket; QLocalSocket* _domain; @@ -62,7 +57,7 @@ private slots: QLocalSocket::LocalSocketState _prevLocalState; Logger* _log; - flatbuffers::FlatBufferBuilder _builder; + void* _builder; bool _registered; bool _sent; diff --git a/include/flatbuffers/parser/FlatBuffersParser.h b/include/flatbuffers/parser/FlatBuffersParser.h new file mode 100644 index 000000000..4ae892fe8 --- /dev/null +++ b/include/flatbuffers/parser/FlatBuffersParser.h @@ -0,0 +1,50 @@ +/* FlatBuffersParser.h +* +* MIT License +* +* Copyright (c) 2020-2024 awawa-dev +* +* Project homesite: https://github.com/awawa-dev/HyperHDR +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + +#pragma once +#include +#include + +namespace FlatBuffersParser +{ + enum FLATBUFFERS_PACKAGE_TYPE { COLOR = 1, IMAGE, CLEAR, PRIORITY, ERROR }; + + + void* createFlatbuffersBuilder(); + void releaseFlatbuffersBuilder(void* builder); + void clearFlatbuffersBuilder(void* builder); + void encodeImageIntoFlatbuffers(void* builder, const uint8_t* rawImageMem, size_t rawImagesize, int imageWidth, int imageHeight, uint8_t** buffer, size_t* bufferSize); + void encodeClearPriorityIntoFlatbuffers(void* builder, int priority, uint8_t** buffer, size_t* bufferSize); + void encodeRegisterPriorityIntoFlatbuffers(void* builder, int priority, const char* name, uint8_t** buffer, size_t* bufferSize); + void encodeColorIntoFlatbuffers(void* builder, int red, int green, int blue, int priority, int duration, uint8_t** buffer, size_t* bufferSize); + bool verifyFlatbuffersReplyBuffer(const uint8_t* messageData, size_t messageSize, bool* _sent, bool* _registered, int* _priority); + int decodeIncomingFlatbuffersFrame(void* builder, const uint8_t* messageData, size_t messageSize, + uint8_t* red, uint8_t* green, uint8_t* blue, + int* priority, std::string* clientDescription, int* duration, + uint8_t** imageData, int* imageWidth, int* imageHeight, size_t* imageSize, + uint8_t** buffer, size_t* bufferSize); +} diff --git a/sources/flatbufserver/hyperhdr_reply.fbs b/include/flatbuffers/parser/hyperhdr_reply.fbs similarity index 100% rename from sources/flatbufserver/hyperhdr_reply.fbs rename to include/flatbuffers/parser/hyperhdr_reply.fbs diff --git a/sources/flatbufserver/hyperhdr_request.fbs b/include/flatbuffers/parser/hyperhdr_request.fbs similarity index 100% rename from sources/flatbufserver/hyperhdr_request.fbs rename to include/flatbuffers/parser/hyperhdr_request.fbs diff --git a/include/flatbufserver/FlatBufferServer.h b/include/flatbuffers/server/FlatBuffersServer.h similarity index 79% rename from include/flatbufserver/FlatBufferServer.h rename to include/flatbuffers/server/FlatBuffersServer.h index 53b96cad8..fcf5c7b3c 100644 --- a/include/flatbufserver/FlatBufferServer.h +++ b/include/flatbuffers/server/FlatBuffersServer.h @@ -13,18 +13,18 @@ class BonjourServiceRegister; class QTcpServer; class QLocalServer; -class FlatBufferClient; +class FlatBuffersServerConnection; class NetOrigin; #define HYPERHDR_DOMAIN_SERVER QStringLiteral("hyperhdr-domain") #define BASEAPI_FLATBUFFER_USER_LUT_FILE QStringLiteral("BASEAPI_user_lut_file") -class FlatBufferServer : public QObject +class FlatBuffersServer : public QObject { Q_OBJECT public: - FlatBufferServer(std::shared_ptr netOrigin, const QJsonDocument& config, const QString& configurationPath, QObject* parent = nullptr); - ~FlatBufferServer() override; + FlatBuffersServer(std::shared_ptr netOrigin, const QJsonDocument& config, const QString& configurationPath, QObject* parent = nullptr); + ~FlatBuffersServer() override; signals: void SignalSetNewComponentStateToAllInstances(hyperhdr::Components component, bool enable); @@ -40,14 +40,14 @@ public slots: private slots: void handlerNewConnection(); - void handlerClientDisconnected(FlatBufferClient* client); + void handlerClientDisconnected(FlatBuffersServerConnection* client); private: void startServer(); void stopServer(); QString GetSharedLut(); void loadLutFile(); - void setupClient(FlatBufferClient* client); + void setupClient(FlatBuffersServerConnection* client); QTcpServer* _server; QLocalServer* _domain; @@ -58,7 +58,7 @@ private slots: const QJsonDocument _config; BonjourServiceRegister* _serviceRegister = nullptr; - QVector _openConnections; + QVector _openConnections; int _hdrToneMappingMode; int _realHdrToneMappingMode; diff --git a/include/flatbuffers/server/FlatBuffersServerConnection.h b/include/flatbuffers/server/FlatBuffersServerConnection.h new file mode 100644 index 000000000..e5665bd90 --- /dev/null +++ b/include/flatbuffers/server/FlatBuffersServerConnection.h @@ -0,0 +1,81 @@ +/* FlatBuffersServerConnection.h +* +* MIT License +* +* Copyright (c) 2020-2024 awawa-dev +* +* Project homesite: https://github.com/awawa-dev/HyperHDR +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + +#pragma once + +// util +#include +#include +#include +#include +#include + +class QTcpSocket; +class QLocalSocket; +class QTimer; + +class FlatBuffersServerConnection : public QObject +{ + Q_OBJECT + +public: + explicit FlatBuffersServerConnection(QTcpSocket* socket, QLocalSocket* domain, int timeout, QObject* parent = nullptr); + ~FlatBuffersServerConnection(); + +signals: + void SignalClearGlobalInput(int priority, bool forceClearAll); + void SignalImageReceived(int priority, const Image& image, int timeout_ms, hyperhdr::Components origin, QString clientDescription); + void SignalSetGlobalColor(int priority, const std::vector& ledColor, int timeout_ms, hyperhdr::Components origin, QString clientDescription); + void SignalClientDisconnected(FlatBuffersServerConnection* client); + +public slots: + void forceClose(); + +private slots: + void readyRead(); + void disconnected(); + +private: + void sendMessage(uint8_t* buffer, size_t size); + bool initParserLibrary(); + +private: + Logger* _log; + QTcpSocket* _socket; + QLocalSocket* _domain; + QString _clientAddress; + QTimer* _timeoutTimer; + int _timeout; + int _priority; + QString _clientDescription; + int _mode; + + void* _builder; + uint32_t _incomingSize; + uint32_t _incomingIndex; + MemoryBuffer _incommingBuffer; +}; diff --git a/include/utils/Image.h b/include/utils/Image.h index 46e1e7d18..aa23290c0 100644 --- a/include/utils/Image.h +++ b/include/utils/Image.h @@ -1,9 +1,5 @@ #pragma once -#ifndef PCH_ENABLED - #include -#endif - #include template diff --git a/sources/CMakeLists.txt b/sources/CMakeLists.txt index d572d7bd8..90d340bc8 100644 --- a/sources/CMakeLists.txt +++ b/sources/CMakeLists.txt @@ -16,7 +16,7 @@ add_subdirectory(blackborder) add_subdirectory(commandline) add_subdirectory(db) add_subdirectory(effectengine) -add_subdirectory(flatbufserver) +add_subdirectory(flatbuffers) add_subdirectory(grabber) add_subdirectory(hyperimage) add_subdirectory(jsonserver) diff --git a/sources/api/BaseAPI.cpp b/sources/api/BaseAPI.cpp index c514bae8e..b1c05700e 100644 --- a/sources/api/BaseAPI.cpp +++ b/sources/api/BaseAPI.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/sources/api/CallbackAPI.cpp b/sources/api/CallbackAPI.cpp index e320c7337..d9f0eb726 100644 --- a/sources/api/CallbackAPI.cpp +++ b/sources/api/CallbackAPI.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include using namespace hyperhdr; diff --git a/sources/api/HyperAPI.cpp b/sources/api/HyperAPI.cpp index 3a0a79391..bf56eda6f 100644 --- a/sources/api/HyperAPI.cpp +++ b/sources/api/HyperAPI.cpp @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/sources/base/CMakeLists.txt b/sources/base/CMakeLists.txt index 2921a8bdc..bc94a03e7 100644 --- a/sources/base/CMakeLists.txt +++ b/sources/base/CMakeLists.txt @@ -2,10 +2,6 @@ SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/base) SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sources/base) -include_directories( - ${CMAKE_CURRENT_BINARY_DIR}/../../sources/flatbufserver -) - FILE ( GLOB HyperHDR_SOURCES "${CURRENT_HEADER_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.cpp" ) SET(HyperHDR_RESOURCES ${CURRENT_SOURCE_DIR}/resource.qrc) @@ -18,7 +14,8 @@ add_library(hyperhdr-base target_link_libraries(hyperhdr-base blackborder hyperhdr-utils - flatbufserver + flatbuffers_server + flatbuffers_client flatbuffers leddevice effectengine diff --git a/sources/base/HyperHdrInstance.cpp b/sources/base/HyperHdrInstance.cpp index d911ed95a..c25162ea3 100644 --- a/sources/base/HyperHdrInstance.cpp +++ b/sources/base/HyperHdrInstance.cpp @@ -576,7 +576,10 @@ void HyperHdrInstance::setColor(int priority, const std::vector& ledCo { clear(priority); } - + if (getCurrentPriority() == priority) + { + emit SignalColorIsSet(ledColors[0], timeout_ms); + } // register color _muxer->registerInput(priority, hyperhdr::COMP_COLOR, origin, ledColors[0]); _muxer->setInput(priority, timeout_ms); diff --git a/sources/base/NetworkForwarder.cpp b/sources/base/NetworkForwarder.cpp index 6bb3e3cb1..101049123 100644 --- a/sources/base/NetworkForwarder.cpp +++ b/sources/base/NetworkForwarder.cpp @@ -37,7 +37,7 @@ #include #include -#include +#include #include #include #include @@ -75,6 +75,8 @@ void NetworkForwarder::startedHandler() connect(this, &NetworkForwarder::SignalForwardImage, this, &NetworkForwarder::signalForwardImageHandler, Qt::QueuedConnection); connect(hyperhdr.get(), &HyperHdrInstance::SignalInstanceSettingsChanged, this, &NetworkForwarder::handleSettingsUpdate); connect(hyperhdr.get(), &HyperHdrInstance::SignalRequestComponent, this, &NetworkForwarder::handleCompStateChangeRequest); + connect(hyperhdr.get(), &HyperHdrInstance::SignalColorIsSet, this, &NetworkForwarder::signalColorIsSetHandler); + handleCompStateChangeRequest(hyperhdr::COMP_FORWARDER, true); } @@ -230,8 +232,15 @@ void NetworkForwarder::addFlatbufferSlave(const QString& slave, const QJsonObjec { _flatSlaves << slave; - FlatBufferConnection* flatbuf = new FlatBufferConnection(this, "Forwarder", slave, _priority, false); - _forwardClients << flatbuf; + try + { + FlatBuffersClient* flatbuf = new FlatBuffersClient(this, "Forwarder", slave, _priority, false); + _forwardClients << flatbuf; + } + catch (std::exception& ex) + { + Error(_log, "Could not initialize client: %s", ex.what()); + } } } @@ -253,6 +262,14 @@ void NetworkForwarder::forwardJsonMessage(const QJsonObject& message) } } +void NetworkForwarder::signalColorIsSetHandler(ColorRgb color, int duration) +{ + for (int i = 0; i < _forwardClients.size(); i++) + { + emit _forwardClients.at(i)->SignalSetColor(color, duration); + } +} + void NetworkForwarder::handlerInstanceImageUpdated(const Image& image) { if (_hasImage.exchange(true)) diff --git a/sources/flatbuffers/CMakeLists.txt b/sources/flatbuffers/CMakeLists.txt new file mode 100644 index 000000000..199e17cae --- /dev/null +++ b/sources/flatbuffers/CMakeLists.txt @@ -0,0 +1,80 @@ +set(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/flatbuffers) +set(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sources/flatbuffers) + +########################################### FLATBUFFERS ##################################################### + +set(Flatbuffer_GENERATED_FBS + ${CURRENT_HEADER_DIR}/parser/hyperhdr_reply_generated.h + ${CURRENT_HEADER_DIR}/parser/hyperhdr_request_generated.h +) + +set(Flatbuffer_FBS + ${CURRENT_HEADER_DIR}/parser/hyperhdr_reply.fbs + ${CURRENT_HEADER_DIR}/parser/hyperhdr_request.fbs +) + +FOREACH(FBS_FILE ${Flatbuffer_FBS}) + compile_flattbuffer_schema(${FBS_FILE} "${CURRENT_HEADER_DIR}/parser") +ENDFOREACH(FBS_FILE) + +set_source_files_properties( + ${Flatbuffer_GENERATED_FBS} PROPERTIES GENERATED TRUE +) + + +############################################# PARSER ####################################################### + +FILE ( GLOB FLATBUF_PARSER_SOURCES "${CURRENT_HEADER_DIR}/parser/*.h" "${CURRENT_SOURCE_DIR}/parser/*.cpp" ) + +include_directories("${CURRENT_HEADER_DIR}/parser") + +add_library(flatbuffers_parser + ${FLATBUF_PARSER_SOURCES} + ${Flatbuffer_GENERATED_FBS} +) + +target_link_libraries(flatbuffers_parser PRIVATE + flatbuffers +) + +############################################# SERVER ####################################################### + +FILE ( GLOB FLATBUF_SERVER_SOURCES "${CURRENT_HEADER_DIR}/server/*.h" "${CURRENT_SOURCE_DIR}/server/*.cpp" ) + +include_directories("${CURRENT_HEADER_DIR}/server") + +add_library(flatbuffers_server + ${FLATBUF_SERVER_SOURCES} +) + +target_link_libraries(flatbuffers_server + hyperhdr-utils + flatbuffers_parser + Qt${Qt_VERSION}::Network + Qt${Qt_VERSION}::Core +) + +if(USE_PRECOMPILED_HEADERS AND COMMAND target_precompile_headers) + target_precompile_headers(flatbuffers_server REUSE_FROM precompiled_hyperhdr_headers) +endif() + +############################################# CLIENT ####################################################### + +FILE ( GLOB FLATBUF_CLIENT_SOURCES "${CURRENT_HEADER_DIR}/client/*.h" "${CURRENT_SOURCE_DIR}/client/*.cpp" ) + +include_directories("${CURRENT_HEADER_DIR}/client") + +add_library(flatbuffers_client + ${FLATBUF_CLIENT_SOURCES} +) + +target_link_libraries(flatbuffers_client + hyperhdr-utils + flatbuffers_parser + Qt${Qt_VERSION}::Network + Qt${Qt_VERSION}::Core +) + +if(USE_PRECOMPILED_HEADERS AND COMMAND target_precompile_headers) + target_precompile_headers(flatbuffers_client REUSE_FROM precompiled_hyperhdr_headers) +endif() diff --git a/sources/flatbufserver/FlatBufferConnection.cpp b/sources/flatbuffers/client/FlatBuffersClient.cpp similarity index 52% rename from sources/flatbufserver/FlatBufferConnection.cpp rename to sources/flatbuffers/client/FlatBuffersClient.cpp index 99f8e23c6..755f707ca 100644 --- a/sources/flatbufserver/FlatBufferConnection.cpp +++ b/sources/flatbuffers/client/FlatBuffersClient.cpp @@ -1,14 +1,42 @@ +/* FlatBuffersClient.cpp +* +* MIT License +* +* Copyright (c) 2020-2024 awawa-dev +* +* Project homesite: https://github.com/awawa-dev/HyperHDR +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + // stl includes #include // flatbuffer includes -#include +#include // flatbuffer FBS -#include "hyperhdr_reply_generated.h" -#include "hyperhdr_request_generated.h" +#include + +using namespace FlatBuffersParser; -FlatBufferConnection::FlatBufferConnection(QObject* parent, const QString& origin, const QString& address, int priority, bool skipReply) +FlatBuffersClient::FlatBuffersClient(QObject* parent, const QString& origin, const QString& address, int priority, bool skipReply) : QObject(parent) , _socket((address == HYPERHDR_DOMAIN_SERVER) ? nullptr : new QTcpSocket(this)) , _domain((address == HYPERHDR_DOMAIN_SERVER) ? new QLocalSocket(this) : nullptr) @@ -17,10 +45,16 @@ FlatBufferConnection::FlatBufferConnection(QObject* parent, const QString& origi , _prevSocketState(QAbstractSocket::UnconnectedState) , _prevLocalState(QLocalSocket::UnconnectedState) , _log(Logger::getInstance("FLATBUFCONN")) + , _builder(nullptr) , _registered(false) , _sent(false) , _lastSendImage(0) { + if (!initParserLibrary() || _builder == nullptr) + { + throw std::runtime_error("Could not initialize Flatbuffers parser"); + } + if (_socket == nullptr) Info(_log, "Connection using local domain socket. Ignoring port."); else @@ -31,7 +65,7 @@ FlatBufferConnection::FlatBufferConnection(QObject* parent, const QString& origi QStringList parts = address.split(":"); if (parts.size() != 2) { - throw std::runtime_error(QString("FLATBUFCONNECTION ERROR: Unable to parse address (%1)").arg(address).toStdString()); + throw std::runtime_error(QString("FlatBuffersClient: Unable to parse address (%1)").arg(address).toStdString()); } _host = parts[0]; @@ -39,7 +73,7 @@ FlatBufferConnection::FlatBufferConnection(QObject* parent, const QString& origi _port = parts[1].toUShort(&ok); if (!ok) { - throw std::runtime_error(QString("FLATBUFCONNECTION ERROR: Unable to parse the port (%1)").arg(parts[1]).toStdString()); + throw std::runtime_error(QString("FlatBuffersClient: Unable to parse the port (%1)").arg(parts[1]).toStdString()); } } else @@ -51,9 +85,15 @@ FlatBufferConnection::FlatBufferConnection(QObject* parent, const QString& origi if (!skipReply) { if (_socket != nullptr) - connect(_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection); + { + connect(_socket, &QTcpSocket::readyRead, this, &FlatBuffersClient::readData, Qt::UniqueConnection); + connect(_socket, &QTcpSocket::connected, this, [&]() { setRegister(_origin, _priority); }); + } else if (_domain != nullptr) - connect(_domain, &QLocalSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection); + { + connect(_domain, &QLocalSocket::readyRead, this, &FlatBuffersClient::readData, Qt::UniqueConnection); + connect(_domain, &QLocalSocket::connected, this, [&]() { setRegister(_origin, _priority); }); + } } // init connect @@ -67,13 +107,25 @@ FlatBufferConnection::FlatBufferConnection(QObject* parent, const QString& origi // start the connection timer _timer.setInterval(5000); - connect(&_timer, &QTimer::timeout, this, &FlatBufferConnection::connectToHost); + connect(&_timer, &QTimer::timeout, this, &FlatBuffersClient::connectToHost); _timer.start(); - connect(this, &FlatBufferConnection::SignalImageToSend, this, &FlatBufferConnection::sendImage); + connect(this, &FlatBuffersClient::SignalImageToSend, this, &FlatBuffersClient::sendImage); + connect(this, &FlatBuffersClient::SignalSetColor, this, &FlatBuffersClient::setColorHandler); } -FlatBufferConnection::~FlatBufferConnection() +void FlatBuffersClient::setColorHandler(ColorRgb color, int duration) +{ + setColor(color, _priority, duration); +} + +bool FlatBuffersClient::initParserLibrary() +{ + _builder = createFlatbuffersBuilder(); + return (_builder != nullptr); +} + +FlatBuffersClient::~FlatBuffersClient() { _timer.stop(); @@ -81,9 +133,12 @@ FlatBufferConnection::~FlatBufferConnection() _socket->close(); if (_domain != nullptr) _domain->close(); + + if (_builder != nullptr) + releaseFlatbuffersBuilder(_builder); } -void FlatBufferConnection::readData() +void FlatBuffersClient::readData() { if (_socket != nullptr) _receiveBuffer += _socket->readAll(); @@ -106,76 +161,69 @@ void FlatBufferConnection::readData() const QByteArray msg = _receiveBuffer.mid(4, messageSize); _receiveBuffer.remove(0, messageSize + 4); - const uint8_t* msgData = reinterpret_cast(msg.constData()); - flatbuffers::Verifier verifier(msgData, messageSize); - - if (hyperhdrnet::VerifyReplyBuffer(verifier)) - { - parseReply(hyperhdrnet::GetReply(msgData)); - continue; - } - Error(_log, "Unable to parse reply"); + if (!verifyFlatbuffersReplyBuffer(reinterpret_cast(msg.data()), messageSize, &_sent, &_registered, &_priority)) + Error(_log, "Unable to parse reply"); } } -void FlatBufferConnection::setSkipReply(bool skip) +void FlatBuffersClient::setSkipReply(bool skip) { if (_socket != nullptr) { if (skip) disconnect(_socket, &QTcpSocket::readyRead, 0, 0); else - connect(_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection); + connect(_socket, &QTcpSocket::readyRead, this, &FlatBuffersClient::readData, Qt::UniqueConnection); } if (_domain != nullptr) { if (skip) disconnect(_domain, &QLocalSocket::readyRead, 0, 0); else - connect(_domain, &QLocalSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection); + connect(_domain, &QLocalSocket::readyRead, this, &FlatBuffersClient::readData, Qt::UniqueConnection); } } -void FlatBufferConnection::setRegister(const QString& origin, int priority) +void FlatBuffersClient::setRegister(const QString& origin, int priority) { - auto registerReq = hyperhdrnet::CreateRegister(_builder, _builder.CreateString(QSTRING_CSTR(origin)), priority); - auto req = hyperhdrnet::CreateRequest(_builder, hyperhdrnet::Command_Register, registerReq.Union()); + uint8_t* outputbuffer = nullptr; + size_t outputbufferSize = 0; + + encodeRegisterPriorityIntoFlatbuffers(_builder, priority, QSTRING_CSTR(origin), &outputbuffer, &outputbufferSize); + - _builder.Finish(req); - uint32_t size = _builder.GetSize(); const uint8_t header[] = { - uint8_t((size >> 24) & 0xFF), - uint8_t((size >> 16) & 0xFF), - uint8_t((size >> 8) & 0xFF), - uint8_t((size) & 0xFF) }; + uint8_t((outputbufferSize >> 24) & 0xFF), + uint8_t((outputbufferSize >> 16) & 0xFF), + uint8_t((outputbufferSize >> 8) & 0xFF), + uint8_t((outputbufferSize) & 0xFF) }; // write message if (_socket != nullptr) { _socket->write(reinterpret_cast(header), 4); - _socket->write(reinterpret_cast(_builder.GetBufferPointer()), size); + _socket->write(reinterpret_cast(outputbuffer), outputbufferSize); _socket->flush(); } else if (_domain != nullptr) { _domain->write(reinterpret_cast(header), 4); - _domain->write(reinterpret_cast(_builder.GetBufferPointer()), size); + _domain->write(reinterpret_cast(outputbuffer), outputbufferSize); _domain->flush(); } - _builder.Clear(); + clearFlatbuffersBuilder(_builder); } -void FlatBufferConnection::setColor(const ColorRgb& color, int priority, int duration) +void FlatBuffersClient::setColor(const ColorRgb& color, int priority, int duration) { - auto colorReq = hyperhdrnet::CreateColor(_builder, (color.red << 16) | (color.green << 8) | color.blue, duration); - auto req = hyperhdrnet::CreateRequest(_builder, hyperhdrnet::Command_Color, colorReq.Union()); - - _builder.Finish(req); - sendMessage(_builder.GetBufferPointer(), _builder.GetSize()); - _builder.Clear(); + uint8_t* outputbuffer = nullptr; + size_t outputbufferSize = 0; + encodeColorIntoFlatbuffers(_builder, color.red, color.green, color.blue, priority, duration, &outputbuffer, &outputbufferSize); + sendMessage(outputbuffer, outputbufferSize); + clearFlatbuffersBuilder(_builder); } -void FlatBufferConnection::sendImage(const Image& image) +void FlatBuffersClient::sendImage(const Image& image) { auto current = InternalClock::now(); auto outOfTime = (current - _lastSendImage); @@ -200,32 +248,30 @@ void FlatBufferConnection::sendImage(const Image& image) _sent = true; _lastSendImage = current; - auto imgData = _builder.CreateVector(image.rawMem(), image.size()); - auto rawImg = hyperhdrnet::CreateRawImage(_builder, imgData, image.width(), image.height()); - auto imageReq = hyperhdrnet::CreateImage(_builder, hyperhdrnet::ImageType_RawImage, rawImg.Union(), -1); - auto req = hyperhdrnet::CreateRequest(_builder, hyperhdrnet::Command_Image, imageReq.Union()); - - _builder.Finish(req); - sendMessage(_builder.GetBufferPointer(), _builder.GetSize()); - _builder.Clear(); + // encode and send + uint8_t* outputbuffer = nullptr; + size_t outputbufferSize = 0; + encodeImageIntoFlatbuffers(_builder, image.rawMem(), image.size(), image.width(), image.height(), &outputbuffer, &outputbufferSize); + sendMessage(outputbuffer, outputbufferSize); + clearFlatbuffersBuilder(_builder); } -void FlatBufferConnection::clear(int priority) +void FlatBuffersClient::clear(int priority) { - auto clearReq = hyperhdrnet::CreateClear(_builder, priority); - auto req = hyperhdrnet::CreateRequest(_builder, hyperhdrnet::Command_Clear, clearReq.Union()); - - _builder.Finish(req); - sendMessage(_builder.GetBufferPointer(), _builder.GetSize()); - _builder.Clear(); + // encode and send + uint8_t* outputbuffer = nullptr; + size_t outputbufferSize = 0; + encodeClearPriorityIntoFlatbuffers(_builder, priority, &outputbuffer, &outputbufferSize); + sendMessage(outputbuffer, outputbufferSize); + clearFlatbuffersBuilder(_builder); } -void FlatBufferConnection::clearAll() +void FlatBuffersClient::clearAll() { clear(-1); } -void FlatBufferConnection::connectToHost() +void FlatBuffersClient::connectToHost() { // try connection only when if (_socket != nullptr && _socket->state() == QAbstractSocket::UnconnectedState) @@ -234,7 +280,7 @@ void FlatBufferConnection::connectToHost() _domain->connectToServer(_host); } -void FlatBufferConnection::sendMessage(const uint8_t* buffer, uint32_t size) +void FlatBuffersClient::sendMessage(const uint8_t* buffer, uint32_t size) { // print out connection message only when state is changed if (_socket != nullptr && _socket->state() != _prevSocketState) @@ -304,32 +350,3 @@ void FlatBufferConnection::sendMessage(const uint8_t* buffer, uint32_t size) } } -bool FlatBufferConnection::parseReply(const hyperhdrnet::Reply* reply) -{ - _sent = false; - - if (!reply->error()) - { - // no error set must be a success or registered or video - //const auto videoMode = - reply->video(); - const auto registered = reply->registered(); - /*if (videoMode != -1) { - // We got a video reply. - emit setVideoMode(static_cast(videoMode)); - return true; - }*/ - - // We got a registered reply. - if (registered == -1 || registered != _priority) - _registered = false; - else - _registered = true; - - return true; - } - else - throw std::runtime_error(reply->error()->str()); - - return false; -} diff --git a/sources/flatbuffers/parser/FlatBuffersParser.cpp b/sources/flatbuffers/parser/FlatBuffersParser.cpp new file mode 100644 index 000000000..31d490608 --- /dev/null +++ b/sources/flatbuffers/parser/FlatBuffersParser.cpp @@ -0,0 +1,225 @@ +/* FlatBuffersParser.cpp +* +* MIT License +* +* Copyright (c) 2020-2024 awawa-dev +* +* Project homesite: https://github.com/awawa-dev/HyperHDR +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + +#include +#include +#include +#include + +void* FlatBuffersParser::createFlatbuffersBuilder() +{ + auto builder = new flatbuffers::FlatBufferBuilder(); + return builder; +} + +void FlatBuffersParser::releaseFlatbuffersBuilder(void* builder) +{ + delete reinterpret_cast(builder); +} + +void FlatBuffersParser::clearFlatbuffersBuilder(void* builder) +{ + reinterpret_cast(builder)->Clear(); +} + +void FlatBuffersParser::encodeImageIntoFlatbuffers(void* builder, const uint8_t* rawImageMem, size_t rawImagesize, int imageWidth, int imageHeight, uint8_t** buffer, size_t* bufferSize) +{ + auto _builder = reinterpret_cast(builder); + auto imgData = _builder->CreateVector(rawImageMem, rawImagesize); + auto rawImg = hyperhdrnet::CreateRawImage(*_builder, imgData, imageWidth, imageHeight); + auto imageReq = hyperhdrnet::CreateImage(*_builder, hyperhdrnet::ImageType_RawImage, rawImg.Union(), -1); + auto req = hyperhdrnet::CreateRequest(*_builder, hyperhdrnet::Command_Image, imageReq.Union()); + + _builder->Finish(req); + + *buffer = _builder->GetBufferPointer(); + *bufferSize = _builder->GetSize(); +} + +void FlatBuffersParser::encodeClearPriorityIntoFlatbuffers(void* builder, int priority, uint8_t** buffer, size_t* bufferSize) +{ + auto _builder = reinterpret_cast(builder); + auto clearReq = hyperhdrnet::CreateClear(*_builder, priority); + auto req = hyperhdrnet::CreateRequest(*_builder, hyperhdrnet::Command_Clear, clearReq.Union()); + _builder->Finish(req); + + *buffer = _builder->GetBufferPointer(); + *bufferSize = _builder->GetSize(); +} + +void FlatBuffersParser::encodeRegisterPriorityIntoFlatbuffers(void* builder, int priority, const char* name, uint8_t** buffer, size_t* bufferSize) +{ + auto _builder = reinterpret_cast(builder); + auto registerReq = hyperhdrnet::CreateRegister(*_builder, _builder->CreateString(name), priority); + auto req = hyperhdrnet::CreateRequest(*_builder, hyperhdrnet::Command_Register, registerReq.Union()); + _builder->Finish(req); + + *buffer = _builder->GetBufferPointer(); + *bufferSize = _builder->GetSize(); +} + +void FlatBuffersParser::encodeColorIntoFlatbuffers(void* builder, int red, int green, int blue, int priority, int duration, uint8_t** buffer, size_t* bufferSize) +{ + auto _builder = reinterpret_cast(builder); + auto colorReq = hyperhdrnet::CreateColor(*_builder, (red << 16) | (green << 8) | blue, duration); + auto req = hyperhdrnet::CreateRequest(*_builder, hyperhdrnet::Command_Color, colorReq.Union()); + _builder->Finish(req); + + *buffer = _builder->GetBufferPointer(); + *bufferSize = _builder->GetSize(); +} + +bool FlatBuffersParser::verifyFlatbuffersReplyBuffer(const uint8_t* messageData, size_t messageSize, bool* _sent, bool* _registered, int* _priority) +{ + flatbuffers::Verifier verifier(messageData, messageSize); + + if (hyperhdrnet::VerifyReplyBuffer(verifier)) + { + auto reply = hyperhdrnet::GetReply(messageData); + + *_sent = false; + + if (!reply->error()) + { + + const auto registered = reply->registered(); + + if (registered == -1 || registered != (*_priority)) + *_registered = false; + else + *_registered = true; + + return true; + } + } + return false; +} + +namespace +{ + const char* NO_IMAGE_DATA = "Do not have frame data"; + const char* INVALID_PACKAGE = "Received invalid packet"; + const char* CANT_PARSE = "Unable to parse message"; +} + +static void sendSuccessReply(flatbuffers::FlatBufferBuilder* _builder) +{ + auto reply = hyperhdrnet::CreateReplyDirect(*_builder); + _builder->Finish(reply); +} + +static void sendErrorReply(flatbuffers::FlatBufferBuilder* _builder, const char* error) +{ + auto reply = hyperhdrnet::CreateReplyDirect(*_builder, error); + _builder->Finish(reply); +} + +int FlatBuffersParser::decodeIncomingFlatbuffersFrame(void* builder, const uint8_t* messageData, size_t messageSize, + uint8_t* red, uint8_t* green, uint8_t* blue, + int* priority, std::string* clientDescription, int* duration, + uint8_t** imageData, int* imageWidth, int* imageHeight, size_t* imageSize, + uint8_t** buffer, size_t* bufferSize) +{ + int retType = FLATBUFFERS_PACKAGE_TYPE::ERROR; + auto _builder = reinterpret_cast(builder); + flatbuffers::Verifier verifier(messageData, messageSize); + + if (hyperhdrnet::VerifyRequestBuffer(verifier)) + { + auto req = hyperhdrnet::GetRequest(messageData); + const void* reqPtr; + + if ((reqPtr = req->command_as_Color()) != nullptr) + { + auto colorReq = static_cast(reqPtr); + const int32_t rgbData = colorReq->data(); + + *red = uint8_t((rgbData >> 16) & 0xff); + *green = uint8_t((rgbData >> 8) & 0xff); + *blue = uint8_t(rgbData & 0xff); + *duration = colorReq->duration(); + + retType = FLATBUFFERS_PACKAGE_TYPE::COLOR; + sendSuccessReply(_builder); + } + else if ((reqPtr = req->command_as_Image()) != nullptr) + { + auto flatImage = static_cast(reqPtr); + *duration = flatImage->duration(); + + const void* rawFlatImage; + if ((rawFlatImage = flatImage->data_as_RawImage()) != nullptr) + { + const auto* img = static_cast(rawFlatImage); + const auto& imgD = img->data(); + + *imageData = const_cast(imgD->data()); + *imageSize = imgD->size(); + *imageWidth = img->width(); + *imageHeight = img->height(); + + retType = FLATBUFFERS_PACKAGE_TYPE::IMAGE; + sendSuccessReply(_builder); + } + else + sendErrorReply(_builder, NO_IMAGE_DATA); + } + else if ((reqPtr = req->command_as_Clear()) != nullptr) + { + auto clear = static_cast(reqPtr); + *priority = clear->priority(); + + sendSuccessReply(_builder); + retType = FLATBUFFERS_PACKAGE_TYPE::CLEAR; + } + else if ((reqPtr = req->command_as_Register()) != nullptr) + { + auto regPrio = static_cast(reqPtr); + *priority = regPrio->priority(); + *clientDescription = regPrio->origin()->c_str(); + + auto reply = hyperhdrnet::CreateReplyDirect(*_builder, nullptr, -1, *priority); + _builder->Finish(reply); + + retType = FLATBUFFERS_PACKAGE_TYPE::PRIORITY; + } + else + { + sendErrorReply(_builder, INVALID_PACKAGE); + retType = FLATBUFFERS_PACKAGE_TYPE::ERROR; + } + } + else + { + sendErrorReply(_builder, CANT_PARSE); + retType = FLATBUFFERS_PACKAGE_TYPE::ERROR; + } + + *buffer = _builder->GetBufferPointer(); + *bufferSize = _builder->GetSize(); + return retType; +} diff --git a/sources/flatbufserver/FlatBufferServer.cpp b/sources/flatbuffers/server/FlatBuffersServer.cpp similarity index 72% rename from sources/flatbufserver/FlatBufferServer.cpp rename to sources/flatbuffers/server/FlatBuffersServer.cpp index 7081339e9..924340f49 100644 --- a/sources/flatbufserver/FlatBufferServer.cpp +++ b/sources/flatbuffers/server/FlatBuffersServer.cpp @@ -1,6 +1,6 @@ -#include -#include "FlatBufferClient.h" #include "HyperhdrConfig.h" +#include +#include // util #include @@ -19,7 +19,7 @@ #define LUT_FILE_SIZE 50331648 -FlatBufferServer::FlatBufferServer(std::shared_ptr netOrigin, const QJsonDocument& config, const QString& configurationPath, QObject* parent) +FlatBuffersServer::FlatBuffersServer(std::shared_ptr netOrigin, const QJsonDocument& config, const QString& configurationPath, QObject* parent) : QObject(parent) , _server(new QTcpServer(this)) , _domain(new QLocalServer(this)) @@ -36,23 +36,23 @@ FlatBufferServer::FlatBufferServer(std::shared_ptr netOrigin, const Q { } -FlatBufferServer::~FlatBufferServer() +FlatBuffersServer::~FlatBuffersServer() { Debug(_log, "Prepare to shutdown"); stopServer(); - Debug(_log, "FlatBufferServer instance is closed"); + Debug(_log, "FlatBuffersServer instance is closed"); } -void FlatBufferServer::initServer() +void FlatBuffersServer::initServer() { if (_server != nullptr) - connect(_server, &QTcpServer::newConnection, this, &FlatBufferServer::handlerNewConnection); + connect(_server, &QTcpServer::newConnection, this, &FlatBuffersServer::handlerNewConnection); if (_domain != nullptr) - connect(_domain, &QLocalServer::newConnection, this, &FlatBufferServer::handlerNewConnection); + connect(_domain, &QLocalServer::newConnection, this, &FlatBuffersServer::handlerNewConnection); - connect(this, &FlatBufferServer::SignalImportFromProto, this, &FlatBufferServer::handlerImportFromProto); + connect(this, &FlatBuffersServer::SignalImportFromProto, this, &FlatBuffersServer::handlerImportFromProto); // apply config handleSettingsUpdate(settings::type::FLATBUFSERVER, _config); @@ -60,7 +60,7 @@ void FlatBufferServer::initServer() loadLutFile(); } -void FlatBufferServer::signalRequestSourceHandler(hyperhdr::Components component, int instanceIndex, bool listen) +void FlatBuffersServer::signalRequestSourceHandler(hyperhdr::Components component, int instanceIndex, bool listen) { if (component == hyperhdr::Components::COMP_HDR) { @@ -77,12 +77,12 @@ void FlatBufferServer::signalRequestSourceHandler(hyperhdr::Components component } } -int FlatBufferServer::getHdrToneMappingEnabled() +int FlatBuffersServer::getHdrToneMappingEnabled() { return _realHdrToneMappingMode; } -void FlatBufferServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config) +void FlatBuffersServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config) { if (type == settings::type::FLATBUFSERVER) { @@ -118,16 +118,16 @@ void FlatBufferServer::handleSettingsUpdate(settings::type type, const QJsonDocu } } -void FlatBufferServer::setupClient(FlatBufferClient* client) +void FlatBuffersServer::setupClient(FlatBuffersServerConnection* client) { - connect(client, &FlatBufferClient::SignalClientDisconnected, this, &FlatBufferServer::handlerClientDisconnected); - connect(client, &FlatBufferClient::SignalClearGlobalInput, GlobalSignals::getInstance(), &GlobalSignals::SignalClearGlobalInput); - connect(client, &FlatBufferClient::SignalImageReceived, this, &FlatBufferServer::handlerImageReceived); - connect(client, &FlatBufferClient::SignalSetGlobalColor, GlobalSignals::getInstance(), &GlobalSignals::SignalSetGlobalColor); + connect(client, &FlatBuffersServerConnection::SignalClientDisconnected, this, &FlatBuffersServer::handlerClientDisconnected); + connect(client, &FlatBuffersServerConnection::SignalClearGlobalInput, GlobalSignals::getInstance(), &GlobalSignals::SignalClearGlobalInput); + connect(client, &FlatBuffersServerConnection::SignalImageReceived, this, &FlatBuffersServer::handlerImageReceived); + connect(client, &FlatBuffersServerConnection::SignalSetGlobalColor, GlobalSignals::getInstance(), &GlobalSignals::SignalSetGlobalColor); _openConnections.append(client); } -void FlatBufferServer::handlerNewConnection() +void FlatBuffersServer::handlerNewConnection() { while (_server != nullptr && _server->hasPendingConnections()) { @@ -136,9 +136,16 @@ void FlatBufferServer::handlerNewConnection() if (_netOrigin->accessAllowed(socket->peerAddress(), socket->localAddress())) { Debug(_log, "New connection from %s", QSTRING_CSTR(socket->peerAddress().toString())); - FlatBufferClient* client = new FlatBufferClient(socket, nullptr, _timeout, this); - // internal - setupClient(client); + try + { + FlatBuffersServerConnection* client = new FlatBuffersServerConnection(socket, nullptr, _timeout, this); + // internal + setupClient(client); + } + catch (std::exception& ex) + { + Error(_log, "Could not initialize server client: %s", ex.what()); + } } else socket->close(); @@ -149,14 +156,21 @@ void FlatBufferServer::handlerNewConnection() if (QLocalSocket* socket = _domain->nextPendingConnection()) { Debug(_log, "New local domain connection"); - FlatBufferClient* client = new FlatBufferClient(nullptr, socket, _timeout, this); - // internal - setupClient(client); + try + { + FlatBuffersServerConnection* client = new FlatBuffersServerConnection(nullptr, socket, _timeout, this); + // internal + setupClient(client); + } + catch (std::exception& ex) + { + Error(_log, "Could not initialize server client: %s", ex.what()); + } } } } -void FlatBufferServer::handlerClientDisconnected(FlatBufferClient* client) +void FlatBuffersServer::handlerClientDisconnected(FlatBuffersServerConnection* client) { if (client != nullptr) { @@ -165,7 +179,7 @@ void FlatBufferServer::handlerClientDisconnected(FlatBufferClient* client) } } -void FlatBufferServer::startServer() +void FlatBuffersServer::startServer() { if (_server != nullptr && !_server->isListening()) { @@ -187,11 +201,11 @@ void FlatBufferServer::startServer() } } -void FlatBufferServer::stopServer() +void FlatBuffersServer::stopServer() { if ((_server != nullptr &&_server->isListening()) || (_domain != nullptr && _domain->isListening())) { - QVectorIterator i(_openConnections); + QVectorIterator i(_openConnections); while (i.hasNext()) { const auto& client = i.next(); @@ -209,7 +223,7 @@ void FlatBufferServer::stopServer() } } -QString FlatBufferServer::GetSharedLut() +QString FlatBuffersServer::GetSharedLut() { #ifdef __APPLE__ QString ret = QString("%1%2").arg(QCoreApplication::applicationDirPath()).arg("/../lut"); @@ -223,7 +237,7 @@ QString FlatBufferServer::GetSharedLut() // copied from Grabber::loadLutFile() // color should always be RGB24 for flatbuffers -void FlatBufferServer::loadLutFile() +void FlatBuffersServer::loadLutFile() { QString fileName01 = QString("%1%2").arg(_configurationPath).arg("/flat_lut_lin_tables.3d"); QString fileName02 = QString("%1%2").arg(GetSharedLut()).arg("/flat_lut_lin_tables.3d"); @@ -293,7 +307,7 @@ void FlatBufferServer::loadLutFile() } } -void FlatBufferServer::handlerImportFromProto(int priority, int duration, const Image& image, QString clientDescription) +void FlatBuffersServer::handlerImportFromProto(int priority, int duration, const Image& image, QString clientDescription) { if (getHdrToneMappingEnabled()) FrameDecoder::applyLUT((uint8_t*)image.rawMem(), image.width(), image.height(), _lut.data(), getHdrToneMappingEnabled()); @@ -301,7 +315,7 @@ void FlatBufferServer::handlerImportFromProto(int priority, int duration, const emit GlobalSignals::getInstance()->SignalSetGlobalImage(priority, image, duration, hyperhdr::Components::COMP_PROTOSERVER, clientDescription); } -void FlatBufferServer::handlerImageReceived(int priority, const Image& image, int timeout_ms, hyperhdr::Components origin, QString clientDescription) +void FlatBuffersServer::handlerImageReceived(int priority, const Image& image, int timeout_ms, hyperhdr::Components origin, QString clientDescription) { if (getHdrToneMappingEnabled()) FrameDecoder::applyLUT((uint8_t*)image.rawMem(), image.width(), image.height(), _lut.data(), getHdrToneMappingEnabled()); diff --git a/sources/flatbuffers/server/FlatBuffersServerConnection.cpp b/sources/flatbuffers/server/FlatBuffersServerConnection.cpp new file mode 100644 index 000000000..3212d3789 --- /dev/null +++ b/sources/flatbuffers/server/FlatBuffersServerConnection.cpp @@ -0,0 +1,272 @@ +/* FlatBuffersServerConnection.cpp +* +* MIT License +* +* Copyright (c) 2020-2024 awawa-dev +* +* Project homesite: https://github.com/awawa-dev/HyperHDR +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + + +#include +#include +using namespace FlatBuffersParser; + +// qt +#include +#include +#include +#include + +// util includes +#include + +FlatBuffersServerConnection::FlatBuffersServerConnection(QTcpSocket* socket, QLocalSocket* domain, int timeout, QObject* parent) + : QObject(parent) + , _log(Logger::getInstance("FLATBUFSERVER")) + , _socket(socket) + , _domain(domain) + , _clientAddress("@LocalSocket") + , _timeoutTimer(new QTimer(this)) + , _timeout(timeout * 1000) + , _priority(140) + , _mode(0) + , _builder(nullptr) + , _incomingSize(0) + , _incomingIndex(0) +{ + if (!initParserLibrary() || _builder == nullptr) + { + throw std::runtime_error("Could not initialize Flatbuffers parser"); + } + + if (_socket != nullptr) + _clientAddress = "@" + _socket->peerAddress().toString(); + + // timer setup + _timeoutTimer->setSingleShot(true); + _timeoutTimer->setInterval(_timeout); + connect(_timeoutTimer, &QTimer::timeout, this, [&]() { + Warning(_log, "Timeout (%i) has been reached. Disconnecting", _timeoutTimer->interval()); + forceClose(); + }); + + // connect socket signals + if (_socket != nullptr) + { + connect(_socket, &QTcpSocket::readyRead, this, &FlatBuffersServerConnection::readyRead); + connect(_socket, &QTcpSocket::disconnected, this, &FlatBuffersServerConnection::disconnected); + } + else if (_domain != nullptr) + { + connect(_domain, &QLocalSocket::readyRead, this, &FlatBuffersServerConnection::readyRead); + connect(_domain, &QLocalSocket::disconnected, this, &FlatBuffersServerConnection::disconnected); + } +} + +FlatBuffersServerConnection::~FlatBuffersServerConnection() +{ + if (_socket != nullptr) + _socket->deleteLater(); + if (_domain != nullptr) + _domain->deleteLater(); + if (_builder != nullptr) + releaseFlatbuffersBuilder(_builder); +} + +bool FlatBuffersServerConnection::initParserLibrary() +{ + _builder = createFlatbuffersBuilder(); + return (_builder != nullptr); +} + +void FlatBuffersServerConnection::readyRead() +{ + _timeoutTimer->start(); + + while ((_socket != nullptr && _socket->bytesAvailable()) || (_domain != nullptr && _domain->bytesAvailable())) + { + + if (_incomingSize == 0) + { + QByteArray sizeArr; + if (_socket != nullptr && _socket->bytesAvailable() >= 4) + sizeArr = _socket->read(4); + else if (_domain != nullptr && _domain->bytesAvailable() >= 4) + sizeArr = _domain->read(4); + else + return; + + _incomingSize = + ((sizeArr[0] << 24) & 0xFF000000) | + ((sizeArr[1] << 16) & 0x00FF0000) | + ((sizeArr[2] << 8) & 0x0000FF00) | + ((sizeArr[3]) & 0x000000FF); + + if (_incomingSize > 10000000 || _incomingSize == 0) + { + Error(_log, "The frame is too large (> 10000000) or too small (0), has %i byte", _incomingSize); + _incomingSize = 0; + _incomingIndex = 0; + QUEUE_CALL_0(this, disconnected); + return; + } + + _incomingIndex = 0; + _incommingBuffer.resize(_incomingSize); + } + + if (_socket != nullptr) + _incomingIndex += _socket->read(reinterpret_cast(_incommingBuffer.data() + _incomingIndex), static_cast(_incomingSize) - _incomingIndex); + else if (_domain != nullptr) + _incomingIndex += _domain->read(reinterpret_cast(_incommingBuffer.data() + _incomingIndex), static_cast(_incomingSize) - _incomingIndex); + + + // check if we can read a header + if (_incomingSize == _incomingIndex) + { + uint8_t red = 0, green = 0, blue = 0; + int priority = 0, duration = 0, imageWidth = 0, imageHeight = 0; + std::string clientDescription; + uint8_t* imageData = nullptr; + uint8_t* buffer = nullptr; + size_t imageSize = 0, bufferSize = 0; + + auto result = decodeIncomingFlatbuffersFrame(_builder, _incommingBuffer.data(), _incomingSize, + &red, &green, &blue, + &priority, &clientDescription, &duration, + &imageData, &imageWidth, &imageHeight, &imageSize, + &buffer, &bufferSize); + + if (result == FLATBUFFERS_PACKAGE_TYPE::COLOR) + { + emit SignalClearGlobalInput(_priority, false); + + if (duration > 0) + { + _timeoutTimer->setInterval(duration); + _timeoutTimer->start(); + } + else + _timeoutTimer->stop(); + + + _mode = result; + std::vector color{ ColorRgb(red, green, blue) }; + if (_clientDescription.isEmpty()) + _clientDescription = QString("Forwarder%1").arg(_clientAddress); + emit SignalSetGlobalColor(_priority, color, duration, hyperhdr::Components::COMP_FLATBUFSERVER, QString(_clientDescription)); + + } + if (result == FLATBUFFERS_PACKAGE_TYPE::IMAGE) + { + if (_mode == FLATBUFFERS_PACKAGE_TYPE::COLOR) + { + emit SignalClearGlobalInput(_priority, false); + _timeoutTimer->setInterval(_timeout); + _timeoutTimer->start(); + } + + _mode = result; + + if ((int)imageSize != imageWidth * imageHeight * 3) + { + Error(_log, "Size of image data does not match with the width and height"); + } + else + { + Image imageDest(imageWidth, imageHeight); + memmove(imageDest.rawMem(), imageData, imageSize); + + emit SignalImageReceived(_priority, imageDest, duration, hyperhdr::Components::COMP_FLATBUFSERVER, _clientDescription); + } + } + if (result == FLATBUFFERS_PACKAGE_TYPE::PRIORITY) + { + if (priority < 50 || priority > 250) + { + Error(_log, "Register request from client %s contains invalid priority %d. Valid priority for Flatbuffer connections is between 50 and 250.", QSTRING_CSTR(_clientAddress), priority); + } + else + { + _priority = priority; + _clientDescription = QString("%1%2").arg(QString::fromStdString(clientDescription)).arg(_clientAddress); + } + } + if (result == FLATBUFFERS_PACKAGE_TYPE::CLEAR) + { + emit SignalClearGlobalInput(priority, false); + } + if (result == FLATBUFFERS_PACKAGE_TYPE::ERROR) + { + Error(_log, "An flatbuffers error has occured"); + } + + if (buffer != nullptr && bufferSize > 0) + { + sendMessage(buffer, bufferSize); + } + + clearFlatbuffersBuilder(_builder); + + _incomingSize = _incomingIndex = 0; + } + } +} + +void FlatBuffersServerConnection::forceClose() +{ + if (_socket != nullptr) + _socket->close(); + if (_domain != nullptr) + _domain->close(); +} + +void FlatBuffersServerConnection::disconnected() +{ + Debug(_log, "Socket Closed"); + + if (_priority >= 50 && _priority <= 250) + emit SignalClearGlobalInput(_priority, false); + + emit SignalClientDisconnected(this); +} + + + +void FlatBuffersServerConnection::sendMessage(uint8_t* buffer, size_t size) +{ + uint8_t sizeData[] = { uint8_t(size >> 24), uint8_t(size >> 16), uint8_t(size >> 8), uint8_t(size) }; + + if (_socket != nullptr) + { + _socket->write((const char*)sizeData, sizeof(sizeData)); + _socket->write((const char*)buffer, size); + _socket->flush(); + } + else if (_domain != nullptr) + { + _domain->write((const char*)sizeData, sizeof(sizeData)); + _domain->write((const char*)buffer, size); + _domain->flush(); + } +} + diff --git a/sources/flatbufserver/CMakeLists.txt b/sources/flatbufserver/CMakeLists.txt deleted file mode 100644 index e715a1391..000000000 --- a/sources/flatbufserver/CMakeLists.txt +++ /dev/null @@ -1,45 +0,0 @@ - -# Define the current source locations -set(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/flatbufserver) -set(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sources/flatbufserver) - -include_directories( - ${CMAKE_CURRENT_BINARY_DIR} - ${FLATBUFFERS_INCLUDE_DIRS} -) - -FILE ( GLOB FLATBUFSERVER_SOURCES "${CURRENT_HEADER_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.cpp" ) - -set(Flatbuffer_GENERATED_FBS - hyperhdr_reply_generated.h - hyperhdr_request_generated.h -) - -set(Flatbuffer_FBS - ${CURRENT_SOURCE_DIR}/hyperhdr_reply.fbs - ${CURRENT_SOURCE_DIR}/hyperhdr_request.fbs -) -FOREACH(FBS_FILE ${Flatbuffer_FBS}) - compile_flattbuffer_schema(${FBS_FILE} ${CMAKE_CURRENT_SOURCE_DIR}) -ENDFOREACH(FBS_FILE) - -# let cmake know about new generated source files -set_source_files_properties( - ${Flatbuffer_GENERATED_FBS} PROPERTIES GENERATED TRUE -) - -add_library(flatbufserver - ${FLATBUFSERVER_SOURCES} - ${Flatbuffer_GENERATED_FBS} -) - -target_link_libraries(flatbufserver - hyperhdr-utils - flatbuffers - Qt${Qt_VERSION}::Network - Qt${Qt_VERSION}::Core -) - -if(USE_PRECOMPILED_HEADERS AND COMMAND target_precompile_headers) - target_precompile_headers(flatbufserver REUSE_FROM precompiled_hyperhdr_headers) -endif() diff --git a/sources/flatbufserver/FlatBufferClient.cpp b/sources/flatbufserver/FlatBufferClient.cpp deleted file mode 100644 index a5f6ed572..000000000 --- a/sources/flatbufserver/FlatBufferClient.cpp +++ /dev/null @@ -1,244 +0,0 @@ -#include "FlatBufferClient.h" - -// qt -#include -#include -#include -#include - -// util includes -#include - -FlatBufferClient::FlatBufferClient(QTcpSocket* socket, QLocalSocket* domain, int timeout, QObject* parent) - : QObject(parent) - , _log(Logger::getInstance("FLATBUFSERVER")) - , _socket(socket) - , _domain(domain) - , _clientAddress("@LocalSocket") - , _timeoutTimer(new QTimer(this)) - , _timeout(timeout * 1000) - , _priority(140) -{ - if (_socket != nullptr) - _clientAddress = "@" + _socket->peerAddress().toString(); - - // timer setup - _timeoutTimer->setSingleShot(true); - _timeoutTimer->setInterval(_timeout); - connect(_timeoutTimer, &QTimer::timeout, this, &FlatBufferClient::forceClose); - - // connect socket signals - if (_socket != nullptr) - { - connect(_socket, &QTcpSocket::readyRead, this, &FlatBufferClient::readyRead); - connect(_socket, &QTcpSocket::disconnected, this, &FlatBufferClient::disconnected); - } - else if (_domain != nullptr) - { - connect(_domain, &QLocalSocket::readyRead, this, &FlatBufferClient::readyRead); - connect(_domain, &QLocalSocket::disconnected, this, &FlatBufferClient::disconnected); - } -} - -void FlatBufferClient::readyRead() -{ - _timeoutTimer->start(); - - if (_socket != nullptr) - _receiveBuffer += _socket->readAll(); - else if (_domain != nullptr) - _receiveBuffer += _domain->readAll(); - - // check if we can read a header - while (_receiveBuffer.size() >= 4) - { - uint32_t messageSize = - ((_receiveBuffer[0] << 24) & 0xFF000000) | - ((_receiveBuffer[1] << 16) & 0x00FF0000) | - ((_receiveBuffer[2] << 8) & 0x0000FF00) | - ((_receiveBuffer[3]) & 0x000000FF); - - // check if we can read a complete message - if ((uint32_t)_receiveBuffer.size() < messageSize + 4) return; - - // extract message only and remove header + msg from buffer :: QByteArray::remove() does not return the removed data - const QByteArray msg = _receiveBuffer.mid(4, messageSize); - _receiveBuffer.remove(0, messageSize + 4); - - const auto* msgData = reinterpret_cast(msg.constData()); - flatbuffers::Verifier verifier(msgData, messageSize); - - if (hyperhdrnet::VerifyRequestBuffer(verifier)) - { - auto message = hyperhdrnet::GetRequest(msgData); - handleMessage(message); - continue; - } - sendErrorReply("Unable to parse message"); - } -} - -void FlatBufferClient::forceClose() -{ - if (_socket != nullptr) - _socket->close(); - if (_domain != nullptr) - _domain->close(); -} - -void FlatBufferClient::disconnected() -{ - Debug(_log, "Socket Closed"); - - if (_socket != nullptr) - _socket->deleteLater(); - if (_domain != nullptr) - _domain->deleteLater(); - - if (_priority >= 50 && _priority <= 250) - emit SignalClearGlobalInput(_priority, false); - - emit SignalClientDisconnected(this); -} - -void FlatBufferClient::handleMessage(const hyperhdrnet::Request* req) -{ - const void* reqPtr; - if ((reqPtr = req->command_as_Color()) != nullptr) { - handleColorCommand(static_cast(reqPtr)); - } - else if ((reqPtr = req->command_as_Image()) != nullptr) { - handleImageCommand(static_cast(reqPtr)); - } - else if ((reqPtr = req->command_as_Clear()) != nullptr) { - handleClearCommand(static_cast(reqPtr)); - } - else if ((reqPtr = req->command_as_Register()) != nullptr) { - handleRegisterCommand(static_cast(reqPtr)); - } - else { - sendErrorReply("Received invalid packet."); - } -} - -void FlatBufferClient::handleColorCommand(const hyperhdrnet::Color* colorReq) -{ - // extract parameters - const int32_t rgbData = colorReq->data(); - std::vector color{ ColorRgb{ uint8_t((rgbData >> 16) & 0xff), uint8_t((rgbData >> 8) & 0xff), uint8_t(rgbData & 0xff) } }; - - // set output - emit SignalSetGlobalColor(_priority, color, colorReq->duration(), hyperhdr::Components::COMP_FLATBUFSERVER, _clientDescription); - - // send reply - sendSuccessReply(); -} - -void FlatBufferClient::handleRegisterCommand(const hyperhdrnet::Register* regReq) -{ - if (regReq->priority() < 50 || regReq->priority() > 250) - { - Error(_log, "Register request from client %s contains invalid priority %d. Valid priority for Flatbuffer connections is between 50 and 250.", QSTRING_CSTR(_clientAddress), regReq->priority()); - sendErrorReply("The priority " + std::to_string(regReq->priority()) + " is not in the priority range between 50 and 250."); - return; - } - - _priority = regReq->priority(); - _clientDescription = regReq->origin()->c_str() + _clientAddress; - - auto reply = hyperhdrnet::CreateReplyDirect(_builder, nullptr, -1, _priority); - _builder.Finish(reply); - - // send reply - sendMessage(); - - _builder.Clear(); -} - -void FlatBufferClient::handleImageCommand(const hyperhdrnet::Image* image) -{ - // extract parameters - int duration = image->duration(); - - const void* reqPtr; - if ((reqPtr = image->data_as_RawImage()) != nullptr) - { - const auto* img = static_cast(reqPtr); - const auto& imageData = img->data(); - const int width = img->width(); - const int height = img->height(); - - if ((int)imageData->size() != width * height * 3) - { - sendErrorReply("Size of image data does not match with the width and height"); - return; - } - - Image imageDest(width, height); - memmove(imageDest.rawMem(), imageData->data(), imageData->size()); - - emit SignalImageReceived(_priority, imageDest, duration, hyperhdr::Components::COMP_FLATBUFSERVER, _clientDescription); - } - - // send reply - sendSuccessReply(); -} - - -void FlatBufferClient::handleClearCommand(const hyperhdrnet::Clear* clear) -{ - // extract parameters - const int priority = clear->priority(); - - emit SignalClearGlobalInput(priority, false); - - sendSuccessReply(); -} - -void FlatBufferClient::handleNotImplemented() -{ - sendErrorReply("Command not implemented"); -} - -void FlatBufferClient::sendMessage() -{ - auto size = _builder.GetSize(); - const uint8_t* buffer = _builder.GetBufferPointer(); - uint8_t sizeData[] = { uint8_t(size >> 24), uint8_t(size >> 16), uint8_t(size >> 8), uint8_t(size) }; - - if (_socket != nullptr) - { - _socket->write((const char*)sizeData, sizeof(sizeData)); - _socket->write((const char*)buffer, size); - _socket->flush(); - } - else if (_domain != nullptr) - { - _domain->write((const char*)sizeData, sizeof(sizeData)); - _domain->write((const char*)buffer, size); - _domain->flush(); - } -} - -void FlatBufferClient::sendSuccessReply() -{ - auto reply = hyperhdrnet::CreateReplyDirect(_builder); - _builder.Finish(reply); - - // send reply - sendMessage(); - - _builder.Clear(); -} - -void FlatBufferClient::sendErrorReply(const std::string& error) -{ - // create reply - auto reply = hyperhdrnet::CreateReplyDirect(_builder, error.c_str()); - _builder.Finish(reply); - - // send reply - sendMessage(); - - _builder.Clear(); -} diff --git a/sources/flatbufserver/FlatBufferClient.h b/sources/flatbufserver/FlatBufferClient.h deleted file mode 100644 index 47773359d..000000000 --- a/sources/flatbufserver/FlatBufferClient.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -// util -#include -#include -#include -#include - -// flatbuffer FBS -#include "hyperhdr_reply_generated.h" -#include "hyperhdr_request_generated.h" - -class QTcpSocket; -class QLocalSocket; -class QTimer; - -class FlatBufferClient : public QObject -{ - Q_OBJECT - -public: - explicit FlatBufferClient(QTcpSocket* socket, QLocalSocket* domain, int timeout, QObject* parent = nullptr); - -signals: - void SignalClearGlobalInput(int priority, bool forceClearAll); - void SignalImageReceived(int priority, const Image& image, int timeout_ms, hyperhdr::Components origin, QString clientDescription); - void SignalSetGlobalColor(int priority, const std::vector& ledColor, int timeout_ms, hyperhdr::Components origin, QString clientDescription); - void SignalClientDisconnected(FlatBufferClient* client); - -public slots: - void forceClose(); - -private slots: - void readyRead(); - void disconnected(); - -private: - void handleMessage(const hyperhdrnet::Request* req); - void handleRegisterCommand(const hyperhdrnet::Register* regReq); - void handleColorCommand(const hyperhdrnet::Color* colorReq); - void handleImageCommand(const hyperhdrnet::Image* image); - void handleClearCommand(const hyperhdrnet::Clear* clear); - void handleNotImplemented(); - void sendMessage(); - void sendSuccessReply(); - void sendErrorReply(const std::string& error); - -private: - Logger* _log; - QTcpSocket* _socket; - QLocalSocket* _domain; - QString _clientAddress; - QTimer* _timeoutTimer; - int _timeout; - int _priority; - QString _clientDescription; - - QByteArray _receiveBuffer; - - flatbuffers::FlatBufferBuilder _builder; -}; diff --git a/sources/hyperhdr/CMakeLists.txt b/sources/hyperhdr/CMakeLists.txt index 96befda39..5c56d9d8f 100644 --- a/sources/hyperhdr/CMakeLists.txt +++ b/sources/hyperhdr/CMakeLists.txt @@ -24,7 +24,8 @@ target_link_libraries(hyperhdr hyperimage effectengine jsonserver - flatbufserver + flatbuffers_server + flatbuffers_client webserver ssdp database diff --git a/sources/hyperhdr/HyperHdrDaemon.cpp b/sources/hyperhdr/HyperHdrDaemon.cpp index 709bc4994..52a5d004e 100644 --- a/sources/hyperhdr/HyperHdrDaemon.cpp +++ b/sources/hyperhdr/HyperHdrDaemon.cpp @@ -32,7 +32,7 @@ #include // Flatbuffer Server -#include +#include // ProtoNanoBuffer Server #include @@ -318,18 +318,18 @@ void HyperHdrDaemon::startNetworkServices() _flatProtoThread->setObjectName("FlatProtoThread"); - FlatBufferServer* _flatBufferServer = new FlatBufferServer(_netOrigin, getSetting(settings::type::FLATBUFSERVER), _rootPath); + FlatBuffersServer* _flatBufferServer = new FlatBuffersServer(_netOrigin, getSetting(settings::type::FLATBUFSERVER), _rootPath); _flatBufferServer->moveToThread(_flatProtoThread); flatProtoThreadClients.push_back(_flatBufferServer); if (_videoGrabber == nullptr) { Warning(_log, "The USB grabber was disabled during build. FlatbufferServer now controlls the HDR state."); - connect(_flatBufferServer, &FlatBufferServer::SignalSetNewComponentStateToAllInstances, _instanceManager.get(), &HyperHdrManager::SignalSetNewComponentStateToAllInstances); + connect(_flatBufferServer, &FlatBuffersServer::SignalSetNewComponentStateToAllInstances, _instanceManager.get(), &HyperHdrManager::SignalSetNewComponentStateToAllInstances); } - connect(GlobalSignals::getInstance(), &GlobalSignals::SignalRequestComponent, _flatBufferServer, &FlatBufferServer::signalRequestSourceHandler); - connect(_flatProtoThread, &QThread::started, _flatBufferServer, &FlatBufferServer::initServer); - connect(_flatProtoThread, &QThread::finished, _flatBufferServer, &FlatBufferServer::deleteLater); - connect(_instanceManager.get(), &HyperHdrManager::SignalSettingsChanged, _flatBufferServer, &FlatBufferServer::handleSettingsUpdate); + connect(GlobalSignals::getInstance(), &GlobalSignals::SignalRequestComponent, _flatBufferServer, &FlatBuffersServer::signalRequestSourceHandler); + connect(_flatProtoThread, &QThread::started, _flatBufferServer, &FlatBuffersServer::initServer); + connect(_flatProtoThread, &QThread::finished, _flatBufferServer, &FlatBuffersServer::deleteLater); + connect(_instanceManager.get(), &HyperHdrManager::SignalSettingsChanged, _flatBufferServer, &FlatBuffersServer::handleSettingsUpdate); NetworkForwarder* _networkForwarder = new NetworkForwarder(); _networkForwarder->moveToThread(_flatProtoThread); @@ -343,7 +343,7 @@ void HyperHdrDaemon::startNetworkServices() flatProtoThreadClients.push_back(_protoServer); connect(_flatProtoThread, &QThread::started, _protoServer, &ProtoServer::initServer); connect(_flatProtoThread, &QThread::finished, _protoServer, &ProtoServer::deleteLater); - connect(_protoServer, &ProtoServer::SignalImportFromProto, _flatBufferServer, &FlatBufferServer::SignalImportFromProto); + connect(_protoServer, &ProtoServer::SignalImportFromProto, _flatBufferServer, &FlatBuffersServer::SignalImportFromProto); connect(_instanceManager.get(), &HyperHdrManager::SignalSettingsChanged, _protoServer, &ProtoServer::handleSettingsUpdate); #endif diff --git a/sources/proto-nano-server/ProtoNanoClientConnection.cpp b/sources/proto-nano-server/ProtoNanoClientConnection.cpp index bd21540ff..df763b79f 100644 --- a/sources/proto-nano-server/ProtoNanoClientConnection.cpp +++ b/sources/proto-nano-server/ProtoNanoClientConnection.cpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include ProtoNanoClientConnection::ProtoNanoClientConnection(QTcpSocket* socket, int timeout, QObject* parent) : QObject(parent) diff --git a/www/js/remote.js b/www/js/remote.js index 78e2ec5c1..6822650aa 100644 --- a/www/js/remote.js +++ b/www/js/remote.js @@ -261,7 +261,7 @@ $(document).ready(function() for (const comp of components) { - if (comp.name === "ALL") + if (comp.name === "ALL" || (comp.name === "FORWARDER" && window.serverInfo.currentInstance != 0)) continue; const comp_btn_id = "comp_btn_" + comp.name;