From caf9890bd17e9f55ce89e0843567b000d67f198f Mon Sep 17 00:00:00 2001 From: Jonathan Warren Date: Fri, 3 May 2013 12:24:47 -0400 Subject: [PATCH] better error handling around sock.sendall --- src/bitmessagemain.py | 82 ++++++++++++++++++++++++------------ src/bitmessageqt/__init__.py | 5 ++- 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index fb77274f..adeb657f 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -412,7 +412,13 @@ class receiveDataThread(threading.Thread): def sendpong(self): print 'Sending pong' - 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') + 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') + 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): print 'verack received' @@ -501,7 +507,13 @@ class receiveDataThread(threading.Thread): shared.printLock.acquire() print 'Sending huge inv message with', numberOfObjects, 'objects to just this one peer' shared.printLock.release() - self.sock.sendall(headerData + payload) + 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() #We have received a broadcast message def recbroadcast(self,data): @@ -1519,7 +1531,7 @@ class receiveDataThread(threading.Thread): except Exception, err: #if not 'Bad file descriptor' in err: shared.printLock.acquire() - sys.stderr.write('sock.send error: %s\n' % err) + sys.stderr.write('sock.sendall error: %s\n' % err) shared.printLock.release() #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. def sendData(self,objectType,payload): + headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. if objectType == 'pubkey': shared.printLock.acquire() print 'sending pubkey' 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 += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) elif objectType == 'getpubkey' or objectType == 'pubkeyrequest': shared.printLock.acquire() print 'sending getpubkey' shared.printLock.release() - headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. 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': shared.printLock.acquire() print 'sending msg' 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 += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) elif objectType == 'broadcast': shared.printLock.acquire() print 'sending broadcast' shared.printLock.release() - headerData = '\xe9\xbe\xb4\xd9' #magic bits, slighly different from Bitcoin's magic bits. headerData += 'broadcast\x00\x00\x00' - headerData += pack('>L',len(payload)) #payload length. - headerData += hashlib.sha512(payload).digest()[:4] - self.sock.sendall(headerData + payload) else: 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 def broadcastinv(self,hash): @@ -1893,12 +1900,17 @@ class receiveDataThread(threading.Thread): datatosend = datatosend + pack('>L',len(payload)) #payload length datatosend = datatosend + hashlib.sha512(payload).digest()[0:4] datatosend = datatosend + payload - - if verbose >= 1: + try: + self.sock.sendall(datatosend) + if verbose >= 1: + shared.printLock.acquire() + print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.' + shared.printLock.release() + except Exception, err: + #if not 'Bad file descriptor' in err: shared.printLock.acquire() - print 'Sending addr with', numberOfAddressesInAddrMessage, 'entries.' + sys.stderr.write('sock.sendall error: %s\n' % err) shared.printLock.release() - self.sock.sendall(datatosend) #We have received a version message def recversion(self,data): @@ -1960,14 +1972,26 @@ class receiveDataThread(threading.Thread): shared.printLock.acquire() print 'Sending version message' shared.printLock.release() - self.sock.sendall(assembleVersionMessage(self.HOST,self.PORT,self.streamNumber)) + try: + 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 def sendverack(self): shared.printLock.acquire() print 'Sending verack' shared.printLock.release() - 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') + 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') + 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 self.verackSent = True if self.verackReceived == True: @@ -2013,7 +2037,13 @@ class sendDataThread(threading.Thread): shared.printLock.acquire() print 'Sending version packet: ', repr(datatosend) shared.printLock.release() - self.sock.sendall(datatosend) + try: + 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 diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 7c8b89bb..7e99db8a 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -564,7 +564,10 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetConnectionCount.removeRow(0) for streamNumber, connectionCount in streamNumberTotals.items(): self.ui.tableWidgetConnectionCount.insertRow(0) - newItem = QtGui.QTableWidgetItem(str(streamNumber)) + if streamNumber == 0: + newItem = QtGui.QTableWidgetItem("?") + else: + newItem = QtGui.QTableWidgetItem(str(streamNumber)) newItem.setFlags( QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled ) self.ui.tableWidgetConnectionCount.setItem(0,0,newItem) newItem = QtGui.QTableWidgetItem(str(connectionCount))