FlatBufferServer.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <flatbufserver/FlatBufferServer.h>
  2. #include "FlatBufferClient.h"
  3. #include "HyperionConfig.h"
  4. // util
  5. #include <utils/NetOrigin.h>
  6. #include <utils/GlobalSignals.h>
  7. // qt
  8. #include <QJsonObject>
  9. #include <QTcpServer>
  10. #include <QTcpSocket>
  11. // Constants
  12. namespace {
  13. const char SERVICE_TYPE[] = "flatbuffer";
  14. } //End of constants
  15. FlatBufferServer::FlatBufferServer(const QJsonDocument& config, QObject* parent)
  16. : QObject(parent)
  17. , _server(new QTcpServer(this))
  18. , _log(Logger::getInstance("FLATBUFSERVER"))
  19. , _timeout(5000)
  20. , _config(config)
  21. {
  22. }
  23. FlatBufferServer::~FlatBufferServer()
  24. {
  25. stopServer();
  26. delete _server;
  27. }
  28. void FlatBufferServer::initServer()
  29. {
  30. _netOrigin = NetOrigin::getInstance();
  31. connect(_server, &QTcpServer::newConnection, this, &FlatBufferServer::newConnection);
  32. // apply config
  33. handleSettingsUpdate(settings::FLATBUFSERVER, _config);
  34. }
  35. void FlatBufferServer::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
  36. {
  37. if(type == settings::FLATBUFSERVER)
  38. {
  39. const QJsonObject& obj = config.object();
  40. quint16 port = obj["port"].toInt(19400);
  41. // port check
  42. if(_server->serverPort() != port)
  43. {
  44. stopServer();
  45. _port = port;
  46. }
  47. // new timeout just for new connections
  48. _timeout = obj["timeout"].toInt(5000);
  49. // enable check
  50. obj["enable"].toBool(true) ? startServer() : stopServer();
  51. }
  52. }
  53. void FlatBufferServer::newConnection()
  54. {
  55. while(_server->hasPendingConnections())
  56. {
  57. if(QTcpSocket* socket = _server->nextPendingConnection())
  58. {
  59. if(_netOrigin->accessAllowed(socket->peerAddress(), socket->localAddress()))
  60. {
  61. Debug(_log, "New connection from %s", QSTRING_CSTR(socket->peerAddress().toString()));
  62. FlatBufferClient *client = new FlatBufferClient(socket, _timeout, this);
  63. // internal
  64. connect(client, &FlatBufferClient::clientDisconnected, this, &FlatBufferServer::clientDisconnected);
  65. connect(client, &FlatBufferClient::registerGlobalInput, GlobalSignals::getInstance(), &GlobalSignals::registerGlobalInput);
  66. connect(client, &FlatBufferClient::clearGlobalInput, GlobalSignals::getInstance(), &GlobalSignals::clearGlobalInput);
  67. connect(client, &FlatBufferClient::setGlobalInputImage, GlobalSignals::getInstance(), &GlobalSignals::setGlobalImage);
  68. connect(client, &FlatBufferClient::setGlobalInputColor, GlobalSignals::getInstance(), &GlobalSignals::setGlobalColor);
  69. connect(client, &FlatBufferClient::setBufferImage, GlobalSignals::getInstance(), &GlobalSignals::setBufferImage);
  70. connect(GlobalSignals::getInstance(), &GlobalSignals::globalRegRequired, client, &FlatBufferClient::registationRequired);
  71. _openConnections.append(client);
  72. }
  73. else
  74. socket->close();
  75. }
  76. }
  77. }
  78. void FlatBufferServer::clientDisconnected()
  79. {
  80. FlatBufferClient* client = qobject_cast<FlatBufferClient*>(sender());
  81. client->deleteLater();
  82. _openConnections.removeAll(client);
  83. }
  84. void FlatBufferServer::startServer()
  85. {
  86. if(!_server->isListening())
  87. {
  88. if(!_server->listen(QHostAddress::Any, _port))
  89. {
  90. Error(_log,"Failed to bind port %d", _port);
  91. }
  92. else
  93. {
  94. Info(_log,"Started on port %d", _port);
  95. emit publishService(SERVICE_TYPE, _port);
  96. }
  97. }
  98. }
  99. void FlatBufferServer::stopServer()
  100. {
  101. if(_server->isListening())
  102. {
  103. // close client connections
  104. for(const auto& client : _openConnections)
  105. {
  106. client->forceClose();
  107. }
  108. _server->close();
  109. Info(_log, "Stopped");
  110. }
  111. }