From e8d9a7f183e6ce409f6e28b6948a8ea28c90e6bf Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sat, 3 Jun 2017 16:30:05 +0200 Subject: [PATCH] Asyncore connect handling - minor improvements in handling of connect events so that it's not processed twice --- src/network/advanceddispatcher.py | 8 ++++++++ src/network/asyncore_pollchoose.py | 4 ++-- src/network/tcp.py | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 89a7423d..dadb625b 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -1,3 +1,4 @@ +import socket import Queue import time @@ -99,6 +100,13 @@ class AdvancedDispatcher(asyncore.dispatcher): self.sentBytes += written self.slice_write_buf(written) + def handle_connect_event(self): + try: + asyncore.dispatcher.handle_connect_event(self) + except socket.error as e: + if e.args[0] not in asyncore._DISCONNECTED: + raise + def handle_connect(self): self.lastTx = time.time() self.process() diff --git a/src/network/asyncore_pollchoose.py b/src/network/asyncore_pollchoose.py index ae18e95e..8131b2b2 100644 --- a/src/network/asyncore_pollchoose.py +++ b/src/network/asyncore_pollchoose.py @@ -57,7 +57,7 @@ import warnings import os from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ ENOTCONN, ESHUTDOWN, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ - ECONNREFUSED, EHOSTUNREACH, ENOTSOCK, \ + ECONNREFUSED, EHOSTUNREACH, ENETUNREACH, ENOTSOCK, \ errorcode try: from errno import WSAEWOULDBLOCK @@ -71,7 +71,7 @@ except (ImportError, AttributeError): from ssl import SSLError, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, - EBADF, ECONNREFUSED, EHOSTUNREACH)) + EBADF, ECONNREFUSED, EHOSTUNREACH, ENETUNREACH)) OP_READ = 1 OP_WRITE = 2 diff --git a/src/network/tcp.py b/src/network/tcp.py index d3b2f862..986a4b63 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -147,9 +147,9 @@ class TCPConnection(BMProto, TLSDispatcher): def sendBigInv(self): self.receiveQueue.put(("biginv", None)) - def handle_connect_event(self): + def handle_connect(self): try: - AdvancedDispatcher.handle_connect_event(self) + AdvancedDispatcher.handle_connect(self) except socket.error as e: if e.errno in asyncore._DISCONNECTED: logger.debug("%s:%i: Connection failed: %s" % (self.destination.host, self.destination.port, str(e)))