data_pts_waiter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. namespace Api {
  9. class Updates;
  10. } // namespace Api
  11. enum PtsSkippedQueue {
  12. SkippedUpdate,
  13. SkippedUpdates,
  14. };
  15. class PtsWaiter {
  16. public:
  17. explicit PtsWaiter(not_null<Api::Updates*> owner);
  18. // 1s wait for skipped seq or pts in updates.
  19. static constexpr auto kWaitForSkippedTimeout = 1000;
  20. void init(int32 pts) {
  21. _good = _last = _count = pts;
  22. clearSkippedUpdates();
  23. }
  24. bool inited() const {
  25. return _good > 0;
  26. }
  27. void setRequesting(bool isRequesting) {
  28. _requesting = isRequesting;
  29. if (_requesting) {
  30. clearSkippedUpdates();
  31. }
  32. }
  33. bool requesting() const {
  34. return _requesting;
  35. }
  36. bool waitingForSkipped() const {
  37. return _waitingForSkipped;
  38. }
  39. bool waitingForShortPoll() const {
  40. return _waitingForShortPoll;
  41. }
  42. void setWaitingForSkipped(ChannelData *channel, crl::time ms); // < 0 - not waiting
  43. void setWaitingForShortPoll(ChannelData *channel, crl::time ms); // < 0 - not waiting
  44. int32 current() const{
  45. return _good;
  46. }
  47. bool updated(
  48. ChannelData *channel,
  49. int32 pts,
  50. int32 count,
  51. const MTPUpdates &updates);
  52. bool updated(
  53. ChannelData *channel,
  54. int32 pts,
  55. int32 count,
  56. const MTPUpdate &update);
  57. bool updated(
  58. ChannelData *channel,
  59. int32 pts,
  60. int32 count);
  61. bool updateAndApply(
  62. ChannelData *channel,
  63. int32 pts,
  64. int32 count,
  65. const MTPUpdates &updates);
  66. bool updateAndApply(
  67. ChannelData *channel,
  68. int32 pts,
  69. int32 count,
  70. const MTPUpdate &update);
  71. bool updateAndApply(
  72. ChannelData *channel,
  73. int32 pts,
  74. int32 count);
  75. void applySkippedUpdates(ChannelData *channel);
  76. void clearSkippedUpdates();
  77. private:
  78. // Return false if need to save that update and apply later.
  79. bool check(ChannelData *channel, int32 pts, int32 count);
  80. uint64 ptsKey(PtsSkippedQueue queue, int32 pts);
  81. void checkForWaiting(ChannelData *channel);
  82. const not_null<Api::Updates*> _owner;
  83. base::flat_map<uint64, PtsSkippedQueue> _queue;
  84. base::flat_map<uint64, MTPUpdate> _updateQueue;
  85. base::flat_map<uint64, MTPUpdates> _updatesQueue;
  86. int32 _good = 0;
  87. int32 _last = 0;
  88. int32 _count = 0;
  89. int32 _applySkippedLevel = 0;
  90. bool _requesting = false;
  91. bool _waitingForSkipped = false;
  92. bool _waitingForShortPoll = false;
  93. uint32 _skippedKey = 0;
  94. };