Client.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/WwiseAuthoringAPI/AkAutobahn/autobahn.h"
  13. #include "AK/WwiseAuthoringAPI/AkAutobahn/AkJson.h"
  14. #include "AK/WwiseAuthoringAPI/AkAutobahn/FutureUtils.h"
  15. #include <map>
  16. #include <cstdint>
  17. #include <functional>
  18. #include <mutex>
  19. namespace AK
  20. {
  21. namespace WwiseAuthoringAPI
  22. {
  23. class session;
  24. class Client
  25. {
  26. private:
  27. // Any timeout value lower than this constant will be adjusted. We must allow a reasonnable amount of time
  28. // for the server to respond. A timeout of -1 is considered infinite (call will block until server has responded).
  29. const int MIN_TIMEOUT_MS = 100;
  30. public:
  31. typedef std::function<void(uint64_t in_subscriptionId, const JsonProvider& in_jsonProvider)> WampEventCallback;
  32. public:
  33. Client();
  34. virtual ~Client();
  35. bool Connect(const char* in_uri, unsigned int in_port, disconnectHandler_t disconnectHandler = nullptr, int in_timeoutMs = -1);
  36. bool IsConnected() const;
  37. void Disconnect();
  38. bool Subscribe(const char* in_uri, const char* in_options, WampEventCallback in_callback, uint64_t& out_subscriptionId, std::string& out_result, int in_timeoutMs = -1);
  39. bool Subscribe(const char* in_uri, const AkJson& in_options, WampEventCallback in_callback, uint64_t& out_subscriptionId, AkJson& out_result, int in_timeoutMs = -1);
  40. bool Unsubscribe(const uint64_t& in_subscriptionId, std::string& out_result, int in_timeoutMs = -1);
  41. bool Unsubscribe(const uint64_t& in_subscriptionId, AkJson& out_result, int in_timeoutMs = -1);
  42. bool Call(const char* in_uri, const char* in_args, const char* in_options, std::string& out_result, int in_timeoutMs = -1);
  43. bool Call(const char* in_uri, const AkJson& in_args, const AkJson& in_options, AkJson& out_result, int in_timeoutMs = -1);
  44. protected:
  45. void Log(const char* log);
  46. private:
  47. void LogErrorMessageFromJson(const AkJson& in_json);
  48. bool SubscribeImpl(const char* in_uri, const AkJson& in_options, handler_t in_callback, int in_timeoutMs, uint64_t& out_subscriptionId, AkJson& out_result);
  49. bool UnsubscribeImpl(const uint64_t& in_subscriptionId, int in_timeoutMs, AkJson& out_result);
  50. template<typename T> bool GetFuture(std::future<T>& in_value, int in_timeoutMs, T& out_result)
  51. {
  52. if (in_timeoutMs > -1)
  53. {
  54. if (in_timeoutMs < MIN_TIMEOUT_MS)
  55. {
  56. in_timeoutMs = MIN_TIMEOUT_MS;
  57. }
  58. return GetFutureWithTimeout(in_timeoutMs, in_value, out_result);
  59. }
  60. else
  61. {
  62. return GetFutureBlocking(in_value, out_result);
  63. }
  64. }
  65. private:
  66. session* m_ws;
  67. };
  68. }
  69. }