2019-07-28 14:06:29 +08:00
|
|
|
import asyncio
|
|
|
|
|
2019-09-16 18:37:00 +08:00
|
|
|
from .exceptions import RawConnError
|
2018-11-11 22:56:44 +08:00
|
|
|
from .raw_connection_interface import IRawConnection
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2018-11-11 22:56:44 +08:00
|
|
|
class RawConnection(IRawConnection):
|
2019-07-28 14:06:29 +08:00
|
|
|
reader: asyncio.StreamReader
|
|
|
|
writer: asyncio.StreamWriter
|
2019-10-25 01:28:42 +08:00
|
|
|
is_initiator: bool
|
2019-07-28 14:06:29 +08:00
|
|
|
|
2019-08-20 17:09:38 +08:00
|
|
|
_drain_lock: asyncio.Lock
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
reader: asyncio.StreamReader,
|
|
|
|
writer: asyncio.StreamWriter,
|
|
|
|
initiator: bool,
|
|
|
|
) -> None:
|
2018-11-12 01:17:12 +08:00
|
|
|
self.reader = reader
|
|
|
|
self.writer = writer
|
2019-10-25 01:28:42 +08:00
|
|
|
self.is_initiator = initiator
|
2018-11-11 22:56:44 +08:00
|
|
|
|
2019-08-20 17:09:38 +08:00
|
|
|
self._drain_lock = asyncio.Lock()
|
|
|
|
|
2019-07-28 14:06:29 +08:00
|
|
|
async def write(self, data: bytes) -> None:
|
2019-09-19 22:19:36 +08:00
|
|
|
"""
|
|
|
|
Raise `RawConnError` if the underlying connection breaks
|
|
|
|
"""
|
2019-09-16 18:37:00 +08:00
|
|
|
try:
|
|
|
|
self.writer.write(data)
|
2019-09-19 22:19:36 +08:00
|
|
|
except ConnectionResetError as error:
|
|
|
|
raise RawConnError(error)
|
2019-08-20 17:09:38 +08:00
|
|
|
# Reference: https://github.com/ethereum/lahja/blob/93610b2eb46969ff1797e0748c7ac2595e130aef/lahja/asyncio/endpoint.py#L99-L102 # noqa: E501
|
|
|
|
# Use a lock to serialize drain() calls. Circumvents this bug:
|
|
|
|
# https://bugs.python.org/issue29930
|
|
|
|
async with self._drain_lock:
|
2019-09-16 18:37:00 +08:00
|
|
|
try:
|
|
|
|
await self.writer.drain()
|
2019-09-21 18:05:54 +08:00
|
|
|
except ConnectionResetError as error:
|
|
|
|
raise RawConnError(error)
|
2019-04-30 15:09:05 +08:00
|
|
|
|
2019-08-20 18:09:36 +08:00
|
|
|
async def read(self, n: int = -1) -> bytes:
|
|
|
|
"""
|
|
|
|
Read up to ``n`` bytes from the underlying stream.
|
|
|
|
This call is delegated directly to the underlying ``self.reader``.
|
2019-09-19 22:19:36 +08:00
|
|
|
|
|
|
|
Raise `RawConnError` if the underlying connection breaks
|
2019-08-20 18:09:36 +08:00
|
|
|
"""
|
2019-09-16 18:37:00 +08:00
|
|
|
try:
|
|
|
|
return await self.reader.read(n)
|
2019-09-19 22:19:36 +08:00
|
|
|
except ConnectionResetError as error:
|
|
|
|
raise RawConnError(error)
|
2019-04-30 15:09:05 +08:00
|
|
|
|
2019-08-25 04:06:24 +08:00
|
|
|
async def close(self) -> None:
|
2018-11-12 06:15:55 +08:00
|
|
|
self.writer.close()
|
2019-08-25 04:06:24 +08:00
|
|
|
await self.writer.wait_closed()
|