Skip to content

Commit

Permalink
Asyncore fixes
Browse files Browse the repository at this point in the history
- TCP fixes
  • Loading branch information
PeterSurda committed May 27, 2017
1 parent 99e714c commit 21f6d38
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 61 deletions.
4 changes: 2 additions & 2 deletions src/network/advanceddispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def read_buf_sufficient(self, length=0):
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
Expand All @@ -54,7 +54,7 @@ def set_state(self, state, length=0):
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
Expand Down
4 changes: 2 additions & 2 deletions src/network/asyncore_pollchoose.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
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
Expand All @@ -66,7 +66,7 @@
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
Expand Down
22 changes: 0 additions & 22 deletions src/network/bmproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,28 +469,6 @@ def assembleAddr(peerList):
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:
Expand Down
35 changes: 11 additions & 24 deletions src/network/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,8 @@ def __init__(self, address=None, sock=None):
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
Expand Down Expand Up @@ -148,35 +142,28 @@ def sendBigInv(self):
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):
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(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):
Expand Down
12 changes: 1 addition & 11 deletions src/network/udp.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def bm_command_verack(self):
def bm_command_version(self):
return True

def handle_connect_event(self):
def handle_connect(self):
return

def writable(self):
Expand Down Expand Up @@ -168,16 +168,6 @@ def handle_write(self):
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
Expand Down

0 comments on commit 21f6d38

Please sign in to comment.