Move the "constant read" thread to the main TelegramClient

This commit is contained in:
Lonami Exo
2017-09-03 09:56:10 +02:00
parent 69d182815f
commit 4de4026bb3
3 changed files with 58 additions and 51 deletions

View File

@@ -97,8 +97,7 @@ class TelegramBareClient:
# region Connecting
def connect(self, exported_auth=None, initial_query=None,
constant_read=False):
def connect(self, exported_auth=None, initial_query=None):
"""Connects to the Telegram servers, executing authentication if
required. Note that authenticating to the Telegram servers is
not the same as authenticating the desired user itself, which
@@ -110,9 +109,6 @@ class TelegramBareClient:
If 'initial_query' is not None, it will override the default
'GetConfigRequest()', and its result will be returned ONLY
if the client wasn't connected already.
The 'constant_read' parameter will be used when creating
the MtProtoSender. Refer to it for more information.
"""
if self._sender and self._sender.is_connected():
# Try sending a ping to make sure we're connected already
@@ -139,9 +135,7 @@ class TelegramBareClient:
self.session.save()
self._sender = MtProtoSender(
connection, self.session, constant_read=constant_read
)
self._sender = MtProtoSender(connection, self.session)
self._sender.unhandled_callbacks = self._update_callbacks
self._sender.connect()
@@ -293,11 +287,15 @@ class TelegramBareClient:
# region Invoking Telegram requests
def invoke(self, request, updates=None):
def invoke(self, request, updates=None, call_receive=True):
"""Invokes (sends) a MTProtoRequest and returns (receives) its result.
If 'updates' is not None, all read update object will be put
in such list. Otherwise, update objects will be ignored.
If 'call_receive' is set to False, then there should be another
thread calling to 'self._sender.receive()' running or this method
will lock forever.
"""
if not isinstance(request, TLObject) and not request.content_related:
raise ValueError('You can only invoke requests, not types!')
@@ -307,12 +305,12 @@ class TelegramBareClient:
try:
self._sender.send(request)
if self._sender.is_constant_read():
if not call_receive:
# TODO This will be slightly troublesome if we allow
# switching between constant read or not on the fly.
# Must also watch out for calling .read() from two places,
# in which case a Lock would be required for .receive().
request.confirm_received.wait() # TODO Optional timeout here?
request.confirm_received.wait() # TODO Socket's timeout here?
else:
while not request.confirm_received.is_set():
self._sender.receive()