Skip to content

Commit

Permalink
Merge pull request #185 from us3r64/fix/recv_into-self-_buffer
Browse files Browse the repository at this point in the history
Fix potential socket data read already into self._buffer
  • Loading branch information
dhalbert committed Jan 15, 2024
2 parents c69b6f2 + e119719 commit 0adb75b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ def recv_into(self, buffer, nbytes: int = 0):
num_to_read = len(buffer) if nbytes == 0 else nbytes
num_read = 0
while num_to_read > 0:
# we might have read socket data into the self._buffer with:
# esp32spi_wsgiserver: socket_readline
if len(self._buffer) > 0:
bytes_to_read = min(num_to_read, len(self._buffer))
buffer[num_read : num_read + bytes_to_read] = self._buffer[
:bytes_to_read
]
num_read += bytes_to_read
num_to_read -= bytes_to_read
self._buffer = self._buffer[bytes_to_read:]
# explicitly recheck num_to_read to avoid extra checks
continue

num_avail = self._available()
if num_avail > 0:
last_read_time = time.monotonic()
Expand Down

0 comments on commit 0adb75b

Please sign in to comment.