From 391d40d78bd980db75caecb156496f7ce045c08a Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Thu, 19 Oct 2017 09:08:05 +0200 Subject: [PATCH] Socket closing changes - closing reason moved to a variable - actual closing now done in asyncore thread instead of receivedata thread --- src/network/advanceddispatcher.py | 2 +- src/network/bmproto.py | 13 +++++++------ src/network/connectionpool.py | 5 ++++- src/network/socks4a.py | 3 ++- src/network/socks5.py | 3 ++- src/network/tcp.py | 4 ++-- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 97481238..2115c454 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -126,4 +126,4 @@ class AdvancedDispatcher(asyncore.dispatcher): with self.writeLock: self.write_buf = bytearray() self.state = "close" - asyncore.dispatcher.close(self) + self.close() diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 3ae7b635..d66d8c4b 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -1,9 +1,9 @@ import base64 import hashlib -import time import random import socket import struct +import time from bmconfigparser import BMConfigParser from debug import logger @@ -122,7 +122,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker): else: #print "Skipping command %s due to invalid data" % (self.command) logger.debug("Closing due to invalid command %s", self.command) - self.handle_close("Invalid command %s" % (self.command)) + self.close_reason = "Invalid command %s" % (self.command) + self.set_state("close") return False if retval: self.set_state("bm_header", length=self.payloadLength) @@ -538,13 +539,13 @@ class BMProto(AdvancedDispatcher, ObjectTracker): except KeyError: pass - def handle_close(self, reason=None): + def handle_close(self): self.set_state("close") - if reason is None: + try: + logger.debug("%s:%i: closing, %s", self.destination.host, self.destination.port, self.close_reason) + except AttributeError: try: logger.debug("%s:%i: closing", self.destination.host, self.destination.port) except AttributeError: logger.debug("Disconnected socket closing") - else: - logger.debug("%s:%i: closing, %s", self.destination.host, self.destination.port, reason) AdvancedDispatcher.handle_close(self) diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 7ae8afd9..1e50eb1c 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -247,8 +247,11 @@ class BMConnectionPool(object): if i.fullyEstablished: i.append_write_buf(protocol.CreatePacket('ping')) else: - i.handle_close("Timeout (%is)" % (time.time() - i.lastTx)) + i.close_reason = "Timeout (%is)" % (time.time() - i.lastTx) + i.handle_close() for i in self.inboundConnections.values() + self.outboundConnections.values() + self.listeningSockets.values() + self.udpSockets.values(): + if i.state == "close": + i.handle_close() if not (i.accepting or i.connecting or i.connected): reaper.append(i) for i in reaper: diff --git a/src/network/socks4a.py b/src/network/socks4a.py index d6cf2ad8..978ede04 100644 --- a/src/network/socks4a.py +++ b/src/network/socks4a.py @@ -86,7 +86,8 @@ class Socks4aConnection(Socks4a): try: return Socks4a.state_pre_connect(self) except Socks4aError as e: - self.handle_close(e.message) + self.close_reason = e.message + self.set_state("close") class Socks4aResolver(Socks4a): diff --git a/src/network/socks5.py b/src/network/socks5.py index e57e7c6a..52050ec9 100644 --- a/src/network/socks5.py +++ b/src/network/socks5.py @@ -154,7 +154,8 @@ class Socks5Connection(Socks5): try: return Socks5.state_pre_connect(self) except Socks5Error as e: - self.handle_close(e.message) + self.close_reason = e.message + self.set_state("close") class Socks5Resolver(Socks5): diff --git a/src/network/tcp.py b/src/network/tcp.py index dd813ac6..ab282fb4 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -213,12 +213,12 @@ class TCPConnection(BMProto, TLSDispatcher): def handle_write(self): TLSDispatcher.handle_write(self) - def handle_close(self, reason=None): + def handle_close(self): if self.isOutbound and not self.fullyEstablished: knownnodes.decreaseRating(self.destination) - BMProto.handle_close(self, reason) if self.fullyEstablished: UISignalQueue.put(('updateNetworkStatusTab', (self.isOutbound, False, self.destination))) + BMProto.handle_close(self) class Socks5BMConnection(Socks5Connection, TCPConnection):