Fix closed sockets not being recreated as they should

See http://stackoverflow.com/a/15958099
This commit is contained in:
Lonami Exo 2017-05-20 11:34:23 +02:00
parent 468033fa7e
commit 6550d83d42
2 changed files with 19 additions and 16 deletions

View File

@ -11,26 +11,29 @@ from telethon.utils import BinaryWriter
class TcpClient:
def __init__(self, proxy=None):
self.connected = False
if proxy:
try:
import socks
self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.set_proxy(*proxy)
except (ImportError, SystemError):
print("Can't import PySocks, fallback to vanilla socket. "
"Proxy settings are ignored. "
"Try to install PySocks via pip")
proxy = None
if not proxy:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.proxy = proxy
self._recreate_socket()
# Support for multi-threading advantages and safety
self.cancelled = Event() # Has the read operation been cancelled?
self.delay = 0.1 # Read delay when there was no data available
self.lock = Lock()
def _recreate_socket(self):
self.socket = None
if self.proxy:
try:
import socks
self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.set_proxy(*self.proxy)
except (ImportError, SystemError):
print("Can't import PySocks, fallback to vanilla socket. "
"Proxy settings are ignored. "
"Try to install PySocks via pip")
if not self.socket:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, ip, port):
"""Connects to the specified IP and port number"""
if not self.connected:
@ -42,6 +45,7 @@ class TcpClient:
if self.connected:
self.socket.close()
self.connected = False
self._recreate_socket()
def write(self, data):
"""Writes (sends) the specified bytes to the connected peer"""

View File

@ -64,8 +64,7 @@ class TcpTransport:
return seq, body
def close(self):
if self.tcp_client.connected:
self.tcp_client.close()
self.tcp_client.close()
def cancel_receive(self):
"""Cancels (stops) trying to receive from the