Asyncore updates

- fix crash in inv thread
- more prints changed into logger
- minor fixes
This commit is contained in:
Peter Šurda 2017-05-29 00:47:41 +02:00
parent c85d52b8e8
commit 65bb6648e7
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
4 changed files with 10 additions and 16 deletions

View File

@ -97,7 +97,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
logger.debug("Bad checksum, ignoring") logger.debug("Bad checksum, ignoring")
self.invalid = True self.invalid = True
retval = True retval = True
if not self.fullyEstablished and self.command not in ("version", "verack"): if not self.fullyEstablished and self.command not in ("error", "version", "verack"):
logger.error("Received command %s before connection was fully established, ignoring", self.command) logger.error("Received command %s before connection was fully established, ignoring", self.command)
self.invalid = True self.invalid = True
if not self.invalid: if not self.invalid:

View File

@ -51,18 +51,16 @@ class InvThread(threading.Thread, StoppableThread):
continue continue
if len(hashes) > 0: if len(hashes) > 0:
connection.writeQueue.put(protocol.CreatePacket('inv', addresses.encodeVarint(len(hashes)) + b"".join(hashes))) connection.writeQueue.put(protocol.CreatePacket('inv', addresses.encodeVarint(len(hashes)) + b"".join(hashes)))
self.collectionOfInvs[iterator] = [] self.collectionOfInvs[iterator] = {}
iterator += 1 iterator += 1
iterator %= InvThread.size iterator %= InvThread.size
self.stop.wait(1) self.stop.wait(1)
def holdHash(self, stream, hash): def holdHash(self, stream, hash):
iter = random.randrange(0, InvThread.size) i = random.randrange(0, InvThread.size)
try: if stream not in self.collectionOfInvs[i]:
self.collectionOfInvs[iter][stream].append(hash) self.collectionOfInvs[i][stream] = []
except KeyError, IndexError: self.collectionOfInvs[i][stream].append(hash)
self.collectionOfInvs[iter][stream] = []
self.collectionOfInvs[iter][stream].append(hash)
def hasHash(self, hash): def hasHash(self, hash):
for streamlist in self.collectionOfInvs: for streamlist in self.collectionOfInvs:

View File

@ -19,7 +19,6 @@ class BMNetworkThread(threading.Thread, StoppableThread):
BMConnectionPool().loop() BMConnectionPool().loop()
def stopThread(self): def stopThread(self):
super(BMNetworkThread, self).stopThread()
for i in BMConnectionPool().listeningSockets: for i in BMConnectionPool().listeningSockets:
try: try:
i.close() i.close()
@ -38,3 +37,4 @@ class BMNetworkThread(threading.Thread, StoppableThread):
# just in case # just in case
asyncore.close_all() asyncore.close_all()
super(BMNetworkThread, self).stopThread()

View File

@ -3,7 +3,6 @@ from binascii import hexlify
import hashlib import hashlib
import math import math
import time import time
from pprint import pprint
import socket import socket
import struct import struct
import random import random
@ -107,7 +106,7 @@ class UDPSocket(BMProto):
remoteport = port remoteport = port
if remoteport is False: if remoteport is False:
return return
print "received peer discovery from %s:%i (port %i):" % (self.destination.host, self.destination.port, remoteport) logger.debug("received peer discovery from %s:%i (port %i):", self.destination.host, self.destination.port, remoteport)
if self.local: if self.local:
peerDiscoveryQueue.put(state.Peer(self.destination.host, remoteport)) peerDiscoveryQueue.put(state.Peer(self.destination.host, remoteport))
return True return True
@ -140,7 +139,7 @@ class UDPSocket(BMProto):
try: try:
(recdata, addr) = self.socket.recvfrom(AdvancedDispatcher._buf_len) (recdata, addr) = self.socket.recvfrom(AdvancedDispatcher._buf_len)
except socket.error as e: except socket.error as e:
print "socket error: %s" % (str(e)) logger.error("socket error: %s", str(e))
return return
self.destination = state.Peer(addr[0], addr[1]) self.destination = state.Peer(addr[0], addr[1])
@ -149,23 +148,20 @@ class UDPSocket(BMProto):
self.local = True self.local = True
else: else:
self.local = False self.local = False
print "read %ib" % (len(recdata))
# overwrite the old buffer to avoid mixing data and so that self.local works correctly # overwrite the old buffer to avoid mixing data and so that self.local works correctly
self.read_buf = recdata self.read_buf = recdata
self.bm_proto_reset() self.bm_proto_reset()
self.process() self.process()
def handle_write(self): def handle_write(self):
# print "handling write"
try: try:
data = self.writeQueue.get(False) data = self.writeQueue.get(False)
except Queue.Empty: except Queue.Empty:
return return
try: try:
retval = self.socket.sendto(data, ('<broadcast>', UDPSocket.port)) retval = self.socket.sendto(data, ('<broadcast>', UDPSocket.port))
#print "broadcasted %ib" % (retval)
except socket.error as e: except socket.error as e:
print "socket error on sendato: %s" % (e) logger.error("socket error on sendato: %s", str(e))
self.writeQueue.task_done() self.writeQueue.task_done()