Remove unused fields from the Sessions

Most of the stuff didn't actually need to be saved and only
belong to the MTProtoState which is not a separate class from
the sessions.
This commit is contained in:
Lonami Exo
2018-06-14 17:04:15 +02:00
parent bb3a564500
commit 1247d050ab
2 changed files with 40 additions and 146 deletions

View File

@@ -1,18 +1,9 @@
from abc import ABC, abstractmethod
import time
import struct
import os
class Session(ABC):
def __init__(self):
# Session IDs can be random on every connection
self.id = struct.unpack('q', os.urandom(8))[0]
self._sequence = 0
self._last_msg_id = 0
self._time_offset = 0
self._salt = 0
self._report_errors = True
self._flood_sleep_threshold = 60
@@ -155,20 +146,6 @@ class Session(ABC):
"""
raise NotImplementedError
@property
def salt(self):
"""
Returns the current salt used when encrypting messages.
"""
return self._salt
@salt.setter
def salt(self, value):
"""
Updates the salt (integer) used when encrypting messages.
"""
self._salt = value
@property
def report_errors(self):
"""
@@ -185,21 +162,6 @@ class Session(ABC):
"""
self._report_errors = value
@property
def time_offset(self):
"""
Time offset (in seconds) to be used
in case the local time is incorrect.
"""
return self._time_offset
@time_offset.setter
def time_offset(self, value):
"""
Updates the integer time offset in seconds.
"""
self._time_offset = value
@property
def flood_sleep_threshold(self):
"""
@@ -214,55 +176,3 @@ class Session(ABC):
Sets the new time threshold (integer, float or timedelta).
"""
self._flood_sleep_threshold = value
@property
def sequence(self):
"""
Current sequence number needed to generate messages.
"""
return self._sequence
@sequence.setter
def sequence(self, value):
"""
Updates the sequence number (integer) value.
"""
self._sequence = value
def get_new_msg_id(self):
"""
Generates a new unique message ID based on the current
time (in ms) since epoch, applying a known time offset.
"""
now = time.time() + self._time_offset
nanoseconds = int((now - int(now)) * 1e+9)
new_msg_id = (int(now) << 32) | (nanoseconds << 2)
if self._last_msg_id >= new_msg_id:
new_msg_id = self._last_msg_id + 4
self._last_msg_id = new_msg_id
return new_msg_id
def update_time_offset(self, correct_msg_id):
"""
Updates the time offset to the correct
one given a known valid message ID.
"""
now = int(time.time())
correct = correct_msg_id >> 32
self._time_offset = correct - now
self._last_msg_id = 0
def generate_sequence(self, content_related):
"""
Generates the next sequence number depending on whether
it should be for a content-related query or not.
"""
if content_related:
result = self._sequence * 2 + 1
self._sequence += 1
return result
else:
return self._sequence * 2