2019-01-04 11:09:48 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2020-06-11 09:41:03 +00:00
|
|
|
#include "api/api_updates.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
|
2020-06-11 09:41:03 +00:00
|
|
|
PtsWaiter::PtsWaiter(not_null<Api::Updates*> owner) : _owner(owner) {
|
2020-06-08 15:17:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
uint64 PtsWaiter::ptsKey(PtsSkippedQueue queue, int32 pts) {
|
2020-06-08 15:17:33 +00:00
|
|
|
return _queue.emplace(
|
|
|
|
uint64(uint32(pts)) << 32 | (++_skippedKey),
|
|
|
|
queue
|
|
|
|
).first->first;
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 09:41:03 +00:00
|
|
|
void PtsWaiter::setWaitingForSkipped(ChannelData *channel, crl::time ms) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (ms >= 0) {
|
2020-06-11 09:41:03 +00:00
|
|
|
_owner->ptsWaiterStartTimerFor(channel, ms);
|
2019-01-04 11:09:48 +00:00
|
|
|
_waitingForSkipped = true;
|
|
|
|
} else {
|
|
|
|
_waitingForSkipped = false;
|
|
|
|
checkForWaiting(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 09:41:03 +00:00
|
|
|
void PtsWaiter::setWaitingForShortPoll(ChannelData *channel, crl::time ms) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (ms >= 0) {
|
2020-06-11 09:41:03 +00:00
|
|
|
_owner->ptsWaiterStartTimerFor(channel, ms);
|
2019-01-04 11:09:48 +00:00
|
|
|
_waitingForShortPoll = true;
|
|
|
|
} else {
|
|
|
|
_waitingForShortPoll = false;
|
|
|
|
checkForWaiting(channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PtsWaiter::checkForWaiting(ChannelData *channel) {
|
2020-06-11 09:41:03 +00:00
|
|
|
if (!_waitingForSkipped && !_waitingForShortPoll) {
|
|
|
|
_owner->ptsWaiterStartTimerFor(channel, -1);
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PtsWaiter::applySkippedUpdates(ChannelData *channel) {
|
2020-06-08 15:17:33 +00:00
|
|
|
if (!_waitingForSkipped) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-04 11:09:48 +00:00
|
|
|
|
|
|
|
setWaitingForSkipped(channel, -1);
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
if (_queue.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-04 11:09:48 +00:00
|
|
|
|
|
|
|
++_applySkippedLevel;
|
|
|
|
for (auto i = _queue.cbegin(), e = _queue.cend(); i != e; ++i) {
|
2020-06-08 15:17:33 +00:00
|
|
|
switch (i->second) {
|
|
|
|
case SkippedUpdate: {
|
2020-06-11 09:41:03 +00:00
|
|
|
_owner->applyUpdateNoPtsCheck(_updateQueue[i->first]);
|
2020-06-08 15:17:33 +00:00
|
|
|
} break;
|
|
|
|
case SkippedUpdates: {
|
2020-06-11 09:41:03 +00:00
|
|
|
_owner->applyUpdatesNoPtsCheck(_updatesQueue[i->first]);
|
2020-06-08 15:17:33 +00:00
|
|
|
} break;
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--_applySkippedLevel;
|
|
|
|
clearSkippedUpdates();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PtsWaiter::clearSkippedUpdates() {
|
|
|
|
_queue.clear();
|
|
|
|
_updateQueue.clear();
|
|
|
|
_updatesQueue.clear();
|
|
|
|
_applySkippedLevel = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
bool PtsWaiter::updated(
|
|
|
|
ChannelData *channel,
|
|
|
|
int32 pts,
|
|
|
|
int32 count,
|
|
|
|
const MTPUpdates &updates) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (_requesting || _applySkippedLevel) {
|
|
|
|
return true;
|
|
|
|
} else if (pts <= _good && count > 0) {
|
|
|
|
return false;
|
|
|
|
} else if (check(channel, pts, count)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
_updatesQueue.emplace(ptsKey(SkippedUpdates, pts), updates);
|
2019-01-04 11:09:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
bool PtsWaiter::updated(
|
|
|
|
ChannelData *channel,
|
|
|
|
int32 pts,
|
|
|
|
int32 count,
|
|
|
|
const MTPUpdate &update) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (_requesting || _applySkippedLevel) {
|
|
|
|
return true;
|
|
|
|
} else if (pts <= _good && count > 0) {
|
|
|
|
return false;
|
|
|
|
} else if (check(channel, pts, count)) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
_updateQueue.emplace(ptsKey(SkippedUpdate, pts), update);
|
2019-01-04 11:09:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
bool PtsWaiter::updateAndApply(
|
|
|
|
ChannelData *channel,
|
|
|
|
int32 pts,
|
|
|
|
int32 count,
|
|
|
|
const MTPUpdates &updates) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (!updated(channel, pts, count, updates)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
if (!_waitingForSkipped || _queue.empty()) {
|
2019-01-04 11:09:48 +00:00
|
|
|
// Optimization - no need to put in queue and back.
|
2020-06-11 09:41:03 +00:00
|
|
|
_owner->applyUpdatesNoPtsCheck(updates);
|
2019-01-04 11:09:48 +00:00
|
|
|
} else {
|
2020-06-08 15:17:33 +00:00
|
|
|
_updatesQueue.emplace(ptsKey(SkippedUpdates, pts), updates);
|
2019-01-04 11:09:48 +00:00
|
|
|
applySkippedUpdates(channel);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
bool PtsWaiter::updateAndApply(
|
|
|
|
ChannelData *channel,
|
|
|
|
int32 pts,
|
|
|
|
int32 count,
|
|
|
|
const MTPUpdate &update) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (!updated(channel, pts, count, update)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
if (!_waitingForSkipped || _queue.empty()) {
|
2019-01-04 11:09:48 +00:00
|
|
|
// Optimization - no need to put in queue and back.
|
2020-06-11 09:41:03 +00:00
|
|
|
_owner->applyUpdateNoPtsCheck(update);
|
2019-01-04 11:09:48 +00:00
|
|
|
} else {
|
2020-06-08 15:17:33 +00:00
|
|
|
_updateQueue.emplace(ptsKey(SkippedUpdate, pts), update);
|
2019-01-04 11:09:48 +00:00
|
|
|
applySkippedUpdates(channel);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-08 15:17:33 +00:00
|
|
|
bool PtsWaiter::updateAndApply(
|
|
|
|
ChannelData *channel,
|
|
|
|
int32 pts,
|
|
|
|
int32 count) {
|
2019-01-04 11:09:48 +00:00
|
|
|
if (!updated(channel, pts, count)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
applySkippedUpdates(channel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
// Return false if need to save that update and apply later.
|
|
|
|
bool PtsWaiter::check(ChannelData *channel, int32 pts, int32 count) {
|
2019-01-04 11:09:48 +00:00
|
|
|
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 {
|
2019-01-18 11:26:43 +00:00
|
|
|
setWaitingForSkipped(channel, kWaitForSkippedTimeout);
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
return !count;
|
|
|
|
}
|