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: 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:
self.write_buf = bytearray()
self.state = "close"
asyncore.dispatcher.close(self)
self.close()

View File

@ -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)

View File

@ -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:

View File

@ -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):

View File

@ -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):

View File

@ -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):