Socket closing changes

- closing reason moved to a variable
- actual closing now done in asyncore thread instead of
receivedata thread
This commit is contained in:
Peter Šurda 2017-10-19 09:08:05 +02:00
parent 01d46c30e4
commit 391d40d78b
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
6 changed files with 18 additions and 12 deletions

View File

@ -126,4 +126,4 @@ class AdvancedDispatcher(asyncore.dispatcher):
with self.writeLock: with self.writeLock:
self.write_buf = bytearray() self.write_buf = bytearray()
self.state = "close" self.state = "close"
asyncore.dispatcher.close(self) self.close()

View File

@ -1,9 +1,9 @@
import base64 import base64
import hashlib import hashlib
import time
import random import random
import socket import socket
import struct import struct
import time
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from debug import logger from debug import logger
@ -122,7 +122,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
else: else:
#print "Skipping command %s due to invalid data" % (self.command) #print "Skipping command %s due to invalid data" % (self.command)
logger.debug("Closing due to invalid command %s", 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 return False
if retval: if retval:
self.set_state("bm_header", length=self.payloadLength) self.set_state("bm_header", length=self.payloadLength)
@ -538,13 +539,13 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
except KeyError: except KeyError:
pass pass
def handle_close(self, reason=None): def handle_close(self):
self.set_state("close") 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: try:
logger.debug("%s:%i: closing", self.destination.host, self.destination.port) logger.debug("%s:%i: closing", self.destination.host, self.destination.port)
except AttributeError: except AttributeError:
logger.debug("Disconnected socket closing") logger.debug("Disconnected socket closing")
else:
logger.debug("%s:%i: closing, %s", self.destination.host, self.destination.port, reason)
AdvancedDispatcher.handle_close(self) AdvancedDispatcher.handle_close(self)

View File

@ -247,8 +247,11 @@ class BMConnectionPool(object):
if i.fullyEstablished: if i.fullyEstablished:
i.append_write_buf(protocol.CreatePacket('ping')) i.append_write_buf(protocol.CreatePacket('ping'))
else: 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(): 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): if not (i.accepting or i.connecting or i.connected):
reaper.append(i) reaper.append(i)
for i in reaper: for i in reaper:

View File

@ -86,7 +86,8 @@ class Socks4aConnection(Socks4a):
try: try:
return Socks4a.state_pre_connect(self) return Socks4a.state_pre_connect(self)
except Socks4aError as e: except Socks4aError as e:
self.handle_close(e.message) self.close_reason = e.message
self.set_state("close")
class Socks4aResolver(Socks4a): class Socks4aResolver(Socks4a):

View File

@ -154,7 +154,8 @@ class Socks5Connection(Socks5):
try: try:
return Socks5.state_pre_connect(self) return Socks5.state_pre_connect(self)
except Socks5Error as e: except Socks5Error as e:
self.handle_close(e.message) self.close_reason = e.message
self.set_state("close")
class Socks5Resolver(Socks5): class Socks5Resolver(Socks5):

View File

@ -213,12 +213,12 @@ class TCPConnection(BMProto, TLSDispatcher):
def handle_write(self): def handle_write(self):
TLSDispatcher.handle_write(self) TLSDispatcher.handle_write(self)
def handle_close(self, reason=None): def handle_close(self):
if self.isOutbound and not self.fullyEstablished: if self.isOutbound and not self.fullyEstablished:
knownnodes.decreaseRating(self.destination) knownnodes.decreaseRating(self.destination)
BMProto.handle_close(self, reason)
if self.fullyEstablished: if self.fullyEstablished:
UISignalQueue.put(('updateNetworkStatusTab', (self.isOutbound, False, self.destination))) UISignalQueue.put(('updateNetworkStatusTab', (self.isOutbound, False, self.destination)))
BMProto.handle_close(self)
class Socks5BMConnection(Socks5Connection, TCPConnection): class Socks5BMConnection(Socks5Connection, TCPConnection):