Skip to content

Commit

Permalink
Merge pull request #155 from tekktrik/hotfix/fix-recv-into
Browse files Browse the repository at this point in the history
Allow optional nbytes parameter for recv_into
  • Loading branch information
FoamyGuy authored Jan 24, 2022
2 parents 52208e4 + 2542b26 commit f00d5f7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,31 @@ def recv(self, bufsize=0):
gc.collect()
return ret

def recv_into(self, buffer):
def recv_into(self, buffer, nbytes=0):
"""Read some bytes from the connected remote address into a given buffer
:param bytearray buffer: The buffer to read into
:param int nbytes: (Optional) Number of bytes to receive default is 0,
which will receive as many bytes as possible before filling the
buffer or timing out
"""

if not 0 <= nbytes <= len(buffer):
raise ValueError(
"Can only read number of bytes between 0 and length of supplied buffer"
)

stamp = time.monotonic()
to_read = len(buffer)
limit = 0 if nbytes == 0 else to_read - nbytes
received = []
while to_read > 0:
while to_read > limit:
# 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)
received.append(recv)
start = len(buffer) - to_read
to_read -= len(recv)
end = len(buffer) - to_read
Expand Down

0 comments on commit f00d5f7

Please sign in to comment.