8000 Fix read- and writeloop by alengwenus · Pull Request #127 · alengwenus/pypck · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix read- and writeloop #127 8000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions pypck/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import asyncio
import logging
import time
from collections import deque
from collections.abc import Awaitable, Callable, Iterable
from types import TracebackType
from typing import Any
Expand Down Expand Up @@ -82,7 +81,7 @@ def __init__(self, host: str, port: int, connection_id: str = "PCHK"):
self.connection_id = connection_id
self.reader: asyncio.StreamReader | None = None
self.writer: asyncio.StreamWriter | None = None
self.buffer: deque[bytes] = deque()
self.buffer: asyncio.Queue[bytes] = asyncio.Queue()
self.idle_time = 0.05
self.last_bus_activity = time.time()
self.event_handler: Callable[[str], Awaitable[None]] = (
Expand All @@ -109,46 +108,53 @@ async def read_data_loop(self) -> None:
"""Is called when some data is received."""
assert self.reader is not None
assert self.writer is not None
while not self.writer.is_closing():
try:
data = await self.reader.readuntil(PckGenerator.TERMINATION.encode())
self.last_bus_activity = time.time()
except asyncio.IncompleteReadError:
_LOGGER.debug("Connection to %s lost", self.connection_id)
await self.event_handler("connection-lost")
await self.async_close()
break
except asyncio.CancelledError:
break

try:
message = data.decode().split(PckGenerator.TERMINATION)[0]
except UnicodeDecodeError as err:
_LOGGER.warning(
"PCK decoding error: %s - skipping received PCK message", err
)
continue
await self.process_message(message)
try:
while not self.writer.is_closing():
try:
data = await self.reader.readuntil(
PckGenerator.TERMINATION.encode()
)
self.last_bus_activity = time.time()
except asyncio.IncompleteReadError:
_LOGGER.debug("Connection to %s lost", self.connection_id)
await self.event_handler("connection-lost")
await self.async_close()
break

try:
message = data.decode().split(PckGenerator.TERMINATION)[0]
except UnicodeDecodeError as err:
_LOGGER.warning(
"PCK decoding error: %s - skipping received PCK message", err
)
continue
await self.process_message(message)
except asyncio.CancelledError:
pass

async def write_data_loop(self) -> None:
"""Processes queue and writes data."""
assert self.writer is not None
while not self.writer.is_closing():
await asyncio.sleep(self.idle_time)
if len(self.buffer) == 0:
continue
if time.time() - self.last_bus_activity < self.idle_time:
continue
data = self.buffer.popleft()
self.last_bus_activity = time.time()

_LOGGER.debug(
"to %s: %s",
self.connection_id,
data.decode().rstrip(PckGenerator.TERMINATION),
)
self.writer.write(data)
await self.writer.drain()
try:
while not self.writer.is_closing():
data = await self.buffer.get()
while (time.time() - self.last_bus_activity) < self.idle_time:
await asyncio.sleep(self.idle_time)

_LOGGER.debug(
"to %s: %s",
self.connection_id,
data.decode().rstrip(PckGenerator.TERMINATION),
)
self.writer.write(data)
await self.writer.drain()
self.last_bus_activity = time.time()
except asyncio.CancelledError:
pass

# empty the queue
while not self.buffer.empty():
await self.buffer.get()

async def send_command(self, pck: bytes | str, **kwargs: Any) -> bool:
"""Send a PCK command to the PCHK server.
Expand All @@ -161,7 +167,7 @@ async def send_command(self, pck: bytes | str, **kwargs: Any) -> bool:
data = (pck + PckGenerator.TERMINATION).encode()
else:
data = pck + PckGenerator.TERMINATION.encode()
self.buffer.append(data)
await self.buffer.put(data)
return True
return False

Expand Down
Loading
0