From bee74145cdebdf7769f12c79e0d41d63c3b09d70 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Thu, 13 Jan 2022 16:57:32 -0500 Subject: [PATCH 1/2] Add recv_into method --- adafruit_esp32spi/adafruit_esp32spi_socket.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/adafruit_esp32spi/adafruit_esp32spi_socket.py b/adafruit_esp32spi/adafruit_esp32spi_socket.py index d3941fe..05e6602 100644 --- a/adafruit_esp32spi/adafruit_esp32spi_socket.py +++ b/adafruit_esp32spi/adafruit_esp32spi_socket.py @@ -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. From 71456a40624dd8f2d41c7a87feddd4f95e220400 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Thu, 13 Jan 2022 18:59:35 -0500 Subject: [PATCH 2/2] Change return of recv_into() to number of bytes read --- adafruit_esp32spi/adafruit_esp32spi_socket.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_socket.py b/adafruit_esp32spi/adafruit_esp32spi_socket.py index 05e6602..d47ad94 100644 --- a/adafruit_esp32spi/adafruit_esp32spi_socket.py +++ b/adafruit_esp32spi/adafruit_esp32spi_socket.py @@ -187,7 +187,7 @@ def recv_into(self, buffer): if self._timeout > 0 and time.monotonic() - stamp > self._timeout: break gc.collect() - return buffer + return len(buffer) - to_read def read(self, size=0): """Read up to 'size' bytes from the socket, this may be buffered internally!