From d89d238d303ccba82dbf4b40c7a6f4c16b22e7d7 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 18 Feb 2018 17:31:00 +0100 Subject: [PATCH] Fix BadMsgNotification error_code 32 --- pyrogram/session/auth.py | 6 +++--- pyrogram/session/internals/msg_factory.py | 5 ++--- pyrogram/session/internals/msg_id.py | 18 ++++++++---------- pyrogram/session/session.py | 5 ++--- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/pyrogram/session/auth.py b/pyrogram/session/auth.py index 741e9a44..f48242e6 100644 --- a/pyrogram/session/auth.py +++ b/pyrogram/session/auth.py @@ -51,12 +51,12 @@ class Auth: self.test_mode = test_mode self.connection = Connection(DataCenter(dc_id, test_mode), proxy) - self.msg_id = MsgId() - def pack(self, data: Object) -> bytes: + @staticmethod + def pack(data: Object) -> bytes: return ( bytes(8) - + Long(self.msg_id()) + + Long(MsgId()) + Int(len(data.write())) + data.write() ) diff --git a/pyrogram/session/internals/msg_factory.py b/pyrogram/session/internals/msg_factory.py index adf29773..f599cd6f 100644 --- a/pyrogram/session/internals/msg_factory.py +++ b/pyrogram/session/internals/msg_factory.py @@ -26,14 +26,13 @@ not_content_related = [Ping, HttpWait, MsgsAck, MsgContainer] class MsgFactory: - def __init__(self, msg_id: MsgId): - self.msg_id = msg_id + def __init__(self): self.seq_no = SeqNo() def __call__(self, body: Object) -> Message: return Message( body, - self.msg_id(), + MsgId(), self.seq_no(type(body) not in not_content_related), len(body) ) diff --git a/pyrogram/session/internals/msg_id.py b/pyrogram/session/internals/msg_id.py index cf8c0402..99aa9d14 100644 --- a/pyrogram/session/internals/msg_id.py +++ b/pyrogram/session/internals/msg_id.py @@ -21,17 +21,15 @@ from time import time class MsgId: - def __init__(self, delta_time: float = 0.0): - self.delta_time = delta_time - self.last_time = 0 - self.offset = 0 - self.lock = Lock() + last_time = 0 + offset = 0 + lock = Lock() - def __call__(self) -> int: - with self.lock: + def __new__(cls) -> int: + with cls.lock: now = time() - self.offset = self.offset + 4 if now == self.last_time else 0 - msg_id = int((now + self.delta_time) * 2 ** 32) + self.offset - self.last_time = now + cls.offset = cls.offset + 4 if now == cls.last_time else 0 + msg_id = int(now * 2 ** 32) + cls.offset + cls.last_time = now return msg_id diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 9010e2fe..b86fc992 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -89,9 +89,8 @@ class Session: self.auth_key = auth_key self.auth_key_id = sha1(auth_key).digest()[-8:] - self.msg_id = MsgId() - self.session_id = Long(self.msg_id()) - self.msg_factory = MsgFactory(self.msg_id) + self.session_id = Long(MsgId()) + self.msg_factory = MsgFactory() self.current_salt = None