Skip to content

Commit

Permalink
Refactoring of the FlatBuffers client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jun 6, 2024
1 parent 3003f8f commit 358b000
Show file tree
Hide file tree
Showing 29 changed files with 924 additions and 526 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
1 change: 1 addition & 0 deletions include/base/HyperHdrInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public slots:
void SignalSmoothingRestarted(int suggestedInterval);
void SignalRawColorsChanged(const std::vector<ColorRgb>& ledValues);
void SignalInstanceJustStarted();
void SignalColorIsSet(ColorRgb color, int duration);

private slots:
void handleVisibleComponentChanged(hyperhdr::Components comp);
Expand Down
5 changes: 3 additions & 2 deletions include/base/NetworkForwarder.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// Forward declaration
class HyperHdrInstance;
class QTcpSocket;
class FlatBufferConnection;
class FlatBuffersClient;

class NetworkForwarder : public QObject
{
Expand All @@ -41,6 +41,7 @@ class NetworkForwarder : public QObject

public slots:
void startedHandler();
void signalColorIsSetHandler(ColorRgb color, int duration);
void signalForwardImageHandler();
void handlerInstanceImageUpdated(const Image<ColorRgb>& ret);

Expand All @@ -60,7 +61,7 @@ private slots:

const int _priority;

QList<FlatBufferConnection*> _forwardClients;
QList<FlatBuffersClient*> _forwardClients;
std::atomic<bool> _hasImage;
Image<ColorRgb> _image;
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,16 @@
#include <utils/ColorRgb.h>
#include <utils/Logger.h>

#include <flatbuffers/flatbuffers.h>

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);
Expand All @@ -38,16 +31,18 @@ class FlatBufferConnection : public QObject

public slots:
void sendImage(const Image<ColorRgb>& image);
void setColorHandler(ColorRgb color, int duration);

private slots:
void connectToHost();
void readData();

signals:
void SignalImageToSend(const Image<ColorRgb>& image);
void SignalSetColor(ColorRgb color, int duration);

private:
bool parseReply(const hyperhdrnet::Reply* reply);
bool initParserLibrary();

QTcpSocket* _socket;
QLocalSocket* _domain;
Expand All @@ -62,7 +57,7 @@ private slots:
QLocalSocket::LocalSocketState _prevLocalState;

Logger* _log;
flatbuffers::FlatBufferBuilder _builder;
void* _builder;

bool _registered;
bool _sent;
Expand Down
50 changes: 50 additions & 0 deletions include/flatbuffers/parser/FlatBuffersParser.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>
#include <string>

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);
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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> netOrigin, const QJsonDocument& config, const QString& configurationPath, QObject* parent = nullptr);
~FlatBufferServer() override;
FlatBuffersServer(std::shared_ptr<NetOrigin> netOrigin, const QJsonDocument& config, const QString& configurationPath, QObject* parent = nullptr);
~FlatBuffersServer() override;

signals:
void SignalSetNewComponentStateToAllInstances(hyperhdr::Components component, bool enable);
Expand All @@ -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;
Expand All @@ -58,7 +58,7 @@ private slots:

const QJsonDocument _config;
BonjourServiceRegister* _serviceRegister = nullptr;
QVector<FlatBufferClient*> _openConnections;
QVector<FlatBuffersServerConnection*> _openConnections;

int _hdrToneMappingMode;
int _realHdrToneMappingMode;
Expand Down
81 changes: 81 additions & 0 deletions include/flatbuffers/server/FlatBuffersServerConnection.h
Original file line number Diff line number Diff line change
@@ -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 <utils/Logger.h>
#include <utils/Image.h>
#include <utils/ColorRgb.h>
#include <utils/Components.h>
#include <utils/MemoryBuffer.h>

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<ColorRgb>& image, int timeout_ms, hyperhdr::Components origin, QString clientDescription);
void SignalSetGlobalColor(int priority, const std::vector<ColorRgb>& 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<uint8_t> _incommingBuffer;
};
4 changes: 0 additions & 4 deletions include/utils/Image.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#pragma once

#ifndef PCH_ENABLED
#include <QExplicitlySharedDataPointer>
#endif

#include <utils/ImageData.h>

template <typename ColorSpace>
Expand Down
2 changes: 1 addition & 1 deletion sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sources/api/BaseAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <base/SystemWrapper.h>
#include <base/Muxer.h>
#include <db/AuthTable.h>
#include <flatbufserver/FlatBufferServer.h>
#include <flatbuffers/server/FlatBuffersServer.h>
#include <utils/jsonschema/QJsonSchemaChecker.h>
#include <utils/GlobalSignals.h>
#include <base/GrabberHelper.h>
Expand Down
2 changes: 1 addition & 1 deletion sources/api/CallbackAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <base/ComponentController.h>
#include <base/Muxer.h>
#include <base/ImageToLedManager.h>
#include <flatbufserver/FlatBufferServer.h>
#include <flatbuffers/server/FlatBuffersServer.h>
#include <utils/PerformanceCounters.h>

using namespace hyperhdr;
Expand Down
2 changes: 1 addition & 1 deletion sources/api/HyperAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <base/SoundCapture.h>
#include <base/ImageToLedManager.h>
#include <base/AccessManager.h>
#include <flatbufserver/FlatBufferServer.h>
#include <flatbuffers/server/FlatBuffersServer.h>
#include <utils/jsonschema/QJsonUtils.h>
#include <utils/jsonschema/QJsonSchemaChecker.h>
#include <utils/JsonUtils.h>
Expand Down
7 changes: 2 additions & 5 deletions sources/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -18,7 +14,8 @@ add_library(hyperhdr-base
target_link_libraries(hyperhdr-base
blackborder
hyperhdr-utils
flatbufserver
flatbuffers_server
flatbuffers_client
flatbuffers
leddevice
effectengine
Expand Down
5 changes: 4 additions & 1 deletion sources/base/HyperHdrInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,10 @@ void HyperHdrInstance::setColor(int priority, const std::vector<ColorRgb>& 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);
Expand Down
Loading

0 comments on commit 358b000

Please sign in to comment.