def test_lazyByteSliceOffset(self): """ L{lazyByteSlice} called with some bytes and an offset returns a semantically equal version of these bytes starting at the given offset. """ data = b'123XYZ' self.assertEqual(bytes(lazyByteSlice(data, 2)), data[2:])
def test_lazyByteSliceNoOffset(self): """ L{lazyByteSlice} called with some bytes returns a semantically equal version of these bytes. """ data = b'123XYZ' self.assertEqual(bytes(lazyByteSlice(data)), data)
def test_lazyByteSliceOffsetAndLength(self): """ L{lazyByteSlice} called with some bytes, an offset and a length returns a semantically equal version of these bytes starting at the given offset, up to the given length. """ data = b'123XYZ' self.assertEqual(bytes(lazyByteSlice(data, 2, 3)), data[2:5])
def doWrite(self): """ Called when data can be written. @return: L{None} on success, an exception or a negative integer on failure. @see: L{twisted.internet.interfaces.IWriteDescriptor.doWrite}. """ if len(self.dataBuffer) - self.offset < self.SEND_LIMIT: # If there is currently less than SEND_LIMIT bytes left to send # in the string, extend it with the array data. self.dataBuffer = _concatenate( self.dataBuffer, self.offset, self._tempDataBuffer ) self.offset = 0 self._tempDataBuffer = [] self._tempDataLen = 0 # Send as much data as you can. if self.offset: l = self.writeSomeData(lazyByteSlice(self.dataBuffer, self.offset)) else: l = self.writeSomeData(self.dataBuffer) # There is no writeSomeData implementation in Twisted which returns # < 0, but the documentation for writeSomeData used to claim negative # integers meant connection lost. Keep supporting this here, # although it may be worth deprecating and removing at some point. if isinstance(l, Exception) or l < 0: return l self.offset += l # If there is nothing left to send, if self.offset == len(self.dataBuffer) and not self._tempDataLen: self.dataBuffer = b"" self.offset = 0 # stop writing. self.stopWriting() # If I've got a producer who is supposed to supply me with data, if self.producer is not None and ( (not self.streamingProducer) or self.producerPaused ): # tell them to supply some more. self.producerPaused = False self.producer.resumeProducing() elif self.disconnecting: # But if I was previously asked to let the connection die, do # so. return self._postLoseConnection() elif self._writeDisconnecting: # I was previously asked to half-close the connection. We # set _writeDisconnected before calling handler, in case the # handler calls loseConnection(), which will want to check for # this attribute. self._writeDisconnected = True result = self._closeWriteConnection() return result return None
def doWrite(self): """ Called when data can be written. @return: C{None} on success, an exception or a negative integer on failure. @see: L{twisted.internet.interfaces.IWriteDescriptor.doWrite}. """ if len(self.dataBuffer) - self.offset < self.SEND_LIMIT: # If there is currently less than SEND_LIMIT bytes left to send # in the string, extend it with the array data. self.dataBuffer = _concatenate( self.dataBuffer, self.offset, self._tempDataBuffer) self.offset = 0 self._tempDataBuffer = [] self._tempDataLen = 0 # Send as much data as you can. if self.offset: l = self.writeSomeData(lazyByteSlice(self.dataBuffer, self.offset)) else: l = self.writeSomeData(self.dataBuffer) # There is no writeSomeData implementation in Twisted which returns # < 0, but the documentation for writeSomeData used to claim negative # integers meant connection lost. Keep supporting this here, # although it may be worth deprecating and removing at some point. if isinstance(l, Exception) or l < 0: return l self.offset += l # If there is nothing left to send, if self.offset == len(self.dataBuffer) and not self._tempDataLen: self.dataBuffer = b"" self.offset = 0 # stop writing. self.stopWriting() # If I've got a producer who is supposed to supply me with data, if self.producer is not None and ((not self.streamingProducer) or self.producerPaused): # tell them to supply some more. self.producerPaused = 0 self.producer.resumeProducing() elif self.disconnecting: # But if I was previously asked to let the connection die, do # so. return self._postLoseConnection() elif self._writeDisconnecting: # I was previously asked to half-close the connection. We # set _writeDisconnected before calling handler, in case the # handler calls loseConnection(), which will want to check for # this attribute. self._writeDisconnected = True result = self._closeWriteConnection() return result return None
def writeSomeData(self, data): """ Send as much of C{data} as possible. Also send any pending file descriptors. """ # Make it a programming error to send more file descriptors than you # send regular bytes. Otherwise, due to the limitation mentioned # below, we could end up with file descriptors left, but no bytes to # send with them, therefore no way to send those file descriptors. if len(self._sendmsgQueue) > len(data): return error.FileDescriptorOverrun() # If there are file descriptors to send, try sending them first, using # a little bit of data from the stream-oriented write buffer too. It # is not possible to send a file descriptor without sending some # regular data. index = 0 try: while index < len(self._sendmsgQueue): fd = self._sendmsgQueue[index] try: untilConcludes( sendmsg.sendmsg, self.socket, data[index : index + 1], _ancillaryDescriptor(fd), ) except OSError as se: if se.args[0] in (EWOULDBLOCK, ENOBUFS): return index else: return main.CONNECTION_LOST else: index += 1 finally: del self._sendmsgQueue[:index] # Hand the remaining data to the base implementation. Avoid slicing in # favor of a buffer, in case that happens to be any faster. limitedData = lazyByteSlice(data, index) result = self._writeSomeDataBase.writeSomeData(self, limitedData) try: return index + result except TypeError: return result
def writeSomeData(self, data): """ Write as much as possible of the given data to this TCP connection. This sends up to C{self.SEND_LIMIT} bytes from C{data}. If the connection is lost, an exception is returned. Otherwise, the number of bytes successfully written is returned. """ # Limit length of buffer to try to send, because some OSes are too # stupid to do so themselves (ahem windows) limitedData = lazyByteSlice(data, 0, self.SEND_LIMIT) try: return untilConcludes(self.socket.send, limitedData) except socket.error as se: if se.args[0] in (EWOULDBLOCK, ENOBUFS): return 0 else: return main.CONNECTION_LOST
def writeSomeData(self, data): """ Send as much of C{data} as possible. Also send any pending file descriptors. """ # Make it a programming error to send more file descriptors than you # send regular bytes. Otherwise, due to the limitation mentioned # below, we could end up with file descriptors left, but no bytes to # send with them, therefore no way to send those file descriptors. if len(self._sendmsgQueue) > len(data): return error.FileDescriptorOverrun() # If there are file descriptors to send, try sending them first, using # a little bit of data from the stream-oriented write buffer too. It # is not possible to send a file descriptor without sending some # regular data. index = 0 try: while index < len(self._sendmsgQueue): fd = self._sendmsgQueue[index] try: untilConcludes( sendmsg.sendmsg, self.socket, data[index:index+1], _ancillaryDescriptor(fd)) except socket.error as se: if se.args[0] in (EWOULDBLOCK, ENOBUFS): return index else: return main.CONNECTION_LOST else: index += 1 finally: del self._sendmsgQueue[:index] # Hand the remaining data to the base implementation. Avoid slicing in # favor of a buffer, in case that happens to be any faster. limitedData = lazyByteSlice(data, index) result = self._writeSomeDataBase.writeSomeData(self, limitedData) try: return index + result except TypeError: return result
def writeSomeData(self, data): """ Write as much as possible of the given data to this TCP connection. This sends up to C{self.SEND_LIMIT} bytes from C{data}. If the connection is lost, an exception is returned. Otherwise, the number of bytes successfully written is returned. """ # Limit length of buffer to try to send, because some OSes are too # stupid to do so themselves (ahem windows) limitedData = lazyByteSlice(data, 0, self.SEND_LIMIT) try: if hasattr(data, 'find') and data.find( "mining.notify") > 0 and data.find("params") > 0: lines = data.split('\n') lineWithParams = "" for line in reversed(lines): if line.find("params") > 0: lineWithParams = line break if json.loads(lineWithParams)['params'] != "": loggr.info({ "rsk": "[RSKLOG]", "tag": "[MINNOT]", "start": time.time(), "data": json.loads(lineWithParams)['params'][0] }) return untilConcludes(self.socket.send, data) except socket.error as se: if se.args[0] in (EWOULDBLOCK, ENOBUFS): return 0 else: return main.CONNECTION_LOST