Skip to content

Use socket.recv instead of socket.read #71

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

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Changes from all commits
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
26 changes: 17 additions & 9 deletions adafruit_esp32spi/adafruit_esp32spi_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ def readline(self):
gc.collect()
return firstline

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."""
#print("Socket read", size)
if size == 0: # read as much as we can at the moment
def recv(self, bufsize=0):
"""Reads some bytes from the connected remote address.
:param int bufsize: maximum number of bytes to receive
"""
#print("Socket read", bufsize)
if bufsize == 0: # read as much as we can at the moment
while True:
avail = self.available()
if avail:
Expand All @@ -129,7 +130,7 @@ def read(self, size=0):
return ret
stamp = time.monotonic()

to_read = size - len(self._buffer)
to_read = bufsize - len(self._buffer)
received = []
while to_read > 0:
#print("Bytes to read:", to_read)
Expand All @@ -146,15 +147,22 @@ def read(self, size=0):
self._buffer += b''.join(received)

ret = None
if len(self._buffer) == size:
if len(self._buffer) == bufsize:
ret = self._buffer
self._buffer = b''
else:
ret = self._buffer[:size]
self._buffer = self._buffer[size:]
ret = self._buffer[:bufsize]
self._buffer = self._buffer[bufsize:]
gc.collect()
return ret

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.
NOTE: This method is deprecated and will be removed.
"""
return self.recv(size)

def settimeout(self, value):
"""Set the read timeout for sockets, if value is 0 it will block"""
self._timeout = value
Expand Down