WebSocketClient.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Copyright (c) 2023 Audiokinetic Inc.
  10. *******************************************************************************/
  11. #pragma once
  12. #include <AK/Tools/Common/AkAssert.h>
  13. #include <string>
  14. extern "C" {
  15. struct mg_connection;
  16. }
  17. namespace AK
  18. {
  19. namespace WwiseAuthoringAPI
  20. {
  21. class IWebSocketClientHandler;
  22. class WebSocketClient final
  23. {
  24. public:
  25. WebSocketClient(const WebSocketClient&) = delete;
  26. WebSocketClient(WebSocketClient&&) = delete;
  27. WebSocketClient(IWebSocketClientHandler* handler)
  28. : m_handler(handler)
  29. {
  30. AKASSERT(handler != nullptr);
  31. }
  32. ~WebSocketClient()
  33. {
  34. // Must wait for the connection close to be handled before being able to delete the object, otherwise
  35. // we could have a dangling callback in the lib that would call us from another thread
  36. // on an already deleted object.
  37. }
  38. bool Connect(const char* host, const int port);
  39. void Close();
  40. bool SendUTF8(const std::string& message, std::string& out_errorMessage);
  41. bool IsConnected()
  42. {
  43. return (m_connection != nullptr);
  44. }
  45. private:
  46. static bool m_isNetworkInitialized;
  47. static inline bool EnsureNetworkInit()
  48. {
  49. if (!m_isNetworkInitialized)
  50. {
  51. m_isNetworkInitialized = InitializeNetwork();
  52. }
  53. return m_isNetworkInitialized;
  54. }
  55. static bool InitializeNetwork();
  56. static int OnMessage(
  57. mg_connection *conn,
  58. int flags,
  59. char* data,
  60. size_t data_len,
  61. void* user_data);
  62. static void OnClose(
  63. const mg_connection *conn,
  64. void *user_data);
  65. mg_connection* m_connection = nullptr;
  66. IWebSocketClientHandler* m_handler;
  67. };
  68. }
  69. }