Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
fileno_peer = self._context2fileno_peer[context]
try:
sock = context.get_sock()
sock.close()
context.set_sock(None)
except socket.error as error:
log.info(
'failed to close the socket, err_msg:%s' % str(error)
)
except Exception as error:
log.warn('failed to close socket:{0}'.format(error))
try:
self._epoll.unregister(fileno_peer[0])
except Exception as error: # pylint: disable=W0703
log.warn(
'epoll unregister error:%s, peerinfo:%s' %
(str(error), str(fileno_peer[1]))
)
del self._fileno2context[fileno_peer[0]]
del self._peer2context[fileno_peer[1]]
del self._context2fileno_peer[context]
log.info('socket {0} closed successfully'.format(peerinfo))
except Exception as error:
pass
finally:
self._mlock.release()
# pylint: disable=W0212
self._thdpool.add_1job(_cleanup_context, context._send_queue, peerinfo)
listened_peer = context.get_listened_peer()
if listened_peer is not None and (listened_peer in self._peer2context):
log.info(
def connect(self, peer):
"""
:param peer:
ip:port
"""
log.info('to connect to peer:{0}'.format(peer))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._set_sock_params(sock)
try:
ret = sock.connect_ex(peer)
if ret != 0:
log.warn('connect failed, peer:{0}'.format(peer))
return None
if sock.getpeername() == sock.getsockname():
log.warn('connect failed, seems connected to self')
sock.close()
return None
self._set_sock_nonblocking(sock)
return sock
except socket.error as error:
log.warn(
'failed to connect to %s:%s. Error:%s' %
(peer[0], peer[1], str(error))
)
sock.close()
return None
else:
sock.close()
"""
peer = None
try:
if not recv_socket:
peer = msg.get_to_addr()[0]
else:
peer = msg.get_from_addr()[0]
context = self._peer2context.get(peer)
if context is not None:
self.cleanup_error_context(context)
else:
log.warn('conn manager close socket failed:{0}'.format(
peer)
)
except Exception as err:
log.warn('failed to close socket:{1}, recv_socket:{0}'.format(
recv_socket, err)
)
return
job = self._jobqueue.get()
while job is not self._WORKER_STOP_SIGN:
with self._worker_state(self._working, current_thd):
# pylint: disable=W0621
context, function, args, kwargs, result_callback = job
del job
try:
# pylint: disable=W0142
result = _CONTEXT_TRACKER.call_with_context(
context, function, *args, **kwargs
)
success = True
except Exception as error:
success = False
log.warn(
'Func failed, func:{0}, error_msg: {1}'.format(
function, error)
)
if result_callback is None:
log.warn('This func does not have callback.')
_CONTEXT_TRACKER.call_with_context(
context, self._log_err_context, context
)
result = None
else:
result = error
del function, args, kwargs
# when out of "with scope",
# the self._working will remove the thread from
# its self._working list
if sock is not None:
context = sockcontext.CConnContext()
context.set_conn_man(self)
context.set_sock(sock)
context.set_peerinfo(peer)
fileno = sock.fileno()
self._peer2context[peer] = context
self._fileno2context[fileno] = context
self._context2fileno_peer[context] = (fileno, peer)
ret = 0
try:
self._epoll.register(
sock.fileno(), self._epoll_write_params()
)
except Exception as error: # pylint: disable=W0703
log.warn(
'failed to register the socket fileno, err_msg:%s,'
'perinfo:%s:%s. To epoll modify it' %
(str(error), peer[0], peer[1])
)
self._epoll.modify(
sock.fileno(), self._epoll_write_params()
)
else:
log.error(
'failed to post msg. Connect failed. peer info:{0}.'
' msg_type:{1}'.format(
str(peer), msg.get_msg_type()
)
)
ret = -1
else:
order_keys = _dict.get_ordered_keys()
except AttributeError:
order_keys = sorted(
_dict.keys(), lambda x, y: self._comp_write_keys(
_dict[x], _dict[y]
)
)
if '$include' in order_keys:
for filepath in _dict['$include']:
self._str += '$include "{0}"{1}'.format(
filepath, self._get_linesep()
)
order_keys.remove('$include')
for key in order_keys:
if key == '$include':
cup.log.warn('cup.conf does not support $include writeback yet')
continue
try:
item = _dict.get_ex(key)
value = item[0]
comments = item[1][1]
except AttributeError:
value = _dict.get(key)
comments = []
for comment in comments:
self._str += self._get_indents() + comment
if isinstance(value, tuple) or isinstance(value, list):
if isinstance(value, tuple):
print('its a tuple, key:{0}, value:{1}'.format(key, value))
if len(value) > 0 and isinstance(value[0], dict):
# items are all arrays
# [..@section]
peerinfo = context.get_peerinfo()
log.info(
'handle socket reset by peer, to close the socket:%s:%s' %
(peerinfo[0], peerinfo[1])
)
fileno_peer = self._context2fileno_peer[context]
try:
sock = context.get_sock()
sock.close()
context.set_sock(None)
except socket.error as error:
log.info(
'failed to close the socket, err_msg:%s' % str(error)
)
except Exception as error:
log.warn('failed to close socket:{0}'.format(error))
try:
self._epoll.unregister(fileno_peer[0])
except Exception as error: # pylint: disable=W0703
log.warn(
'epoll unregister error:%s, peerinfo:%s' %
(str(error), str(fileno_peer[1]))
)
del self._fileno2context[fileno_peer[0]]
del self._peer2context[fileno_peer[1]]
del self._context2fileno_peer[context]
log.info('socket {0} closed successfully'.format(peerinfo))
except Exception as error:
pass
finally:
self._mlock.release()
# pylint: disable=W0212
def _try_read_one_log(self, stream):
"""
read one log record from the stream_close.
:return:
"""
if stream is None:
return (LOGFILE_EOF, None)
str_datalen = datalen = str_data = None
try:
str_datalen = stream.read(self._record_lenbytes)
if len(str_datalen) == 0:
return (LOGFILE_EOF, None)
if len(str_datalen) < self._record_lenbytes:
log.warn('got a bad log from stream:{0}'.format(stream.name))
return (LOGFILE_BAD_RECORD, None)
datalen = self.convert_bytes2uint(str_datalen)
str_data = stream.read(datalen)
if len(str_data) < datalen:
log.warn(
'got less than data len from stream:{0}'.format(
stream.name)
)
return (LOGFILE_BAD_RECORD, None)
log_id = self.convert_bytes2uint(str_data[0: 16])
log_type = self.convert_bytes2uint(str_data[16: 16 + 2])
log_mode = self.convert_bytes2uint(str_data[18: 18 + 2])
log_binary = str_data[20:]
return (
LOGFILE_GOOD, LogRecord(
datalen, log_id, log_type, log_mode, log_binary