From 9477c75fcefb587b17ae237dc0a5334c2148b956 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 6 Jun 2018 21:42:48 +0200 Subject: [PATCH] Fix basic requests sending and receiving --- telethon/extensions/tcp_client.py | 7 +- telethon/network/connection/tcpfull.py | 13 +-- telethon/network/mtprotosender.py | 117 +++++++++++++++++++------ 3 files changed, 100 insertions(+), 37 deletions(-) diff --git a/telethon/extensions/tcp_client.py b/telethon/extensions/tcp_client.py index a12d33a9..8251c5d0 100644 --- a/telethon/extensions/tcp_client.py +++ b/telethon/extensions/tcp_client.py @@ -73,12 +73,13 @@ class TcpClient: if self._socket is None: self._socket = self._create_socket(mode, self.proxy) - asyncio.wait_for(self._loop.sock_connect(self._socket, address), - self.timeout, loop=self._loop) + await asyncio.wait_for(self._loop.sock_connect(self._socket, address), + self.timeout, loop=self._loop) @property def is_connected(self): """Determines whether the client is connected or not.""" + # TODO fileno() is >= 0 even before calling sock_connect! return self._socket is not None and self._socket.fileno() >= 0 def close(self): @@ -123,7 +124,7 @@ class TcpClient: timeout=self.timeout, loop=self._loop ) - if not partial == 0: + if not partial: raise ConnectionResetError() buffer.write(partial) diff --git a/telethon/network/connection/tcpfull.py b/telethon/network/connection/tcpfull.py index a583a242..61ea673d 100644 --- a/telethon/network/connection/tcpfull.py +++ b/telethon/network/connection/tcpfull.py @@ -22,7 +22,7 @@ class ConnectionTcpFull(Connection): async def connect(self, ip, port): try: - self.conn.connect(ip, port) + await self.conn.connect(ip, port) except OSError as e: if e.errno == errno.EISCONN: return # Already connected, no need to re-set everything up @@ -35,7 +35,7 @@ class ConnectionTcpFull(Connection): return self.conn.timeout def is_connected(self): - return self.conn.connected + return self.conn.is_connected async def close(self): self.conn.close() @@ -44,10 +44,11 @@ class ConnectionTcpFull(Connection): return ConnectionTcpFull(self._proxy, self._timeout) async def recv(self): - packet_len_seq = self.read(8) # 4 and 4 + packet_len_seq = await self.read(8) # 4 and 4 packet_len, seq = struct.unpack('