1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/data_pts_waiter.h"
#include "api/api_updates.h"
PtsWaiter::PtsWaiter(not_null<Api::Updates*> owner) : _owner(owner) {
}
uint64 PtsWaiter::ptsKey(PtsSkippedQueue queue, int32 pts) {
return _queue.emplace(
uint64(uint32(pts)) << 32 | (++_skippedKey),
queue
).first->first;
}
void PtsWaiter::setWaitingForSkipped(ChannelData *channel, crl::time ms) {
if (ms >= 0) {
_owner->ptsWaiterStartTimerFor(channel, ms);
_waitingForSkipped = true;
} else {
_waitingForSkipped = false;
checkForWaiting(channel);
}
}
void PtsWaiter::setWaitingForShortPoll(ChannelData *channel, crl::time ms) {
if (ms >= 0) {
_owner->ptsWaiterStartTimerFor(channel, ms);
_waitingForShortPoll = true;
} else {
_waitingForShortPoll = false;
checkForWaiting(channel);
}
}
void PtsWaiter::checkForWaiting(ChannelData *channel) {
if (!_waitingForSkipped && !_waitingForShortPoll) {
_owner->ptsWaiterStartTimerFor(channel, -1);
}
}
void PtsWaiter::applySkippedUpdates(ChannelData *channel) {
if (!_waitingForSkipped) {
return;
}
setWaitingForSkipped(channel, -1);
if (_queue.empty()) {
return;
}
++_applySkippedLevel;
for (auto i = _queue.cbegin(), e = _queue.cend(); i != e; ++i) {
switch (i->second) {
case SkippedUpdate: {
_owner->applyUpdateNoPtsCheck(_updateQueue[i->first]);
} break;
case SkippedUpdates: {
_owner->applyUpdatesNoPtsCheck(_updatesQueue[i->first]);
} break;
}
}
--_applySkippedLevel;
clearSkippedUpdates();
}
void PtsWaiter::clearSkippedUpdates() {
_queue.clear();
_updateQueue.clear();
_updatesQueue.clear();
_applySkippedLevel = 0;
}
bool PtsWaiter::updated(
ChannelData *channel,
int32 pts,
int32 count,
const MTPUpdates &updates) {
if (_requesting || _applySkippedLevel) {
return true;
} else if (pts <= _good && count > 0) {
return false;
} else if (check(channel, pts, count)) {
return true;
}
_updatesQueue.emplace(ptsKey(SkippedUpdates, pts), updates);
return false;
}
bool PtsWaiter::updated(
ChannelData *channel,
int32 pts,
int32 count,
const MTPUpdate &update) {
if (_requesting || _applySkippedLevel) {
return true;
} else if (pts <= _good && count > 0) {
return false;
} else if (check(channel, pts, count)) {
return true;
}
_updateQueue.emplace(ptsKey(SkippedUpdate, pts), update);
return false;
}
bool PtsWaiter::updated(ChannelData *channel, int32 pts, int32 count) {
if (_requesting || _applySkippedLevel) {
return true;
} else if (pts <= _good && count > 0) {
return false;
}
return check(channel, pts, count);
}
bool PtsWaiter::updateAndApply(
ChannelData *channel,
int32 pts,
int32 count,
const MTPUpdates &updates) {
if (!updated(channel, pts, count, updates)) {
return false;
}
if (!_waitingForSkipped || _queue.empty()) {
// Optimization - no need to put in queue and back.
_owner->applyUpdatesNoPtsCheck(updates);
} else {
_updatesQueue.emplace(ptsKey(SkippedUpdates, pts), updates);
applySkippedUpdates(channel);
}
return true;
}
bool PtsWaiter::updateAndApply(
ChannelData *channel,
int32 pts,
int32 count,
const MTPUpdate &update) {
if (!updated(channel, pts, count, update)) {
return false;
}
if (!_waitingForSkipped || _queue.empty()) {
// Optimization - no need to put in queue and back.
_owner->applyUpdateNoPtsCheck(update);
} else {
_updateQueue.emplace(ptsKey(SkippedUpdate, pts), update);
applySkippedUpdates(channel);
}
return true;
}
bool PtsWaiter::updateAndApply(
ChannelData *channel,
int32 pts,
int32 count) {
if (!updated(channel, pts, count)) {
return false;
}
applySkippedUpdates(channel);
return true;
}
// Return false if need to save that update and apply later.
bool PtsWaiter::check(ChannelData *channel, int32 pts, int32 count) {
if (!inited()) {
init(pts);
return true;
}
_last = qMax(_last, pts);
_count += count;
if (_last == _count) {
_good = _last;
return true;
} else if (_last < _count) {
setWaitingForSkipped(channel, 1);
} else {
setWaitingForSkipped(channel, kWaitForSkippedTimeout);
}
return !count;
}
|