From fa56ab3e6fb73c45707c70a9ed0f09e44d928f7a Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Wed, 24 May 2017 21:15:36 +0200 Subject: [PATCH] Asyncore update - better error handling - bug fixes - remove some debug output --- src/network/asyncore_pollchoose.py | 17 +++++++++-------- src/network/bmproto.py | 10 +++++++--- src/network/tls.py | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/network/asyncore_pollchoose.py b/src/network/asyncore_pollchoose.py index b26d4cab..72a829ce 100644 --- a/src/network/asyncore_pollchoose.py +++ b/src/network/asyncore_pollchoose.py @@ -57,6 +57,7 @@ import warnings import os from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ + ECONNREFUSED, \ errorcode try: from errno import WSAEWOULDBLOCK @@ -65,7 +66,7 @@ except: from ssl import SSLError, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, - EBADF)) + EBADF, ECONNREFUSED)) OP_READ = 1 OP_WRITE = 2 @@ -530,7 +531,7 @@ class dispatcher: except TypeError: return None except socket.error as why: - if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN): + if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN, ENOTCONN): return None else: raise @@ -547,11 +548,11 @@ class dispatcher: else: raise except socket.error as why: - if why.errno in (errno.EAGAIN, errno.EWOULDBLOCK) or \ + if why.args[0] in (EAGAIN, EWOULDBLOCK) or \ (sys.platform.startswith('win') and \ - err.errno == errno.WSAEWOULDBLOCK): + err.errno == WSAEWOULDBLOCK): return 0 - elif why.errno in _DISCONNECTED: + elif why.args[0] in _DISCONNECTED: self.handle_close() return 0 else: @@ -574,11 +575,11 @@ class dispatcher: raise except socket.error as why: # winsock sometimes raises ENOTCONN - if why.errno in (errno.EAGAIN, errno.EWOULDBLOCK) or \ + if why.args[0] in (EAGAIN, EWOULDBLOCK) or \ (sys.platform.startswith('win') and \ - err.errno == errno.WSAEWOULDBLOCK): + err.errno == WSAEWOULDBLOCK): return b'' - if why.errno in _DISCONNECTED: + if why.args[0] in _DISCONNECTED: self.handle_close() return b'' else: diff --git a/src/network/bmproto.py b/src/network/bmproto.py index d9ed2a95..9a697b13 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -289,7 +289,7 @@ class BMConnection(TLSDispatcher, BMQueues): # print "skipping getdata" # return True for i in items: - print "received getdata request for item %s" % (hexlify(i)) + #print "received getdata request for item %s" % (hexlify(i)) #logger.debug('received getdata request for item:' + hexlify(i)) #if i in ObjUploadQueue.streamElems(1): if False: @@ -309,7 +309,8 @@ class BMConnection(TLSDispatcher, BMQueues): logger.error("Too many items in inv message!") raise BMProtoExcessiveDataError() else: - print "items in inv: %i" % (len(items)) + pass + #print "items in inv: %i" % (len(items)) startTime = time.time() #advertisedSet = set() @@ -609,7 +610,10 @@ class BMConnection(TLSDispatcher, BMQueues): else: print "%s:%i: closing, %s" % (self.destination.host, self.destination.port, reason) network.connectionpool.BMConnectionPool().removeConnection(self) - asyncore.dispatcher.close(self) + try: + asyncore.dispatcher.close(self) + except AttributeError: + pass class Socks5BMConnection(Socks5Connection, BMConnection): diff --git a/src/network/tls.py b/src/network/tls.py index 669d9aa3..e691006d 100644 --- a/src/network/tls.py +++ b/src/network/tls.py @@ -110,7 +110,7 @@ class TLSDispatcher(AdvancedDispatcher): if not (self.want_write or self.want_read): raise else: - print "%s:%i: handshake success" % (self.destination.host, self.destination.port) + print "%s:%i: TLS handshake success%s" % (self.destination.host, self.destination.port, ", TLS protocol version: %s" % (self.sslSocket.version()) if sys.version_info >= (2, 7, 9) else "") # The handshake has completed, so remove this channel and... self.del_channel() self.set_socket(self.sslSocket)