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.

ovCMessaging.h 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #pragma once
  2. #include <socket/IConnection.h>
  3. #include "defines.h"
  4. #include "ovCMessagingProtocol.h"
  5. namespace Communication {
  6. /**
  7. * \brief The purpose of this class is to provide a communication protocol to exchange EBML between a client and a server.
  8. */
  9. class Communication_API CMessaging
  10. {
  11. public:
  12. /**
  13. * \brief Library error codes
  14. */
  15. enum ELibraryError
  16. {
  17. NoError = 0,
  18. Socket_NotConnected = 1,
  19. Socket_FailedToConnect = 2,
  20. Socket_ReceiveBufferFail = 3,
  21. Socket_SendBufferFail = 4,
  22. Socket_NoIncomingClientConnection = 6,
  23. Socket_NotReadyToSend = 7,
  24. Socket_NoDataReceived = 8,
  25. Socket_FailedCloseClientConnection = 10,
  26. Socket_FailedToCloseConnection = 11,
  27. Socket_FailedConnectClient = 12,
  28. Socket_ClientAlreadyConnected = 13,
  29. Deserialize_BufferTooSmall = 30,
  30. Deserialize_Header = 31,
  31. Deserialize_ProtocolVersionMessage = 32,
  32. Deserialize_BoxDescriptionMessage = 33,
  33. Deserialize_EBMLMessage = 34,
  34. Deserialize_EndMessage = 35,
  35. Deserialize_ErrorMessage = 36,
  36. Deserialize_LogMessage = 37,
  37. Deserialize_AuthenticationMessage = 38,
  38. Deserialize_MessageTypeNotSupported = 39,
  39. BoxDescriptionAlreadyReceived = 60,
  40. BoxDescriptionNotReceived = 61,
  41. BadAuthenticationReceived = 70,
  42. NoAuthenticationReceived = 71,
  43. ThreadJoinFailed = 80
  44. };
  45. CMessaging();
  46. virtual ~CMessaging();
  47. /**
  48. * \brief Get the code of the last error produced by the API
  49. *
  50. * \retval Error code
  51. */
  52. ELibraryError getLastError() const;
  53. /**
  54. * \brief Give a short description of an error.
  55. *
  56. * \param error the error code
  57. *
  58. * \return Description of the error.
  59. */
  60. static std::string getErrorString(ELibraryError error);
  61. /**
  62. * \brief Check that the socket is connected.
  63. *
  64. * \retval True if the socket is connected.
  65. * \retval False if the socket is not connected.
  66. */
  67. bool isConnected() const;
  68. /**
  69. * \brief Check that the synchronization is in error state.
  70. *
  71. * \retval True if the synchronization is in error state.
  72. * \retval False if the synchronization is ok.
  73. */
  74. bool isInErrorState() const;
  75. /**
  76. * \brief Set the connection ID to a new value
  77. * \param connectionID The connection Id to set
  78. */
  79. void setConnectionID(const std::string& connectionID) const;
  80. /**
  81. * \brief Check that a End message was received.
  82. *
  83. * \retval True if the end message from the client is received.
  84. * \retval False if the end message from the client is not received.
  85. */
  86. virtual bool isEndReceived();
  87. /**
  88. * \brief Get the time.
  89. */
  90. virtual uint64_t getTime();
  91. protected:
  92. /**
  93. * \brief Push a message to the send buffer.
  94. * The message will be really sent in the socket in the next synchronization.
  95. *
  96. * \param message The message to send.
  97. *
  98. * \retval True if it succeeds.
  99. * \retval False if library is in error state.
  100. */
  101. bool pushMessage(const Message& message) const;
  102. /**
  103. * \brief Set the last error code.
  104. *
  105. * \param libraryError The error
  106. *
  107. * \sa getLastError
  108. */
  109. void setLastError(ELibraryError libraryError) const;
  110. /**
  111. * \brief Provide the connection to the base class to communicate.
  112. *
  113. * \param connection The connection
  114. */
  115. void setConnection(Socket::IConnection* connection) const;
  116. /**
  117. * \brief Start a thread that will push the outgoing data, pull and process the incoming data.
  118. * This sync will be stopped in cases:
  119. * - An error raised
  120. * - stopSyncing() function was called.
  121. *
  122. * \retval True if it succeeds.
  123. * \retval False if an error occured.
  124. *
  125. * \sa stopSyncing
  126. */
  127. bool startSyncing();
  128. /**
  129. * \brief Request to stop the sync and stop the thread.
  130. *
  131. * \retval True if it succeeds.
  132. * \retval False if an error occured.
  133. *
  134. * \sa startSyncing
  135. */
  136. bool stopSyncing() const;
  137. /**
  138. * @brief Get the oldest authentication message
  139. * @param id[out] Identifier of the message
  140. * @param connectionID[out] Connection Id
  141. * @return true if a message was popped, false if the queue is empty
  142. */
  143. virtual bool popAuthentication(uint64_t& id, std::string& connectionID);
  144. /**
  145. * @brief Get the oldest box description message
  146. * @param id[out] Identifier of the message
  147. * @param boxDescription[out] Descriptor of the box
  148. * @return true if a message was popped, false if the queue is empty
  149. */
  150. virtual bool popBoxDescriptions(uint64_t& id, BoxDescriptionMessage& boxDescription);
  151. /**
  152. * @brief popCommunicationProtocolVersion
  153. * @param id[out] Identifier of the message
  154. * @param majorVersion[out] major version of the protocol
  155. * @param minorVersion[out] minor version of the protocol
  156. * @return true if a message was popped, false if the queue is empty
  157. */
  158. virtual bool popCommunicationProtocolVersion(uint64_t& id, uint8_t& majorVersion, uint8_t& minorVersion);
  159. /**
  160. * @brief Pop the oldest EBML message from the queue
  161. * @param id[out] Identifier of the message
  162. * @param index[out] Input index to which the EBML should be directed
  163. * @param startTime[out] Start time of the buffer
  164. * @param endTime[out] End time of the buffer
  165. * @param ebml[out] The encoded EBML buffer
  166. * @return true if a message was popped, false if the queue is empty
  167. */
  168. virtual bool popEBML(uint64_t& id, size_t& index, uint64_t& startTime, uint64_t& endTime, std::shared_ptr<const std::vector<uint8_t>>& ebml);
  169. /**
  170. * @brief Pop the oldest log message from the queue
  171. * @param id[out] Identifier of the message
  172. * @param type[out] Log level of the message
  173. * @param message[out] Message text
  174. * @return true if a message was popped, false if the queue is empty
  175. */
  176. virtual bool popLog(uint64_t& id, ELogLevel& type, std::string& message);
  177. /**
  178. * @brief Pop the oldest error message from the queue
  179. * @param id[out] Identifier of the message
  180. * @param type[out] Error code
  181. * @param guiltyId[out] If of the sent message that caused this error
  182. * @return true if a message was popped, false if the queue is empty
  183. */
  184. virtual bool popError(uint64_t& id, EError& type, uint64_t& guiltyId);
  185. /**
  186. * @brief Get the oldes End message
  187. * @param id[out] Identifier of the message
  188. * @return true if a message was popped, false if the queue is empty
  189. */
  190. virtual bool popEnd(uint64_t& id);
  191. /**
  192. * @brief Reset the library state. Stop sending and receiving buffers, disconnect
  193. * and empty all buffers.
  194. */
  195. void reset() const;
  196. /**
  197. * \brief Checks if a sync message was received and reset it if it was.
  198. *
  199. * This method should be used in a busy loop to check if the other
  200. * party has finished processing all of the data.
  201. *
  202. * From the external program standpoint this method will return true
  203. * when the box has finished sending all of the data that has to be
  204. * processed in one bulk.
  205. *
  206. * From the box standpoint, this means that the external program has
  207. * finished processing and sending all of the data that the box has
  208. * sent.
  209. *
  210. * \retval True if a sync message is received.
  211. * \retval False if no sync message was received.
  212. */
  213. virtual bool waitForSyncMessage();
  214. private:
  215. /**
  216. * \brief Receive all the available data from the socket and insert it in the rcv buffer.
  217. *
  218. * \retval True if it succeeds.
  219. * \retval False if an error occured.
  220. * Library errors: TODO
  221. *
  222. * \sa push
  223. */
  224. bool pull() const;
  225. /**
  226. * \brief Send all the data to the socket.
  227. *
  228. * \retval True if it succeeds.
  229. * \retval False if an error occured.
  230. * Library errors:
  231. * - Socket_NotConnected
  232. *
  233. *
  234. * \sa push
  235. */
  236. bool push() const;
  237. /**
  238. * \brief Process incoming data, unpack it and put messages in queues.
  239. * It use processBuffer.
  240. *
  241. * \retval True if it succeeds.
  242. * \retval False if an error occured.
  243. * Library errors: Error from processBuffer
  244. *
  245. * \sa processBuffer
  246. */
  247. bool processIncomingMessages() const;
  248. /**
  249. * \brief Process buffer, pack it and put messages in queues.
  250. *
  251. * \param buffer Buffer with the incoming serialized data.
  252. * \param[out] byteRead Number of bytes read and processed.
  253. *
  254. * \retval True if it succeeds.
  255. * \retval False if an error occured.
  256. * Library errors:
  257. * - Deserialize_Header
  258. * - Deserialize_AuthenticationMessage
  259. * - Deserialize_ProtocolVersionMessage
  260. * - Deserialize_BoxDescriptionMessage
  261. * - Deserialize_EBMLMessage
  262. * - Deserialize_LogMessage
  263. * - Deserialize_ErrorMessage
  264. * - Deserialize_MessageTypeNotSupported
  265. *
  266. * \sa processIncomingMessages
  267. */
  268. bool processBuffer(const std::vector<uint8_t>& buffer, size_t& byteRead) const;
  269. /**
  270. * \brief Sync fucntion that is used in a thread to pull, push and process the incoming data.
  271. *
  272. * \retval True if it succeeds.
  273. * \retval False if an error occured.
  274. * Library errors: Errors from processIncomingMessages(), push() or pull()
  275. *
  276. * \sa pull
  277. * \sa push
  278. * \sa processIncomingMessages
  279. */
  280. void sync() const;
  281. public:
  282. static const uint8_t s_CommunicationProtocol_MajorVersion = 1;
  283. static const uint8_t s_CommunicationProtocol_MinorVersion = 1;
  284. protected:
  285. struct SMessagingImpl;
  286. SMessagingImpl* impl = nullptr;
  287. };
  288. } // namespace Communication