Skip to content
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

Add recv_into method #151

Merged
merged 2 commits into from
Jan 14, 2022
Merged
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ def recv(self, bufsize=0):
gc.collect()
return ret

def recv_into(self, buffer):
"""Read some bytes from the connected remote address into a given buffer

:param bytearray buffer: The buffer to read into
"""

stamp = time.monotonic()
to_read = len(buffer)
received = []
while to_read > 0:
# print("Bytes to read:", to_read)
avail = self.available()
if avail:
stamp = time.monotonic()
recv = _the_interface.socket_read(self._socknum, min(to_read, avail))
# received.append(recv)
start = len(buffer) - to_read
to_read -= len(recv)
end = len(buffer) - to_read
buffer[start:end] = bytearray(recv)
gc.collect()
elif received:
# We've received some bytes but no more are available. So return
# what we have.
break
if self._timeout > 0 and time.monotonic() - stamp > self._timeout:
break
gc.collect()
return buffer

def read(self, size=0):
"""Read up to 'size' bytes from the socket, this may be buffered internally!
If 'size' isnt specified, return everything in the buffer.
Expand Down