Skip to content

Commit f00d5f7

Browse files
authored
Merge pull request #155 from tekktrik/hotfix/fix-recv-into
Allow optional nbytes parameter for recv_into
2 parents 52208e4 + 2542b26 commit f00d5f7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,22 +161,31 @@ def recv(self, bufsize=0):
161161
gc.collect()
162162
return ret
163163

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

173+
if not 0 <= nbytes <= len(buffer):
174+
raise ValueError(
175+
"Can only read number of bytes between 0 and length of supplied buffer"
176+
)
177+
170178
stamp = time.monotonic()
171179
to_read = len(buffer)
180+
limit = 0 if nbytes == 0 else to_read - nbytes
172181
received = []
173-
while to_read > 0:
182+
while to_read > limit:
174183
# print("Bytes to read:", to_read)
175184
avail = self.available()
176185
if avail:
177186
stamp = time.monotonic()
178187
recv = _the_interface.socket_read(self._socknum, min(to_read, avail))
179-
# received.append(recv)
188+
received.append(recv)
180189
start = len(buffer) - to_read
181190
to_read -= len(recv)
182191
end = len(buffer) - to_read

0 commit comments

Comments
 (0)