mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-25 19:03:46 +03:00
Use more accurate values for msg_ids
This commit is contained in:
parent
aa7e8dba8a
commit
b0173c3ec2
|
@ -1,4 +1,3 @@
|
|||
import random
|
||||
import time
|
||||
|
||||
from ..extensions import BinaryReader, BinaryWriter
|
||||
|
@ -42,17 +41,12 @@ class MtProtoPlainSender:
|
|||
return response
|
||||
|
||||
def _get_new_msg_id(self):
|
||||
"""Generates a new message ID based on the current time (in ms) since epoch"""
|
||||
# See https://core.telegram.org/mtproto/description#message-identifier-msg-id
|
||||
ms_time = int(time.time() * 1000)
|
||||
new_msg_id = (((ms_time // 1000) << 32)
|
||||
| # "must approximately equal unix time*2^32"
|
||||
((ms_time % 1000) << 22)
|
||||
| # "approximate moment in time the message was created"
|
||||
random.randint(0, 524288)
|
||||
<< 2) # "message identifiers are divisible by 4"
|
||||
|
||||
# Ensure that we always return a message ID which is higher than the previous one
|
||||
"""Generates a new message ID based on the current time since epoch"""
|
||||
# See core.telegram.org/mtproto/description#message-identifier-msg-id
|
||||
now = time.time()
|
||||
nanoseconds = int((now - int(now)) * 1e+9)
|
||||
# "message identifiers are divisible by 4"
|
||||
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
|
||||
if self._last_msg_id >= new_msg_id:
|
||||
new_msg_id = self._last_msg_id + 4
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ import json
|
|||
import os
|
||||
import pickle
|
||||
import platform
|
||||
import random
|
||||
import time
|
||||
from threading import Lock
|
||||
from base64 import b64encode, b64decode
|
||||
|
@ -65,15 +64,10 @@ class Session:
|
|||
return self.sequence * 2
|
||||
|
||||
def get_new_msg_id(self):
|
||||
"""Generates a new message ID based on the current time (in ms) since epoch"""
|
||||
# Refer to mtproto_plain_sender.py for the original method, this is a simple copy
|
||||
ms_time = int(time.time() * 1000)
|
||||
new_msg_id = (((ms_time // 1000 + self.time_offset) << 32)
|
||||
| # "must approximately equal unix time*2^32"
|
||||
((ms_time % 1000) << 22)
|
||||
| # "approximate moment in time the message was created"
|
||||
random.randint(0, 524288)
|
||||
<< 2) # "message identifiers are divisible by 4"
|
||||
now = time.time()
|
||||
nanoseconds = int((now - int(now)) * 1e+9)
|
||||
# "message identifiers are divisible by 4"
|
||||
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
|
||||
|
||||
if self.last_message_id >= new_msg_id:
|
||||
new_msg_id = self.last_message_id + 4
|
||||
|
@ -133,7 +127,7 @@ class JsonSession:
|
|||
self._sequence = 0
|
||||
self.salt = 0 # Unsigned long
|
||||
self.time_offset = 0
|
||||
self.last_message_id = 0 # Long
|
||||
self._last_msg_id = 0 # Long
|
||||
|
||||
def save(self):
|
||||
"""Saves the current session object as session_user_id.session"""
|
||||
|
@ -229,19 +223,16 @@ class JsonSession:
|
|||
def get_new_msg_id(self):
|
||||
"""Generates a new unique message ID based on the current
|
||||
time (in ms) since epoch"""
|
||||
# Refer to mtproto_plain_sender.py for the original method,
|
||||
ms_time = int(time.time() * 1000)
|
||||
new_msg_id = (((ms_time // 1000 + self.time_offset) << 32)
|
||||
| # "must approximately equal unix time*2^32"
|
||||
((ms_time % 1000) << 22)
|
||||
| # "approximate moment in time the message was created"
|
||||
random.randint(0, 524288)
|
||||
<< 2) # "message identifiers are divisible by 4"
|
||||
# Refer to mtproto_plain_sender.py for the original method
|
||||
now = time.time()
|
||||
nanoseconds = int((now - int(now)) * 1e+9)
|
||||
# "message identifiers are divisible by 4"
|
||||
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
|
||||
|
||||
if self.last_message_id >= new_msg_id:
|
||||
new_msg_id = self.last_message_id + 4
|
||||
if self._last_msg_id >= new_msg_id:
|
||||
new_msg_id = self._last_msg_id + 4
|
||||
|
||||
self.last_message_id = new_msg_id
|
||||
self._last_msg_id = new_msg_id
|
||||
return new_msg_id
|
||||
|
||||
def update_time_offset(self, correct_msg_id):
|
||||
|
|
Loading…
Reference in New Issue
Block a user