1 /*
2 This file is part of Telegram Desktop,
3 the official desktop application for the Telegram messaging service.
4 
5 For license and copyright information please follow this link:
6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
7 */
8 #pragma once
9 
10 namespace Api {
11 class Updates;
12 } // namespace Api
13 
14 enum PtsSkippedQueue {
15 	SkippedUpdate,
16 	SkippedUpdates,
17 };
18 
19 class PtsWaiter {
20 public:
21 	explicit PtsWaiter(not_null<Api::Updates*> owner);
22 
23 	// 1s wait for skipped seq or pts in updates.
24 	static constexpr auto kWaitForSkippedTimeout = 1000;
25 
init(int32 pts)26 	void init(int32 pts) {
27 		_good = _last = _count = pts;
28 		clearSkippedUpdates();
29 	}
inited()30 	bool inited() const {
31 		return _good > 0;
32 	}
setRequesting(bool isRequesting)33 	void setRequesting(bool isRequesting) {
34 		_requesting = isRequesting;
35 		if (_requesting) {
36 			clearSkippedUpdates();
37 		}
38 	}
requesting()39 	bool requesting() const {
40 		return _requesting;
41 	}
waitingForSkipped()42 	bool waitingForSkipped() const {
43 		return _waitingForSkipped;
44 	}
waitingForShortPoll()45 	bool waitingForShortPoll() const {
46 		return _waitingForShortPoll;
47 	}
48 	void setWaitingForSkipped(ChannelData *channel, crl::time ms); // < 0 - not waiting
49 	void setWaitingForShortPoll(ChannelData *channel, crl::time ms); // < 0 - not waiting
current()50 	int32 current() const{
51 		return _good;
52 	}
53 	bool updated(
54 		ChannelData *channel,
55 		int32 pts,
56 		int32 count,
57 		const MTPUpdates &updates);
58 	bool updated(
59 		ChannelData *channel,
60 		int32 pts,
61 		int32 count,
62 		const MTPUpdate &update);
63 	bool updated(
64 		ChannelData *channel,
65 		int32 pts,
66 		int32 count);
67 	bool updateAndApply(
68 		ChannelData *channel,
69 		int32 pts,
70 		int32 count,
71 		const MTPUpdates &updates);
72 	bool updateAndApply(
73 		ChannelData *channel,
74 		int32 pts,
75 		int32 count,
76 		const MTPUpdate &update);
77 	bool updateAndApply(
78 		ChannelData *channel,
79 		int32 pts,
80 		int32 count);
81 	void applySkippedUpdates(ChannelData *channel);
82 	void clearSkippedUpdates();
83 
84 private:
85 	// Return false if need to save that update and apply later.
86 	bool check(ChannelData *channel, int32 pts, int32 count);
87 
88 	uint64 ptsKey(PtsSkippedQueue queue, int32 pts);
89 	void checkForWaiting(ChannelData *channel);
90 
91 	const not_null<Api::Updates*> _owner;
92 	base::flat_map<uint64, PtsSkippedQueue> _queue;
93 	base::flat_map<uint64, MTPUpdate> _updateQueue;
94 	base::flat_map<uint64, MTPUpdates> _updatesQueue;
95 	int32 _good = 0;
96 	int32 _last = 0;
97 	int32 _count = 0;
98 	int32 _applySkippedLevel = 0;
99 	bool _requesting = false;
100 	bool _waitingForSkipped = false;
101 	bool _waitingForShortPoll = false;
102 	uint32 _skippedKey = 0;
103 
104 };
105