Skip to content
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
15 changes: 6 additions & 9 deletions tinylink/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Handle(Protocol):

def read(self, size: int) -> bytes:
""" "
Read up to `size` bytes.
Read up to `size` bytes.
"""

def write(self, data: bytes) -> int:
Expand Down Expand Up @@ -105,12 +105,9 @@ def __init__(

# Pre-allocate buffer that fits header + body. The premable will be
# cleared when it is detected, so it does not need space.
self.stream = bytearray(max_length + consts.LEN_HEADER + consts.LEN_BODY)
self.buffer = bytearray(max_length + consts.LEN_HEADER + consts.LEN_BODY)
self.index = 0

# Python 2 does not allow unpack from bytearray, but Python 3.
self.buffer = self.stream

def write_frame(self, frame: Frame) -> int:
"""
Write a frame via the handle.
Expand Down Expand Up @@ -168,8 +165,8 @@ def read(self, limit: int = 1) -> list[Frame]:
if not char:
return []

# Append to stream.
self.stream[self.index] = ord(char)
# Append to buffer.
self.buffer[self.index] = ord(char)
self.index += 1

# Decide what to do.
Expand All @@ -187,10 +184,10 @@ def read(self, limit: int = 1) -> list[Frame]:
self.index
== self.max_length + consts.LEN_HEADER + consts.LEN_BODY
):
# Preamble not found and stream is full. Copy last four
# Preamble not found and buffer is full. Copy last four
# bytes, because the next byte may form the preamble
# together with the last three bytes.
self.stream[0:4] = self.stream[-4:]
self.buffer[0:4] = self.buffer[-4:]
self.index = 4

elif self.state == consts.WAITING_FOR_HEADER:
Expand Down