def __init__(self, trans, func, rbuf_size=DEFAULT_BUFFER): TTwisted.TCallbackTransport.__init__(self, func) self.__trans = trans self.__wbuf = BufferIO() # Pass string argument to initialize read buffer as cStringIO.InputType self.__rbuf = BufferIO(b'') self.__rbuf_size = rbuf_size
class TPegasusTransport(TTwisted.TCallbackTransport): """Class that wraps another transport and buffers its I/O. """ DEFAULT_BUFFER = 4096 def __init__(self, trans, func, rbuf_size=DEFAULT_BUFFER): TTwisted.TCallbackTransport.__init__(self, func) self.__trans = trans self.__wbuf = BufferIO() # Pass string argument to initialize read buffer as cStringIO.InputType self.__rbuf = BufferIO(b'') self.__rbuf_size = rbuf_size def get_peer_addr(self): return self.__trans.getPeer() def isOpen(self): return self.__trans.isOpen() def open(self): return self.__trans.open() def close(self): return self.__trans.loseConnection() def read(self, sz): ret = self.__rbuf.read(sz) if len(ret) != 0: return ret self.__rbuf = BufferIO(self.__trans.read(max(sz, self.__rbuf_size))) return self.__rbuf.read(sz) def seek(self, offset): try: self.__wbuf.seek(offset) except Exception as e: # on exception reset wbuf so it doesn't contain a partial function call self.__wbuf = BufferIO() raise e def tell(self): try: return self.__wbuf.tell() except Exception as e: # on exception reset wbuf so it doesn't contain a partial function call self.__wbuf = BufferIO() raise e def write(self, buf): try: self.__wbuf.write(buf) except Exception as e: # on exception reset wbuf so it doesn't contain a partial function call self.__wbuf = BufferIO() raise e def flush(self): msg = self.__wbuf.getvalue() self.__wbuf = StringIO() return self.sendMessage(msg)
def write(self, buf): try: self.__wbuf.write(buf) except Exception as e: # on exception reset wbuf so it doesn't contain a partial function call self.__wbuf = BufferIO() raise e
def tell(self): try: return self.__wbuf.tell() except Exception as e: # on exception reset wbuf so it doesn't contain a partial function call self.__wbuf = BufferIO() raise e
def seek(self, offset): try: self.__wbuf.seek(offset) except Exception as e: # on exception reset wbuf so it doesn't contain a partial function call self.__wbuf = BufferIO() raise e
def read(self, sz): ret = self.__rbuf.read(sz) if len(ret) != 0: return ret self.__rbuf = BufferIO(self.__trans.read(max(sz, self.__rbuf_size))) return self.__rbuf.read(sz)