Use more accurate values for msg_ids

This commit is contained in:
Lonami Exo 2017-06-26 11:00:43 +02:00
parent aa7e8dba8a
commit b0173c3ec2
2 changed files with 19 additions and 34 deletions

View File

@ -1,4 +1,3 @@
import random
import time import time
from ..extensions import BinaryReader, BinaryWriter from ..extensions import BinaryReader, BinaryWriter
@ -42,17 +41,12 @@ class MtProtoPlainSender:
return response return response
def _get_new_msg_id(self): def _get_new_msg_id(self):
"""Generates a new message ID based on the current time (in ms) since epoch""" """Generates a new message ID based on the current time since epoch"""
# See https://core.telegram.org/mtproto/description#message-identifier-msg-id # See core.telegram.org/mtproto/description#message-identifier-msg-id
ms_time = int(time.time() * 1000) now = time.time()
new_msg_id = (((ms_time // 1000) << 32) nanoseconds = int((now - int(now)) * 1e+9)
| # "must approximately equal unix time*2^32" # "message identifiers are divisible by 4"
((ms_time % 1000) << 22) new_msg_id = (int(now) << 32) | (nanoseconds << 2)
| # "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
if self._last_msg_id >= new_msg_id: if self._last_msg_id >= new_msg_id:
new_msg_id = self._last_msg_id + 4 new_msg_id = self._last_msg_id + 4

View File

@ -2,7 +2,6 @@ import json
import os import os
import pickle import pickle
import platform import platform
import random
import time import time
from threading import Lock from threading import Lock
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
@ -65,15 +64,10 @@ class Session:
return self.sequence * 2 return self.sequence * 2
def get_new_msg_id(self): def get_new_msg_id(self):
"""Generates a new message ID based on the current time (in ms) since epoch""" now = time.time()
# Refer to mtproto_plain_sender.py for the original method, this is a simple copy nanoseconds = int((now - int(now)) * 1e+9)
ms_time = int(time.time() * 1000) # "message identifiers are divisible by 4"
new_msg_id = (((ms_time // 1000 + self.time_offset) << 32) new_msg_id = (int(now) << 32) | (nanoseconds << 2)
| # "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"
if self.last_message_id >= new_msg_id: if self.last_message_id >= new_msg_id:
new_msg_id = self.last_message_id + 4 new_msg_id = self.last_message_id + 4
@ -133,7 +127,7 @@ class JsonSession:
self._sequence = 0 self._sequence = 0
self.salt = 0 # Unsigned long self.salt = 0 # Unsigned long
self.time_offset = 0 self.time_offset = 0
self.last_message_id = 0 # Long self._last_msg_id = 0 # Long
def save(self): def save(self):
"""Saves the current session object as session_user_id.session""" """Saves the current session object as session_user_id.session"""
@ -229,19 +223,16 @@ class JsonSession:
def get_new_msg_id(self): def get_new_msg_id(self):
"""Generates a new unique message ID based on the current """Generates a new unique message ID based on the current
time (in ms) since epoch""" time (in ms) since epoch"""
# Refer to mtproto_plain_sender.py for the original method, # Refer to mtproto_plain_sender.py for the original method
ms_time = int(time.time() * 1000) now = time.time()
new_msg_id = (((ms_time // 1000 + self.time_offset) << 32) nanoseconds = int((now - int(now)) * 1e+9)
| # "must approximately equal unix time*2^32" # "message identifiers are divisible by 4"
((ms_time % 1000) << 22) new_msg_id = (int(now) << 32) | (nanoseconds << 2)
| # "approximate moment in time the message was created"
random.randint(0, 524288)
<< 2) # "message identifiers are divisible by 4"
if self.last_message_id >= new_msg_id: if self._last_msg_id >= new_msg_id:
new_msg_id = self.last_message_id + 4 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 return new_msg_id
def update_time_offset(self, correct_msg_id): def update_time_offset(self, correct_msg_id):