config_loader.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/timer.h"
  9. #include "base/weak_ptr.h"
  10. #include "base/bytes.h"
  11. #include "mtproto/mtproto_response.h"
  12. namespace MTP {
  13. class Instance;
  14. namespace details {
  15. class SpecialConfigRequest;
  16. class ConfigLoader : public base::has_weak_ptr {
  17. public:
  18. ConfigLoader(
  19. not_null<Instance*> instance,
  20. const QString &phone,
  21. Fn<void(const MTPConfig &result)> onDone,
  22. FailHandler onFail,
  23. bool proxyEnabled);
  24. ~ConfigLoader();
  25. void load();
  26. void setPhone(const QString &phone);
  27. void setProxyEnabled(bool value);
  28. private:
  29. mtpRequestId sendRequest(ShiftedDcId shiftedDcId);
  30. void addSpecialEndpoint(
  31. DcId dcId,
  32. const std::string &ip,
  33. int port,
  34. bytes::const_span secret);
  35. void sendSpecialRequest();
  36. void enumerate();
  37. void refreshSpecialLoader();
  38. void createSpecialLoader();
  39. DcId specialToRealDcId(DcId specialDcId);
  40. void specialConfigLoaded(const MTPConfig &result);
  41. void terminateRequest();
  42. void terminateSpecialRequest();
  43. not_null<Instance*> _instance;
  44. base::Timer _enumDCTimer;
  45. DcId _enumCurrent = 0;
  46. mtpRequestId _enumRequest = 0;
  47. struct SpecialEndpoint {
  48. DcId dcId;
  49. std::string ip;
  50. int port;
  51. bytes::vector secret;
  52. };
  53. friend bool operator==(const SpecialEndpoint &a, const SpecialEndpoint &b);
  54. std::unique_ptr<SpecialConfigRequest> _specialLoader;
  55. std::vector<SpecialEndpoint> _specialEndpoints;
  56. std::vector<SpecialEndpoint> _triedSpecialEndpoints;
  57. base::Timer _specialEnumTimer;
  58. DcId _specialEnumCurrent = 0;
  59. mtpRequestId _specialEnumRequest = 0;
  60. QString _phone;
  61. bool _proxyEnabled = false;
  62. Fn<void(const MTPConfig &result)> _doneHandler;
  63. FailHandler _failHandler;
  64. };
  65. inline bool operator==(const ConfigLoader::SpecialEndpoint &a, const ConfigLoader::SpecialEndpoint &b) {
  66. return (a.dcId == b.dcId) && (a.ip == b.ip) && (a.port == b.port);
  67. }
  68. } // namespace details
  69. } // namespace MTP