Skip to content

Commit

Permalink
feat: 重新添加__anext__到Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Arama0517 committed Dec 19, 2024
1 parent 6fa8aa3 commit a8c3374
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/steam/core/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Connection:

@property
def local_address(self):
return self._stream_writer.get_extra_info('sockname')[0]
raise NotImplementedError

async def connect(self, server_addr):
raise NotImplementedError
Expand All @@ -48,7 +48,7 @@ async def disconnect(self):
self._readbuf = b''
self.send_queue._queue.clear()
self.recv_queue._queue.clear()
await self.recv_queue.put(StopIteration)
await self.recv_queue.put(StopAsyncIteration)

if self._stream_writer:
logger.debug('wait close')
Expand All @@ -57,12 +57,14 @@ async def disconnect(self):

logger.debug('Disconnected.')

async def __aiter__(self):
while True:
result = await self.recv_queue.get()
if result is StopIteration:
raise StopAsyncIteration
yield result
def __aiter__(self):
return self

async def __anext__(self):
result = await self.recv_queue.get()
if result is StopAsyncIteration:
raise result
return result

async def put_message(self, message):
await self.send_queue.put(message)
Expand All @@ -81,6 +83,10 @@ class TCPConnection(Connection):
FMT = '<I4s'
FMT_SIZE = struct.calcsize(FMT)

@property
def local_address(self):
return self._stream_writer.get_extra_info('sockname')[0]

async def connect(self, server_addr):
host, port = server_addr

Expand Down Expand Up @@ -108,9 +114,6 @@ async def _writer_loop_func(self):
try:
self._stream_writer.write(packet)
await self._stream_writer.drain()
except asyncio.CancelledError:
logger.debug('Writer loop was cancelled.')
break
except Exception as e:
logger.debug(f'Writer error: {e}')
await self.disconnect()
Expand All @@ -128,9 +131,6 @@ async def _reader_loop_func(self):
logger.debug(f'Received data: {data}')
self._readbuf += data
await self._read_packets()
except asyncio.CancelledError:
logger.debug('Reader loop was cancelled.')
break
except Exception as e:
logger.debug(f'Reader error: {e}')
await self.disconnect()
Expand Down Expand Up @@ -195,6 +195,13 @@ async def connect(self, server_addr: tuple[str, int]):
logger.error(f'WebSocket connection failed: {e}')
return False

async def disconnect(self):
if self.ws is not None:
logger.debug('Disconnecting WebSocket...')
await self.ws.close()

await super().disconnect()

async def _writer_loop_func(self):
while True:
message = await self.send_queue.get()
Expand All @@ -221,10 +228,3 @@ async def _reader_loop_func(self):
logger.debug(f'Reader error: {e}')
await self.disconnect()
return

async def disconnect(self):
if self.ws is not None:
logger.debug('Disconnecting WebSocket...')
await self.ws.close()

await super().disconnect()

0 comments on commit a8c3374

Please sign in to comment.