MercurySession.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include <atomic> // for atomic
  3. #include <cstdint> // for uint8_t, uint64_t, uint32_t
  4. #include <functional> // for function
  5. #include <memory> // for shared_ptr
  6. #include <mutex> // for mutex
  7. #include <string> // for string
  8. #include <unordered_map> // for unordered_map
  9. #include <vector> // for vector
  10. #include "BellTask.h" // for Task
  11. #include "Packet.h" // for Packet
  12. #include "Queue.h" // for Queue
  13. #include "Session.h" // for Session
  14. #include "protobuf/mercury.pb.h" // for Header
  15. namespace cspot {
  16. class TimeProvider;
  17. class MercurySession : public bell::Task, public cspot::Session {
  18. public:
  19. MercurySession(std::shared_ptr<cspot::TimeProvider> timeProvider);
  20. ~MercurySession();
  21. typedef std::vector<std::vector<uint8_t>> DataParts;
  22. struct Response {
  23. Header mercuryHeader;
  24. uint8_t flags;
  25. DataParts parts;
  26. uint64_t sequenceId;
  27. bool fail;
  28. };
  29. typedef std::function<void(Response&)> ResponseCallback;
  30. typedef std::function<void(bool, const std::vector<uint8_t>&)> AudioKeyCallback;
  31. typedef std::function<void()> ConnectionEstabilishedCallback;
  32. enum class RequestType : uint8_t {
  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. std::unordered_map<RequestType, std::string> RequestTypeMap = {
  49. {RequestType::GET, "GET"},
  50. {RequestType::SEND, "SEND"},
  51. {RequestType::SUB, "SUB"},
  52. {RequestType::UNSUB, "UNSUB"},
  53. };
  54. void handlePacket();
  55. uint64_t executeSubscription(RequestType type, const std::string& uri,
  56. ResponseCallback callback,
  57. ResponseCallback subscription, DataParts& parts);
  58. uint64_t executeSubscription(RequestType type, const std::string& uri,
  59. ResponseCallback callback,
  60. ResponseCallback subscription) {
  61. DataParts parts = {};
  62. return this->executeSubscription(type, uri, callback, subscription, parts);
  63. }
  64. uint64_t execute(RequestType type, const std::string& uri,
  65. ResponseCallback callback) {
  66. return this->executeSubscription(type, uri, callback, nullptr);
  67. }
  68. uint64_t execute(RequestType type, const std::string& uri,
  69. ResponseCallback callback, DataParts& parts) {
  70. return this->executeSubscription(type, uri, callback, nullptr, parts);
  71. }
  72. void requestAudioKey(const std::vector<uint8_t>& trackId,
  73. const std::vector<uint8_t>& fileId,
  74. AudioKeyCallback audioCallback);
  75. std::string getCountryCode();
  76. void disconnect();
  77. void setConnectedHandler(ConnectionEstabilishedCallback callback);
  78. bool triggerTimeout() override;
  79. private:
  80. const int PING_TIMEOUT_MS = 2 * 60 * 1000 + 5000;
  81. std::shared_ptr<cspot::TimeProvider> timeProvider;
  82. Header tempMercuryHeader = {};
  83. ConnectionEstabilishedCallback connectionReadyCallback = nullptr;
  84. bell::Queue<cspot::Packet> packetQueue;
  85. void runTask() override;
  86. void reconnect();
  87. std::unordered_map<uint64_t, ResponseCallback> callbacks;
  88. std::unordered_map<std::string, ResponseCallback> subscriptions;
  89. AudioKeyCallback audioKeyCallback;
  90. uint64_t sequenceId = 1;
  91. uint32_t audioKeySequence = 1;
  92. unsigned long long timestampDiff;
  93. unsigned long long lastPingTimestamp = -1;
  94. std::string countryCode = "";
  95. std::mutex isRunningMutex;
  96. std::atomic<bool> isRunning = false;
  97. std::atomic<bool> isReconnecting = false;
  98. std::atomic<bool> executeEstabilishedCallback = false;
  99. void failAllPending();
  100. Response decodeResponse(const std::vector<uint8_t>& data);
  101. };
  102. } // namespace cspot