mtproto_received_ids_manager.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include "mtproto/details/mtproto_received_ids_manager.h"
  8. namespace MTP::details {
  9. ReceivedIdsManager::Result ReceivedIdsManager::registerMsgId(
  10. mtpMsgId msgId,
  11. bool needAck) {
  12. const auto i = _idsNeedAck.find(msgId);
  13. if (i != _idsNeedAck.end()) {
  14. MTP_LOG(-1, ("No need to handle - %1 already is in map").arg(msgId));
  15. return Result::Duplicate;
  16. } else if (_idsNeedAck.size() < kIdsBufferSize || msgId > min()) {
  17. _idsNeedAck.emplace(msgId, needAck);
  18. return Result::Success;
  19. }
  20. MTP_LOG(-1, ("Reset on too old - %1 < min = %2").arg(msgId).arg(min()));
  21. return Result::TooOld;
  22. }
  23. mtpMsgId ReceivedIdsManager::min() const {
  24. return _idsNeedAck.empty() ? 0 : _idsNeedAck.begin()->first;
  25. }
  26. mtpMsgId ReceivedIdsManager::max() const {
  27. auto end = _idsNeedAck.end();
  28. return _idsNeedAck.empty() ? 0 : (--end)->first;
  29. }
  30. ReceivedIdsManager::State ReceivedIdsManager::lookup(mtpMsgId msgId) const {
  31. auto i = _idsNeedAck.find(msgId);
  32. if (i == _idsNeedAck.end()) {
  33. return State::NotFound;
  34. }
  35. return i->second ? State::NeedsAck : State::NoAckNeeded;
  36. }
  37. void ReceivedIdsManager::shrink() {
  38. auto size = _idsNeedAck.size();
  39. while (size-- > kIdsBufferSize) {
  40. _idsNeedAck.erase(_idsNeedAck.begin());
  41. }
  42. }
  43. void ReceivedIdsManager::clear() {
  44. _idsNeedAck.clear();
  45. }
  46. } // namespace MTP::details