Asyncore fixes

- TCP fixes
This commit is contained in:
Peter Šurda 2017-05-27 21:52:56 +02:00
parent 99e714c432
commit 21f6d38ec2
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
5 changed files with 15 additions and 60 deletions

View File

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

View File

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

View File

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

View File

@ -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)
except socket.error as e:
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()
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):
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):

View File

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