You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovCMessagingServer.cpp 3.9KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "ovCMessagingServer.h"
  2. #include "ovCMessagingImpl.hpp"
  3. namespace Communication {
  4. MessagingServer::~MessagingServer()
  5. {
  6. this->close();
  7. m_Server->release();
  8. }
  9. bool MessagingServer::accept()
  10. {
  11. this->reset();
  12. if (this->isConnected())
  13. {
  14. // A connection is already done to a client
  15. this->setLastError(Socket_ClientAlreadyConnected);
  16. return false;
  17. }
  18. if (!m_Server->isReadyToReceive())
  19. {
  20. this->setLastError(Socket_NoIncomingClientConnection);
  21. return false;
  22. }
  23. m_Client = m_Server->accept();
  24. if (m_Client == nullptr)
  25. {
  26. this->setLastError(Socket_NoIncomingClientConnection);
  27. return false;
  28. }
  29. this->setConnection(m_Client);
  30. this->startSyncing();
  31. if (!impl->m_ConnectionID.empty())
  32. {
  33. // The server has to verify that the client is the one that is waited. So the server wait the authentication packet.
  34. const std::chrono::time_point<std::chrono::system_clock> startClock = std::chrono::system_clock::now();
  35. std::string connectionID;
  36. uint64_t id = 0;
  37. bool isAuthReceived = false;
  38. while (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - startClock).count() < 10)
  39. {
  40. if (this->popAuthentication(id, connectionID))
  41. {
  42. isAuthReceived = true;
  43. if (connectionID != impl->m_ConnectionID)
  44. {
  45. this->pushError(Error_AuthenticationFail, id);
  46. this->close();
  47. this->setLastError(BadAuthenticationReceived);
  48. return false;
  49. }
  50. break;
  51. }
  52. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  53. }
  54. if (!isAuthReceived)
  55. {
  56. this->pushError(Error_AuthenticationRequested, id);
  57. this->close();
  58. this->setLastError(NoAuthenticationReceived);
  59. return false;
  60. }
  61. }
  62. if (!this->pushMessage(CommunicationProtocolVersionMessage(s_CommunicationProtocol_MajorVersion, s_CommunicationProtocol_MinorVersion)))
  63. {
  64. // Error set in the function
  65. const ELibraryError error = this->getLastError();
  66. this->close();
  67. this->setLastError(error);
  68. return false;
  69. }
  70. if (!this->pushMessage(impl->m_BoxDesc))
  71. {
  72. // Error set in the function
  73. const ELibraryError error = this->getLastError();
  74. this->close();
  75. this->setLastError(error);
  76. return false;
  77. }
  78. return true;
  79. }
  80. bool MessagingServer::close()
  81. {
  82. this->pushMessage(EndMessage());
  83. this->stopSyncing();
  84. bool errorRaised = false;
  85. if (m_Client != nullptr)
  86. {
  87. if (!m_Client->close())
  88. {
  89. errorRaised = true;
  90. this->setLastError(Socket_FailedToCloseConnection);
  91. }
  92. m_Client->release();
  93. m_Client = nullptr;
  94. }
  95. if (!m_Server->close())
  96. {
  97. this->setLastError(Socket_FailedToCloseConnection);
  98. errorRaised = true;
  99. }
  100. return !errorRaised;
  101. }
  102. bool MessagingServer::addParameter(const uint64_t id, const size_t type, const std::string& name, const std::string& value) const
  103. {
  104. return impl->m_BoxDesc.addParameter(id, type, name, value);
  105. }
  106. bool MessagingServer::addInput(const uint64_t id, const size_t type, const std::string& name) const { return impl->m_BoxDesc.addInput(id, type, name); }
  107. bool MessagingServer::addOutput(const uint64_t id, const size_t type, const std::string& name) const { return impl->m_BoxDesc.addOutput(id, type, name); }
  108. bool MessagingServer::popLog(uint64_t& packetId, ELogLevel& type, std::string& message) { return CMessaging::popLog(packetId, type, message); }
  109. bool MessagingServer::popEBML(uint64_t& packetId, size_t& index, uint64_t& startTime, uint64_t& endTime, std::shared_ptr<const std::vector<uint8_t>>& ebml)
  110. {
  111. return CMessaging::popEBML(packetId, index, startTime, endTime, ebml);
  112. }
  113. bool MessagingServer::pushError(const EError error, const uint64_t guiltyId) const { return this->pushMessage(ErrorMessage(error, guiltyId)); }
  114. bool MessagingServer::pushEBML(const size_t index, const uint64_t startTime, const uint64_t endTime,
  115. const std::shared_ptr<const std::vector<uint8_t>>& ebml) const
  116. {
  117. return this->pushMessage(EBMLMessage(index, startTime, endTime, ebml));
  118. }
  119. } // namespace Communication