def read(self, call_id: int = 0) -> DataWrapper[bytes]: if call_id > 0: data = self._call_store.pop(call_id) if data is not None: return data msg = None try: hsize, bsize = self._read_size() if hsize == 0: return DataWrapper(False, "", b'', 104, "Connection reset (got 0 size)") data = self._read(hsize + bsize) msg = transport_pb2.TransportHeader() msg.ParseFromString(data[:hsize]) if msg.status.success: response = DataWrapper(True, msg.uri, data[hsize:], id=msg.id) else: response = DataWrapper(False, msg.uri, b'', msg.status.error_code, "", msg.status.narrative[:], id=msg.id) if call_id != 0 and msg.id != call_id: self._call_store.put(call_id, response) return self.read(call_id) return response except ConnectionResetError as e: return DataWrapper(False, msg.uri if msg else "", b'', 104, str(e))
async def read(self, call_id: int = 0) -> DataWrapper[bytes]: if call_id > 0: data = self._call_store.pop(call_id) if data is not None: return data try: self.warning("+++++++++ READ MUTEX LOCK WAIT") async with self._read_lock: self.warning("++++++ READ") hsize, bsize = await self._read_size() if hsize == 0: return DataWrapper( False, "", b'', 104, "Connection closed by peer (got 0 size)") data = await self._read(hsize + bsize) msg = transport_pb2.TransportHeader() msg.ParseFromString(data[:hsize]) if msg.status.success: response = DataWrapper(True, msg.uri, data[hsize:], id=msg.id) else: response = DataWrapper(False, msg.uri, b'', msg.status.error_code, "", msg.status.narrative[:], id=msg.id) if call_id != 0 and msg.id != call_id: self._call_store.put(call_id, response) return await self.read(call_id) return response except ConnectionResetError as e: return DataWrapper(False, "", b'', 104, str(e))
def write(self, data: bytes, path: str = "", call_id: int = 0) -> int: msg = transport_pb2.TransportHeader() msg.uri = path msg.id = self._call_store.new_id_if0(call_id) msg.status.success = True self.write_msg(msg, data) return msg.id
async def write_error(self, error_code: int, narrative: List[str], path: str = "", call_id: int = 0) -> int: msg = transport_pb2.TransportHeader() msg.uri = path msg.id = self._call_store.new_id_if0(call_id) msg.status.success = False msg.status.error_code = error_code msg.status.narrative.extend(narrative) await self.write_msg(msg, b'') return msg.id