Skip to content

Commit c28977f

Browse files
authored
Merge pull request #71 from brentru/switch-read
Use socket.recv instead of socket.read
2 parents e16dd06 + bfdbbdd commit c28977f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ def readline(self):
111111
gc.collect()
112112
return firstline
113113

114-
def read(self, size=0):
115-
"""Read up to 'size' bytes from the socket, this may be buffered internally!
116-
If 'size' isnt specified, return everything in the buffer."""
117-
#print("Socket read", size)
118-
if size == 0: # read as much as we can at the moment
114+
def recv(self, bufsize=0):
115+
"""Reads some bytes from the connected remote address.
116+
:param int bufsize: maximum number of bytes to receive
117+
"""
118+
#print("Socket read", bufsize)
119+
if bufsize == 0: # read as much as we can at the moment
119120
while True:
120121
avail = self.available()
121122
if avail:
@@ -129,7 +130,7 @@ def read(self, size=0):
129130
return ret
130131
stamp = time.monotonic()
131132

132-
to_read = size - len(self._buffer)
133+
to_read = bufsize - len(self._buffer)
133134
received = []
134135
while to_read > 0:
135136
#print("Bytes to read:", to_read)
@@ -146,15 +147,22 @@ def read(self, size=0):
146147
self._buffer += b''.join(received)
147148

148149
ret = None
149-
if len(self._buffer) == size:
150+
if len(self._buffer) == bufsize:
150151
ret = self._buffer
151152
self._buffer = b''
152153
else:
153-
ret = self._buffer[:size]
154-
self._buffer = self._buffer[size:]
154+
ret = self._buffer[:bufsize]
155+
self._buffer = self._buffer[bufsize:]
155156
gc.collect()
156157
return ret
157158

159+
def read(self, size=0):
160+
"""Read up to 'size' bytes from the socket, this may be buffered internally!
161+
If 'size' isnt specified, return everything in the buffer.
162+
NOTE: This method is deprecated and will be removed.
163+
"""
164+
return self.recv(size)
165+
158166
def settimeout(self, value):
159167
"""Set the read timeout for sockets, if value is 0 it will block"""
160168
self._timeout = value

0 commit comments

Comments
 (0)