def sendBytes(self, data): if shared.config.getint('bitmessagesettings', 'maxuploadrate') == 0: uploadRateLimitBytes = 999999999 # float("inf") doesn't work else: uploadRateLimitBytes = shared.config.getint('bitmessagesettings', 'maxuploadrate') * 1000 with shared.sendDataLock: while data: while shared.numberOfBytesSentLastSecond >= uploadRateLimitBytes: if int(time.time()) == shared.lastTimeWeResetBytesSent: time.sleep(0.3) else: # It's a new second. Let us clear the shared.numberOfBytesSentLastSecond shared.lastTimeWeResetBytesSent = int(time.time()) shared.numberOfBytesSentLastSecond = 0 # If the user raises or lowers the uploadRateLimit then we should make use of # the new setting. If we are hitting the limit then we'll check here about # once per second. if shared.config.getint('bitmessagesettings', 'maxuploadrate') == 0: uploadRateLimitBytes = 999999999 # float("inf") doesn't work else: uploadRateLimitBytes = shared.config.getint('bitmessagesettings', 'maxuploadrate') * 1000 if ((self.services & shared.NODE_SSL == shared.NODE_SSL) and self.connectionIsOrWasFullyEstablished and shared.haveSSL(not self.initiatedConnection)): amountSent = self.sslSock.send(data[:1000]) else: amountSent = self.sock.send(data[:1000]) shared.numberOfBytesSent += amountSent # Used for the 'network status' tab in the UI shared.numberOfBytesSentLastSecond += amountSent self.lastTimeISentData = int(time.time()) data = data[amountSent:]
def sendBytes(self, data): if shared.config.getint('bitmessagesettings', 'maxuploadrate') == 0: uploadRateLimitBytes = 999999999 # float("inf") doesn't work else: uploadRateLimitBytes = shared.config.getint( 'bitmessagesettings', 'maxuploadrate') * 1000 with shared.sendDataLock: while data: while shared.numberOfBytesSentLastSecond >= uploadRateLimitBytes: if int(time.time()) == shared.lastTimeWeResetBytesSent: time.sleep(0.3) else: # It's a new second. Let us clear the shared.numberOfBytesSentLastSecond shared.lastTimeWeResetBytesSent = int(time.time()) shared.numberOfBytesSentLastSecond = 0 # If the user raises or lowers the uploadRateLimit then we should make use of # the new setting. If we are hitting the limit then we'll check here about # once per second. if shared.config.getint('bitmessagesettings', 'maxuploadrate') == 0: uploadRateLimitBytes = 999999999 # float("inf") doesn't work else: uploadRateLimitBytes = shared.config.getint( 'bitmessagesettings', 'maxuploadrate') * 1000 if ((self.services & shared.NODE_SSL == shared.NODE_SSL) and self.connectionIsOrWasFullyEstablished and shared.haveSSL(not self.initiatedConnection)): amountSent = self.sslSock.send(data[:1000]) else: amountSent = self.sock.send(data[:1000]) shared.numberOfBytesSent += amountSent # used for the 'network status' tab in the UI shared.numberOfBytesSentLastSecond += amountSent self.lastTimeISentData = int(time.time()) data = data[amountSent:]
def connectionFullyEstablished(self): if self.connectionIsOrWasFullyEstablished: # there is no reason to run this function a second time return self.connectionIsOrWasFullyEstablished = True self.sslSock = self.sock if ((self.services & shared.NODE_SSL == shared.NODE_SSL) and shared.haveSSL(not self.initiatedConnection)): logger.debug("Initialising TLS") self.sslSock = ssl.wrap_socket(self.sock, keyfile = os.path.join(shared.codePath(), 'sslkeys', 'key.pem'), certfile = os.path.join(shared.codePath(), 'sslkeys', 'cert.pem'), server_side = not self.initiatedConnection, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False, ciphers='AECDH-AES256-SHA') if hasattr(self.sslSock, "context"): self.sslSock.context.set_ecdh_curve("secp256k1") while True: try: self.sslSock.do_handshake() break except ssl.SSLError as e: if e.errno == 2: select.select([self.sslSock], [self.sslSock], []) else: break except: break # Command the corresponding sendDataThread to set its own connectionIsOrWasFullyEstablished variable to True also self.sendDataThreadQueue.put((0, 'connectionIsOrWasFullyEstablished', (self.services, self.sslSock))) if not self.initiatedConnection: shared.clientHasReceivedIncomingConnections = True shared.UISignalQueue.put(('setStatusIcon', 'green')) self.sock.settimeout( 600) # We'll send out a pong every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately. shared.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) logger.debug('Connection fully established with ' + str(self.peer) + "\n" + \ 'The size of the connectedHostsList is now ' + str(len(shared.connectedHostsList)) + "\n" + \ 'The length of sendDataQueues is now: ' + str(len(shared.sendDataQueues)) + "\n" + \ 'broadcasting addr from within connectionFullyEstablished function.') # Let all of our peers know about this new node. dataToSend = (int(time.time()), self.streamNumber, 1, self.peer.host, self.remoteNodeIncomingPort) shared.broadcastToSendDataQueues(( self.streamNumber, 'advertisepeer', dataToSend)) self.sendaddr() # This is one large addr message to this one peer. if not self.initiatedConnection and len(shared.connectedHostsList) > 200: logger.info ('We are connected to too many people. Closing connection.') self.sendDataThreadQueue.put((0, 'shutdown','no data')) return self.sendBigInv()
def run(self): logger.debug('receiveDataThread starting. ID ' + str(id(self)) + '. The size of the shared.connectedHostsList is now ' + str(len(shared.connectedHostsList))) while True: if shared.config.getint('bitmessagesettings', 'maxdownloadrate') == 0: downloadRateLimitBytes = float("inf") else: downloadRateLimitBytes = shared.config.getint( 'bitmessagesettings', 'maxdownloadrate') * 1000 with shared.receiveDataLock: while shared.numberOfBytesReceivedLastSecond >= downloadRateLimitBytes: if int(time.time()) == shared.lastTimeWeResetBytesReceived: # If it's still the same second that it was last time then sleep. time.sleep(0.3) else: # It's a new second. Let us clear the shared.numberOfBytesReceivedLastSecond. shared.lastTimeWeResetBytesReceived = int(time.time()) shared.numberOfBytesReceivedLastSecond = 0 dataLen = len(self.data) try: if ((self.services & shared.NODE_SSL == shared.NODE_SSL) and self.connectionIsOrWasFullyEstablished and shared.haveSSL(not self.initiatedConnection)): dataRecv = self.sslSock.recv(1024) else: dataRecv = self.sock.recv(1024) self.data += dataRecv shared.numberOfBytesReceived += len( dataRecv ) # for the 'network status' UI tab. The UI clears this value whenever it updates. shared.numberOfBytesReceivedLastSecond += len( dataRecv) # for the download rate limit except socket.timeout: logger.error('Timeout occurred waiting for data from ' + str(self.peer) + '. Closing receiveData thread. (ID: ' + str(id(self)) + ')') break except Exception as err: if (sys.platform == 'win32' and err.errno in ([2, 10035])) or ( sys.platform != 'win32' and err.errno == errno.EWOULDBLOCK): select.select([self.sslSock], [], []) continue logger.error('sock.recv error. Closing receiveData thread (' + str(self.peer) + ', Thread ID: ' + str(id(self)) + ').' + str(err.errno) + "/" + str(err)) break # print 'Received', repr(self.data) if len(self.data ) == dataLen: # If self.sock.recv returned no data: logger.debug('Connection to ' + str(self.peer) + ' closed. Closing receiveData thread. (ID: ' + str(id(self)) + ')') break else: self.processData() try: del self.selfInitiatedConnections[self.streamNumber][self] logger.debug( 'removed self (a receiveDataThread) from selfInitiatedConnections' ) except: pass self.sendDataThreadQueue.put( (0, 'shutdown', 'no data') ) # commands the corresponding sendDataThread to shut itself down. try: del shared.connectedHostsList[self.peer.host] except Exception as err: logger.error('Could not delete ' + str(self.peer.host) + ' from shared.connectedHostsList.' + str(err)) try: del shared.numberOfObjectsThatWeHaveYetToGetPerPeer[self.peer] except: pass shared.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) logger.debug('receiveDataThread ending. ID ' + str(id(self)) + '. The size of the shared.connectedHostsList is now ' + str(len(shared.connectedHostsList)))
def run(self): logger.debug('receiveDataThread starting. ID ' + str(id(self)) + '. The size of the shared.connectedHostsList is now ' + str(len(shared.connectedHostsList))) while True: if shared.config.getint('bitmessagesettings', 'maxdownloadrate') == 0: downloadRateLimitBytes = float("inf") else: downloadRateLimitBytes = shared.config.getint('bitmessagesettings', 'maxdownloadrate') * 1000 with shared.receiveDataLock: while shared.numberOfBytesReceivedLastSecond >= downloadRateLimitBytes: if int(time.time()) == shared.lastTimeWeResetBytesReceived: # If it's still the same second that it was last time then sleep. time.sleep(0.3) else: # It's a new second. Let us clear the shared.numberOfBytesReceivedLastSecond. shared.lastTimeWeResetBytesReceived = int(time.time()) shared.numberOfBytesReceivedLastSecond = 0 dataLen = len(self.data) try: if ((self.services & shared.NODE_SSL == shared.NODE_SSL) and self.connectionIsOrWasFullyEstablished and shared.haveSSL(not self.initiatedConnection)): dataRecv = self.sslSock.recv(1024) else: dataRecv = self.sock.recv(1024) self.data += dataRecv shared.numberOfBytesReceived += len(dataRecv) # for the 'network status' UI tab. The UI clears this value whenever it updates. shared.numberOfBytesReceivedLastSecond += len(dataRecv) # for the download rate limit except socket.timeout: logger.error ('Timeout occurred waiting for data from ' + str(self.peer) + '. Closing receiveData thread. (ID: ' + str(id(self)) + ')') break except Exception as err: if (sys.platform == 'win32' and err.errno in ([2, 10035])) or (sys.platform != 'win32' and err.errno == errno.EWOULDBLOCK): select.select([self.sslSock], [], []) continue logger.error('sock.recv error. Closing receiveData thread (' + str(self.peer) + ', Thread ID: ' + str(id(self)) + ').' + str(err.errno) + "/" + str(err)) break # print 'Received', repr(self.data) if len(self.data) == dataLen: # If self.sock.recv returned no data: logger.debug('Connection to ' + str(self.peer) + ' closed. Closing receiveData thread. (ID: ' + str(id(self)) + ')') break else: self.processData() try: del self.selfInitiatedConnections[self.streamNumber][self] logger.debug('removed self (a receiveDataThread) from selfInitiatedConnections') except: pass self.sendDataThreadQueue.put((0, 'shutdown','no data')) # commands the corresponding sendDataThread to shut itself down. try: del shared.connectedHostsList[self.peer.host] except Exception as err: logger.error('Could not delete ' + str(self.peer.host) + ' from shared.connectedHostsList.' + str(err)) try: del shared.numberOfObjectsThatWeHaveYetToGetPerPeer[ self.peer] except: pass shared.UISignalQueue.put(('updateNetworkStatusTab', 'no data')) logger.debug('receiveDataThread ending. ID ' + str(id(self)) + '. The size of the shared.connectedHostsList is now ' + str(len(shared.connectedHostsList)))