better error handling around sock.sendall

This commit is contained in:
Jonathan Warren 2013-05-03 12:24:47 -04:00
parent 73ec3e6293
commit caf9890bd1
2 changed files with 60 additions and 27 deletions

View File

@ -412,7 +412,13 @@ class receiveDataThread(threading.Thread):
def sendpong(self): def sendpong(self):
print 'Sending pong' print 'Sending pong'
try:
self.sock.sendall('\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35') self.sock.sendall('\xE9\xBE\xB4\xD9\x70\x6F\x6E\x67\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35')
except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
def recverack(self): def recverack(self):
print 'verack received' print 'verack received'
@ -501,7 +507,13 @@ class receiveDataThread(threading.Thread):
shared.printLock.acquire() shared.printLock.acquire()
print 'Sending huge inv message with', numberOfObjects, 'objects to just this one peer' print 'Sending huge inv message with', numberOfObjects, 'objects to just this one peer'
shared.printLock.release() shared.printLock.release()
try:
self.sock.sendall(headerData + payload) self.sock.sendall(headerData + payload)
except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
#We have received a broadcast message #We have received a broadcast message
def recbroadcast(self,data): def recbroadcast(self,data):
@ -1519,7 +1531,7 @@ class receiveDataThread(threading.Thread):
except Exception, err: except Exception, err:
#if not 'Bad file descriptor' in err: #if not 'Bad file descriptor' in err:
shared.printLock.acquire() shared.printLock.acquire()
sys.stderr.write('sock.send error: %s\n' % err) sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release() shared.printLock.release()
#We have received a getdata request from our peer #We have received a getdata request from our peer
@ -1553,44 +1565,39 @@ class receiveDataThread(threading.Thread):
#Our peer has requested (in a getdata message) that we send an object. #Our peer has requested (in a getdata message) that we send an object.
def sendData(self,objectType,payload): def sendData(self,objectType,payload):
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
if objectType == 'pubkey': if objectType == 'pubkey':
shared.printLock.acquire() shared.printLock.acquire()
print 'sending pubkey' print 'sending pubkey'
shared.printLock.release() shared.printLock.release()
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData += 'pubkey\x00\x00\x00\x00\x00\x00' headerData += 'pubkey\x00\x00\x00\x00\x00\x00'
headerData += pack('>L',len(payload)) #payload length.
headerData += hashlib.sha512(payload).digest()[:4]
self.sock.sendall(headerData + payload)
elif objectType == 'getpubkey' or objectType == 'pubkeyrequest': elif objectType == 'getpubkey' or objectType == 'pubkeyrequest':
shared.printLock.acquire() shared.printLock.acquire()
print 'sending getpubkey' print 'sending getpubkey'
shared.printLock.release() shared.printLock.release()
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData += 'getpubkey\x00\x00\x00' headerData += 'getpubkey\x00\x00\x00'
headerData += pack('>L',len(payload)) #payload length.
headerData += hashlib.sha512(payload).digest()[:4]
self.sock.sendall(headerData + payload)
elif objectType == 'msg': elif objectType == 'msg':
shared.printLock.acquire() shared.printLock.acquire()
print 'sending msg' print 'sending msg'
shared.printLock.release() shared.printLock.release()
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00' headerData += 'msg\x00\x00\x00\x00\x00\x00\x00\x00\x00'
headerData += pack('>L',len(payload)) #payload length.
headerData += hashlib.sha512(payload).digest()[:4]
self.sock.sendall(headerData + payload)
elif objectType == 'broadcast': elif objectType == 'broadcast':
shared.printLock.acquire() shared.printLock.acquire()
print 'sending broadcast' print 'sending broadcast'
shared.printLock.release() shared.printLock.release()
headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits.
headerData += 'broadcast\x00\x00\x00' headerData += 'broadcast\x00\x00\x00'
headerData += pack('>L',len(payload)) #payload length.
headerData += hashlib.sha512(payload).digest()[:4]
self.sock.sendall(headerData + payload)
else: else:
sys.stderr.write('Error: sendData has been asked to send a strange objectType: %s\n' % str(objectType)) sys.stderr.write('Error: sendData has been asked to send a strange objectType: %s\n' % str(objectType))
return
headerData += pack('>L',len(payload)) #payload length.
headerData += hashlib.sha512(payload).digest()[:4]
try:
self.sock.sendall(headerData + payload)
except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
#Send an inv message with just one hash to all of our peers #Send an inv message with just one hash to all of our peers
def broadcastinv(self,hash): def broadcastinv(self,hash):
@ -1893,12 +1900,17 @@ class receiveDataThread(threading.Thread):
datatosend = datatosend + pack('>L',len(payload)) #payload length datatosend = datatosend + pack('>L',len(payload)) #payload length
datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + hashlib.sha512(payload).digest()[0:4]
datatosend = datatosend + payload datatosend = datatosend + payload
try:
self.sock.sendall(datatosend)
if verbose >= 1: if verbose >= 1:
shared.printLock.acquire() shared.printLock.acquire()
print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.' print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.'
shared.printLock.release() shared.printLock.release()
self.sock.sendall(datatosend) except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
#We have received a version message #We have received a version message
def recversion(self,data): def recversion(self,data):
@ -1960,14 +1972,26 @@ class receiveDataThread(threading.Thread):
shared.printLock.acquire() shared.printLock.acquire()
print 'Sending version message' print 'Sending version message'
shared.printLock.release() shared.printLock.release()
try:
self.sock.sendall(assembleVersionMessage(self.HOST,self.PORT,self.streamNumber)) self.sock.sendall(assembleVersionMessage(self.HOST,self.PORT,self.streamNumber))
except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
#Sends a verack message #Sends a verack message
def sendverack(self): def sendverack(self):
shared.printLock.acquire() shared.printLock.acquire()
print 'Sending verack' print 'Sending verack'
shared.printLock.release() shared.printLock.release()
try:
self.sock.sendall('\xE9\xBE\xB4\xD9\x76\x65\x72\x61\x63\x6B\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35') self.sock.sendall('\xE9\xBE\xB4\xD9\x76\x65\x72\x61\x63\x6B\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xcf\x83\xe1\x35')
except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
#cf 83 e1 35 #cf 83 e1 35
self.verackSent = True self.verackSent = True
if self.verackReceived == True: if self.verackReceived == True:
@ -2013,7 +2037,13 @@ class sendDataThread(threading.Thread):
shared.printLock.acquire() shared.printLock.acquire()
print 'Sending version packet: ', repr(datatosend) print 'Sending version packet: ', repr(datatosend)
shared.printLock.release() shared.printLock.release()
try:
self.sock.sendall(datatosend) self.sock.sendall(datatosend)
except Exception, err:
#if not 'Bad file descriptor' in err:
shared.printLock.acquire()
sys.stderr.write('sock.sendall error: %s\n' % err)
shared.printLock.release()
self.versionSent = 1 self.versionSent = 1

View File

@ -564,6 +564,9 @@ class MyForm(QtGui.QMainWindow):
self.ui.tableWidgetConnectionCount.removeRow(0) self.ui.tableWidgetConnectionCount.removeRow(0)
for streamNumber, connectionCount in streamNumberTotals.items(): for streamNumber, connectionCount in streamNumberTotals.items():
self.ui.tableWidgetConnectionCount.insertRow(0) self.ui.tableWidgetConnectionCount.insertRow(0)
if streamNumber == 0:
newItem = QtGui.QTableWidgetItem("?")
else:
newItem = QtGui.QTableWidgetItem(str(streamNumber)) newItem = QtGui.QTableWidgetItem(str(streamNumber))
newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled )
self.ui.tableWidgetConnectionCount.setItem(0,0,newItem) self.ui.tableWidgetConnectionCount.setItem(0,0,newItem)