MercuryManager.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef MERCURY_MANAGER_H
  2. #define MERCURY_MANAGER_H
  3. #include <map>
  4. #include <string>
  5. #include <functional>
  6. #include <vector>
  7. #include <mutex>
  8. #include "ShannonConnection.h"
  9. #include "MercuryResponse.h"
  10. #include "Packet.h"
  11. #include "Utils.h"
  12. #include "MercuryManager.h"
  13. #include "AudioChunk.h"
  14. #include "AudioChunkManager.h"
  15. #include <atomic>
  16. #include "BellTask.h"
  17. #include "platform/WrappedSemaphore.h"
  18. #include "TimeProvider.h"
  19. #include "Session.h"
  20. #include <NanoPBHelper.h>
  21. #include "protobuf/mercury.pb.h"
  22. #include <stdint.h>
  23. #include <memory>
  24. #define AUDIOCHUNK_TIMEOUT_MS 5 * 1000
  25. #define RECONNECTION_RETRY_MS 5 * 1000
  26. #define PING_TIMEOUT_MS 2 * 60 * 1000 + 5000
  27. typedef std::function<void(std::unique_ptr<MercuryResponse>)> mercuryCallback;
  28. typedef std::function<void(bool, std::vector<uint8_t>)> audioKeyCallback;
  29. typedef std::function<void()> voidCallback;
  30. #define AUDIO_CHUNK_SIZE 0x20000
  31. enum class MercuryType : uint8_t
  32. {
  33. SUB = 0xb3,
  34. UNSUB = 0xb4,
  35. SUBRES = 0xb5,
  36. SEND = 0xb2,
  37. GET = 0xFF, // Shitty workaround, it's value is actually same as SEND
  38. PING = 0x04,
  39. PONG_ACK = 0x4a,
  40. AUDIO_CHUNK_REQUEST_COMMAND = 0x08,
  41. AUDIO_CHUNK_SUCCESS_RESPONSE = 0x09,
  42. AUDIO_CHUNK_FAILURE_RESPONSE = 0x0A,
  43. AUDIO_KEY_REQUEST_COMMAND = 0x0C,
  44. AUDIO_KEY_SUCCESS_RESPONSE = 0x0D,
  45. AUDIO_KEY_FAILURE_RESPONSE = 0x0E,
  46. COUNTRY_CODE_RESPONSE = 0x1B,
  47. };
  48. extern std::map<MercuryType, std::string> MercuryTypeMap;
  49. class MercuryManager : public bell::Task
  50. {
  51. private:
  52. Header tempMercuryHeader;
  53. std::map<uint64_t, mercuryCallback> callbacks;
  54. std::mutex reconnectionMutex;
  55. std::mutex runningMutex;
  56. std::mutex stopMutex;
  57. std::map<std::string, mercuryCallback> subscriptions;
  58. std::unique_ptr<Session> session;
  59. std::shared_ptr<LoginBlob> lastAuthBlob;
  60. std::unique_ptr<AudioChunkManager> audioChunkManager;
  61. std::vector<std::unique_ptr<Packet>> queue;
  62. std::unique_ptr<WrappedSemaphore> queueSemaphore;
  63. unsigned long long lastRequestTimestamp = -1;
  64. unsigned long long lastPingTimestamp = -1;
  65. uint64_t sequenceId;
  66. uint32_t audioKeySequence;
  67. audioKeyCallback keyCallback;
  68. void runTask();
  69. public:
  70. MercuryManager(std::unique_ptr<Session> session);
  71. ~MercuryManager();
  72. std::atomic<bool> isRunning = false;
  73. voidCallback reconnectedCallback;
  74. uint16_t audioChunkSequence;
  75. std::shared_ptr<TimeProvider> timeProvider;
  76. char countryCode[2];
  77. bool timeoutHandler();
  78. uint64_t execute(MercuryType method, std::string uri, mercuryCallback &callback, mercuryCallback &subscription, mercuryParts &payload);
  79. uint64_t execute(MercuryType method, std::string uri, mercuryCallback &callback, mercuryCallback &subscription);
  80. uint64_t execute(MercuryType method, std::string uri, mercuryCallback &callback, mercuryParts &payload);
  81. uint64_t execute(MercuryType method, std::string uri, mercuryCallback &callback);
  82. void updateQueue();
  83. void stop();
  84. void handleQueue();
  85. void requestAudioKey(std::vector<uint8_t> trackId, std::vector<uint8_t> fileId, audioKeyCallback &audioCallback);
  86. std::shared_ptr<AudioChunk> fetchAudioChunk(std::vector<uint8_t> fileId, std::vector<uint8_t> &audioKey, uint16_t index);
  87. std::shared_ptr<AudioChunk> fetchAudioChunk(std::vector<uint8_t> fileId, std::vector<uint8_t> &audioKey, uint32_t startPos, uint32_t endPos);
  88. void unregisterAudioCallback(uint16_t seqId);
  89. void unregisterMercuryCallback(uint64_t seqId);
  90. void freeAudioKeyCallback();
  91. void reconnect();
  92. };
  93. #endif