mirror of
https://github.com/TeamPGM/pyrogram.git
synced 2024-11-16 04:35:24 +00:00
Fix time going out of sync when starting new sessions
This commit is contained in:
parent
d7be2c90a1
commit
fbded4e23b
@ -26,14 +26,13 @@ not_content_related = (Ping, HttpWait, MsgsAck, MsgContainer)
|
||||
|
||||
|
||||
class MsgFactory:
|
||||
def __init__(self, server_time: float = 0):
|
||||
def __init__(self):
|
||||
self.seq_no = SeqNo()
|
||||
self.server_time = server_time
|
||||
|
||||
def __call__(self, body: TLObject) -> Message:
|
||||
return Message(
|
||||
body,
|
||||
MsgId(self.server_time),
|
||||
MsgId(),
|
||||
self.seq_no(not isinstance(body, not_content_related)),
|
||||
len(body)
|
||||
)
|
||||
|
@ -16,18 +16,29 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from time import monotonic
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MsgId:
|
||||
reference_clock = monotonic()
|
||||
last_time = 0
|
||||
msg_id_offset = 0
|
||||
server_time = 0
|
||||
|
||||
def __new__(cls, server_time: float = 0) -> int:
|
||||
now = monotonic() - cls.reference_clock + server_time
|
||||
def __new__(cls) -> int:
|
||||
now = monotonic() - cls.reference_clock + cls.server_time
|
||||
cls.msg_id_offset = cls.msg_id_offset + 4 if now == cls.last_time else 0
|
||||
msg_id = int(now * 2 ** 32) + cls.msg_id_offset
|
||||
cls.last_time = now
|
||||
|
||||
return msg_id
|
||||
|
||||
@classmethod
|
||||
def set_server_time(cls, server_time: int):
|
||||
if not cls.server_time:
|
||||
cls.server_time = server_time
|
||||
log.info(f"Time synced: {datetime.utcfromtimestamp(server_time)} UTC")
|
||||
|
@ -23,6 +23,7 @@ from concurrent.futures.thread import ThreadPoolExecutor
|
||||
from datetime import datetime, timedelta
|
||||
from hashlib import sha1
|
||||
from io import BytesIO
|
||||
import os
|
||||
|
||||
import pyrogram
|
||||
from pyrogram import __copyright__, __license__, __version__
|
||||
@ -96,7 +97,7 @@ class Session:
|
||||
|
||||
self.auth_key_id = sha1(auth_key).digest()[-8:]
|
||||
|
||||
self.session_id = Long(MsgId(time.time()))
|
||||
self.session_id = os.urandom(8)
|
||||
self.msg_factory = MsgFactory()
|
||||
|
||||
self.current_salt = None
|
||||
@ -247,9 +248,7 @@ class Session:
|
||||
|
||||
for msg in messages:
|
||||
if msg.seq_no == 0:
|
||||
server_time = msg.msg_id / (2 ** 32)
|
||||
self.msg_factory.server_time = server_time
|
||||
log.info(f"Time synced: {datetime.utcfromtimestamp(server_time)} UTC")
|
||||
MsgId.set_server_time(msg.msg_id / (2 ** 32))
|
||||
|
||||
if msg.seq_no % 2 != 0:
|
||||
if msg.msg_id in self.pending_acks:
|
||||
|
Loading…
Reference in New Issue
Block a user