From 21f6d38ec260ea1d8209da014977a9ba93e17916 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sat, 27 May 2017 21:52:56 +0200 Subject: [PATCH] Asyncore fixes - TCP fixes --- src/network/advanceddispatcher.py | 4 ++-- src/network/asyncore_pollchoose.py | 4 ++-- src/network/bmproto.py | 22 -------------------- src/network/tcp.py | 33 +++++++++--------------------- src/network/udp.py | 12 +---------- 5 files changed, 15 insertions(+), 60 deletions(-) diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 947824bd..8c976b36 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -34,7 +34,7 @@ class AdvancedDispatcher(asyncore.dispatcher): return True def process(self): - if self.state not in ["init", "tls_handshake"] and len(self.read_buf) == 0: + if self.state != "tls_handshake" and len(self.read_buf) == 0: return if not self.connected: return @@ -54,7 +54,7 @@ class AdvancedDispatcher(asyncore.dispatcher): self.state = state def writable(self): - return self.connecting or len(self.write_buf) > 0 or not self.writeQueue.empty() + return self.connected and (len(self.write_buf) > 0 or not self.writeQueue.empty()) def readable(self): return self.connecting or len(self.read_buf) < AdvancedDispatcher._buf_len diff --git a/src/network/asyncore_pollchoose.py b/src/network/asyncore_pollchoose.py index 08ee42d0..dda6d7c2 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, \ + ECONNREFUSED, EHOSTUNREACH, \ errorcode try: from errno import WSAEWOULDBLOCK @@ -66,7 +66,7 @@ except: from ssl import SSLError, SSL_ERROR_WANT_READ, SSL_ERROR_WANT_WRITE _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, - EBADF, ECONNREFUSED)) + EBADF, ECONNREFUSED, EHOSTUNREACH)) OP_READ = 1 OP_WRITE = 2 diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 6e1c3a18..c706b81a 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -469,28 +469,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker): payload += struct.pack('>H', peer.port) # remote port return protocol.CreatePacket('addr', payload) - def handle_connect_event(self): - try: - asyncore.dispatcher.handle_connect_event(self) - self.connectedAt = time.time() - except socket.error as e: - #print "%s:%i: socket error: %s" % (self.destination.host, self.destination.port, str(e)) - self.close() - - def handle_read_event(self): - try: - asyncore.dispatcher.handle_read_event(self) - except socket.error as e: - #print "%s:%i: socket error: %s" % (self.destination.host, self.destination.port, str(e)) - self.close() - - def handle_write_event(self): - try: - asyncore.dispatcher.handle_write_event(self) - except socket.error as e: - #print "%s:%i: socket error: %s" % (self.destination.host, self.destination.port, str(e)) - self.close() - def close(self, reason=None): self.set_state("close") # if reason is None: diff --git a/src/network/tcp.py b/src/network/tcp.py index ef54fc18..8f7e60d7 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -63,14 +63,8 @@ class TCPConnection(BMProto, TLSDispatcher): shared.connectedHostsList[self.destination] = 0 ObjectTracker.__init__(self) UISignalQueue.put(('updateNetworkStatusTab', 'no data')) - - def state_init(self): self.bm_proto_reset() - if self.isOutbound: - self.writeQueue.put(protocol.assembleVersionMessage(self.destination.host, self.destination.port, network.connectionpool.BMConnectionPool().streams, False)) - print "%s:%i: Sending version" % (self.destination.host, self.destination.port) self.set_state("bm_header") - return True def antiIntersectionDelay(self, initial = False): # estimated time for a small object to propagate across the whole network @@ -148,35 +142,28 @@ class TCPConnection(BMProto, TLSDispatcher): def handle_connect_event(self): try: asyncore.dispatcher.handle_connect_event(self) - self.connectedAt = time.time() except socket.error as e: - #print "%s:%i: socket error: %s" % (self.destination.host, self.destination.port, str(e)) - self.close() + if e.errno in asyncore._DISCONNECTED: + self.close("Connection failed") + return + self.writeQueue.put(protocol.assembleVersionMessage(self.destination.host, self.destination.port, network.connectionpool.BMConnectionPool().streams, False)) + #print "%s:%i: Sending version" % (self.destination.host, self.destination.port) + self.connectedAt = time.time() - def handle_read_event(self): + def handle_read(self): try: - asyncore.dispatcher.handle_read_event(self) + AdvancedDispatcher.handle_read(self) except socket.error as e: #print "%s:%i: socket error: %s" % (self.destination.host, self.destination.port, str(e)) self.close() - def handle_write_event(self): + def handle_write(self): try: - asyncore.dispatcher.handle_write_event(self) + AdvancedDispatcher.handle_write(self) except socket.error as e: #print "%s:%i: socket error: %s" % (self.destination.host, self.destination.port, str(e)) self.close() - def close(self, reason=None): - self.set_state("close") -# if reason is None: -# print "%s:%i: closing" % (self.destination.host, self.destination.port) -# #traceback.print_stack() -# else: -# print "%s:%i: closing, %s" % (self.destination.host, self.destination.port, reason) - network.connectionpool.BMConnectionPool().removeConnection(self) - asyncore.dispatcher.close(self) - class Socks5BMConnection(Socks5Connection, TCPConnection): def __init__(self, address): diff --git a/src/network/udp.py b/src/network/udp.py index 42f8bd18..9e687603 100644 --- a/src/network/udp.py +++ b/src/network/udp.py @@ -127,7 +127,7 @@ class UDPSocket(BMProto): def bm_command_version(self): return True - def handle_connect_event(self): + def handle_connect(self): return def writable(self): @@ -168,16 +168,6 @@ class UDPSocket(BMProto): print "socket error on sendato: %s" % (e) self.writeQueue.task_done() - def close(self, reason=None): - self.set_state("close") -# if reason is None: -# print "%s:%i: closing" % (self.destination.host, self.destination.port) -# #traceback.print_stack() -# else: -# print "%s:%i: closing, %s" % (self.destination.host, self.destination.port, reason) - network.connectionpool.BMConnectionPool().removeConnection(self) - asyncore.dispatcher.close(self) - if __name__ == "__main__": # initial fill