|
21 | 21 | from ssl import VerifyMode
|
22 | 22 | from typing import Optional, TextIO, cast
|
23 | 23 |
|
24 |
| -import aiofiles |
25 | 24 | from construct import Const, Container
|
26 | 25 | from construct import Enum as ConstructEnum
|
27 | 26 | from construct import GreedyBytes, GreedyRange, Int8ul, Int16ub, Int64ul, Prefixed, Struct
|
@@ -161,12 +160,11 @@ async def tun_read_task(self) -> None:
|
161 | 160 | read_size = self.tun.mtu + len(LOOPBACK_HEADER)
|
162 | 161 | try:
|
163 | 162 | if sys.platform != 'win32':
|
164 |
| - async with aiofiles.open(self.tun.fileno(), 'rb', opener=lambda path, flags: path, buffering=0) as f: |
165 |
| - while True: |
166 |
| - packet = await f.read(read_size) |
167 |
| - assert packet.startswith(LOOPBACK_HEADER) |
168 |
| - packet = packet[len(LOOPBACK_HEADER):] |
169 |
| - await self.send_packet_to_device(packet) |
| 163 | + while True: |
| 164 | + packet = await asyncio.to_thread(self.tun.read, read_size) |
| 165 | + assert packet.startswith(LOOPBACK_HEADER) |
| 166 | + packet = packet[len(LOOPBACK_HEADER):] |
| 167 | + await self.send_packet_to_device(packet) |
170 | 168 | else:
|
171 | 169 | while True:
|
172 | 170 | packet = await self.tun.async_read()
|
@@ -311,13 +309,13 @@ async def stop_tunnel(self) -> None:
|
311 | 309 | self._sock_read_task.cancel()
|
312 | 310 | with suppress(CancelledError):
|
313 | 311 | await self._sock_read_task
|
| 312 | + await super().stop_tunnel() |
314 | 313 | if not self._writer.is_closing():
|
315 | 314 | self._writer.close()
|
316 | 315 | try:
|
317 | 316 | await self._writer.wait_closed()
|
318 | 317 | except OSError:
|
319 | 318 | pass
|
320 |
| - await super().stop_tunnel() |
321 | 319 |
|
322 | 320 |
|
323 | 321 | @dataclasses.dataclass
|
|
0 commit comments