Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
from bases.FrameworkServices.SimpleService import SimpleService


DEFAULT_CONNECT_TIMEOUT = 2.0
DEFAULT_READ_TIMEOUT = 2.0
DEFAULT_WRITE_TIMEOUT = 2.0


class SocketService(SimpleService):
def __init__(self, configuration=None, name=None):
self._sock = None
Expand All @@ -31,6 +36,9 @@ def __init__(self, configuration=None, name=None):
self.__socket_config = None
self.__empty_request = "".encode()
SimpleService.__init__(self, configuration=configuration, name=name)
self.connect_timeout = configuration.get('connect_timeout', DEFAULT_CONNECT_TIMEOUT)
self.read_timeout = configuration.get('read_timeout', DEFAULT_READ_TIMEOUT)
self.write_timeout = configuration.get('write_timeout', DEFAULT_WRITE_TIMEOUT)

def _socket_error(self, message=None):
if self.unix_socket is not None:
Expand Down Expand Up @@ -86,6 +94,8 @@ def _connect2socket(self, res=None):

try:
self.debug('connecting socket to "{address}", port {port}'.format(address=sa[0], port=sa[1]))
self._sock.settimeout(self.connect_timeout)
self.debug('set socket connect timeout to: {0}'.format(self._sock.gettimeout()))
self._sock.connect(sa)
except (socket.error, ssl.SSLError) as error:
self.error('Failed to connect to "{address}", port {port}, error: {error}'.format(address=sa[0],
Expand All @@ -111,6 +121,8 @@ def _connect2unixsocket(self):
try:
self.debug('attempting DGRAM unix socket "{0}"'.format(self.unix_socket))
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self._sock.settimeout(self.connect_timeout)
self.debug('set socket connect timeout to: {0}'.format(self._sock.gettimeout()))
self._sock.connect(self.unix_socket)
self.debug('connected DGRAM unix socket "{0}"'.format(self.unix_socket))
return True
Expand All @@ -121,6 +133,8 @@ def _connect2unixsocket(self):
try:
self.debug('attempting STREAM unix socket "{0}"'.format(self.unix_socket))
self._sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self._sock.settimeout(self.connect_timeout)
self.debug('set socket connect timeout to: {0}'.format(self._sock.gettimeout()))
self._sock.connect(self.unix_socket)
self.debug('connected STREAM unix socket "{0}"'.format(self.unix_socket))
return True
Expand Down Expand Up @@ -156,11 +170,6 @@ def _connect(self):
self._sock = None
self.__socket_config = None

if self._sock is not None:
self._sock.setblocking(0)
self._sock.settimeout(5)
self.debug('set socket timeout to: {0}'.format(self._sock.gettimeout()))

def _disconnect(self):
"""
Close socket connection
Expand All @@ -183,6 +192,8 @@ def _send(self, request=None):
# Send request if it is needed
if self.request != self.__empty_request:
try:
self.debug('set socket write timeout to: {0}'.format(self._sock.gettimeout()))
self._sock.settimeout(self.write_timeout)
self.debug('sending request: {0}'.format(request or self.request))
self._sock.send(request or self.request)
except Exception as error:
Expand All @@ -203,6 +214,8 @@ def _receive(self, raw=False):
while True:
self.debug('receiving response')
try:
self.debug('set socket read timeout to: {0}'.format(self._sock.gettimeout()))
self._sock.settimeout(self.read_timeout)
buf = self._sock.recv(4096)
except Exception as error:
self._socket_error('failed to receive response: {0}'.format(error))
Expand Down