Add logging calls on the TcpClient

This commit is contained in:
Lonami Exo 2018-02-26 13:41:07 +01:00
parent 9604161c91
commit 6f16aeb553

View File

@ -2,6 +2,7 @@
This module holds a rough implementation of the C# TCP client. This module holds a rough implementation of the C# TCP client.
""" """
import errno import errno
import logging
import socket import socket
import time import time
from datetime import timedelta from datetime import timedelta
@ -19,6 +20,8 @@ CONN_RESET_ERRNOS = {
errno.EINVAL, errno.ENOTCONN errno.EINVAL, errno.ENOTCONN
} }
__log__ = logging.getLogger(__name__)
class TcpClient: class TcpClient:
"""A simple TCP client to ease the work with sockets and proxies.""" """A simple TCP client to ease the work with sockets and proxies."""
@ -75,6 +78,7 @@ class TcpClient:
self._socket.connect(address) self._socket.connect(address)
break # Successful connection, stop retrying to connect break # Successful connection, stop retrying to connect
except OSError as e: except OSError as e:
__log__.info('OSError "%s" raised while connecting', e)
# Stop retrying to connect if proxy connection error occurred # Stop retrying to connect if proxy connection error occurred
if socks and isinstance(e, socks.ProxyConnectionError): if socks and isinstance(e, socks.ProxyConnectionError):
raise raise
@ -120,19 +124,22 @@ class TcpClient:
:param data: the data to send. :param data: the data to send.
""" """
if self._socket is None: if self._socket is None:
self._raise_connection_reset() self._raise_connection_reset(None)
# TODO Timeout may be an issue when sending the data, Changed in v3.5: # TODO Timeout may be an issue when sending the data, Changed in v3.5:
# The socket timeout is now the maximum total duration to send all data. # The socket timeout is now the maximum total duration to send all data.
try: try:
self._socket.sendall(data) self._socket.sendall(data)
except socket.timeout as e: except socket.timeout as e:
__log__.debug('socket.timeout "%s" while writing data', e)
raise TimeoutError() from e raise TimeoutError() from e
except ConnectionError: except ConnectionError as e:
self._raise_connection_reset() __log__.info('ConnectionError "%s" while writing data', e)
self._raise_connection_reset(e)
except OSError as e: except OSError as e:
__log__.info('OSError "%s" while writing data', e)
if e.errno in CONN_RESET_ERRNOS: if e.errno in CONN_RESET_ERRNOS:
self._raise_connection_reset() self._raise_connection_reset(e)
else: else:
raise raise
@ -144,7 +151,7 @@ class TcpClient:
:return: the read data with len(data) == size. :return: the read data with len(data) == size.
""" """
if self._socket is None: if self._socket is None:
self._raise_connection_reset() self._raise_connection_reset(None)
# TODO Remove the timeout from this method, always use previous one # TODO Remove the timeout from this method, always use previous one
with BufferedWriter(BytesIO(), buffer_size=size) as buffer: with BufferedWriter(BytesIO(), buffer_size=size) as buffer:
@ -153,17 +160,22 @@ class TcpClient:
try: try:
partial = self._socket.recv(bytes_left) partial = self._socket.recv(bytes_left)
except socket.timeout as e: except socket.timeout as e:
# These are somewhat common if the server has nothing
# to send to us, so use a lower logging priority.
__log__.debug('socket.timeout "%s" while reading data', e)
raise TimeoutError() from e raise TimeoutError() from e
except ConnectionError: except ConnectionError as e:
self._raise_connection_reset() __log__.info('ConnectionError "%s" while reading data', e)
self._raise_connection_reset(e)
except OSError as e: except OSError as e:
__log__.info('OSError "%s" while reading data', e)
if e.errno in CONN_RESET_ERRNOS: if e.errno in CONN_RESET_ERRNOS:
self._raise_connection_reset() self._raise_connection_reset(e)
else: else:
raise raise
if len(partial) == 0: if len(partial) == 0:
self._raise_connection_reset() self._raise_connection_reset(None)
buffer.write(partial) buffer.write(partial)
bytes_left -= len(partial) bytes_left -= len(partial)
@ -172,7 +184,8 @@ class TcpClient:
buffer.flush() buffer.flush()
return buffer.raw.getvalue() return buffer.raw.getvalue()
def _raise_connection_reset(self): def _raise_connection_reset(self, original):
"""Disconnects the client and raises ConnectionResetError.""" """Disconnects the client and raises ConnectionResetError."""
self.close() # Connection reset -> flag as socket closed self.close() # Connection reset -> flag as socket closed
raise ConnectionResetError('The server has closed the connection.') raise ConnectionResetError('The server has closed the connection.')\
from original