Skip to content

Commit 5b5c5c6

Browse files
committed
Remove non-CPython socket methods; remove wsgi server
1 parent 7e0796c commit 5b5c5c6

File tree

6 files changed

+37
-739
lines changed

6 files changed

+37
-739
lines changed

adafruit_esp32spi/adafruit_esp32spi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def socket_available(self, socket_num):
780780
return reply
781781

782782
def socket_read(self, socket_num, size):
783-
"""Read up to 'size' bytes from the socket number. Returns a bytearray"""
783+
"""Read up to 'size' bytes from the socket number. Returns a bytes"""
784784
if self._debug:
785785
print(
786786
"Reading %d bytes from ESP socket with status %d"

adafruit_esp32spi/adafruit_esp32spi_socket.py

Lines changed: 36 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -86,144 +86,65 @@ def send(self, data): # pylint: disable=no-self-use
8686
_the_interface.socket_write(self._socknum, data, conn_mode=conntype)
8787
gc.collect()
8888

89-
def write(self, data):
90-
"""Sends data to the socket.
91-
NOTE: This method is deprecated and will be removed.
92-
"""
93-
self.send(data)
94-
95-
def readline(self, eol=b"\r\n"):
96-
"""Attempt to return as many bytes as we can up to but not including
97-
end-of-line character (default is '\\r\\n')"""
98-
99-
# print("Socket readline")
100-
stamp = time.monotonic()
101-
while eol not in self._buffer:
102-
# there's no line already in there, read some more
103-
avail = self.available()
104-
if avail:
105-
self._buffer += _the_interface.socket_read(self._socknum, avail)
106-
elif self._timeout > 0 and time.monotonic() - stamp > self._timeout:
107-
self.close() # Make sure to close socket so that we don't exhaust sockets.
108-
raise RuntimeError("Didn't receive full response, failing out")
109-
firstline, self._buffer = self._buffer.split(eol, 1)
110-
gc.collect()
111-
return firstline
112-
113-
def recv(self, bufsize=0):
89+
def recv(self, bufsize: int):
11490
"""Reads some bytes from the connected remote address. Will only return
11591
an empty string after the configured timeout.
11692
11793
:param int bufsize: maximum number of bytes to receive
11894
"""
119-
# print("Socket read", bufsize)
120-
if bufsize == 0: # read as much as we can at the moment
121-
while True:
122-
avail = self.available()
123-
if avail:
124-
self._buffer += _the_interface.socket_read(self._socknum, avail)
125-
else:
126-
break
127-
gc.collect()
128-
ret = self._buffer
129-
self._buffer = b""
130-
gc.collect()
131-
return ret
132-
stamp = time.monotonic()
133-
134-
to_read = bufsize - len(self._buffer)
135-
received = []
136-
while to_read > 0:
137-
# print("Bytes to read:", to_read)
138-
avail = self.available()
139-
if avail:
140-
stamp = time.monotonic()
141-
recv = _the_interface.socket_read(self._socknum, min(to_read, avail))
142-
received.append(recv)
143-
to_read -= len(recv)
144-
gc.collect()
145-
elif received:
146-
# We've received some bytes but no more are available. So return
147-
# what we have.
148-
break
149-
if self._timeout > 0 and time.monotonic() - stamp > self._timeout:
150-
break
151-
# print(received)
152-
self._buffer += b"".join(received)
153-
154-
ret = None
155-
if len(self._buffer) == bufsize:
156-
ret = self._buffer
157-
self._buffer = b""
158-
else:
159-
ret = self._buffer[:bufsize]
160-
self._buffer = self._buffer[bufsize:]
161-
gc.collect()
162-
return ret
95+
buf = bytearray(bufsize)
96+
self.recv_into(buf, bufsize)
16397

164-
def recv_into(self, buffer, nbytes=0):
165-
"""Read some bytes from the connected remote address into a given buffer
98+
def recv_into(self, buffer, nbytes: int = 0):
99+
"""Read bytes from the connected remote address into a given buffer.
166100
167-
: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
101+
:param bytearray buffer: the buffer to read into
102+
:param int nbytes: maximum number of bytes to receive; if 0,
103+
receive as many bytes as possible before filling the
170104
buffer or timing out
171105
"""
172-
173106
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-
178-
stamp = time.monotonic()
179-
to_read = len(buffer)
180-
limit = 0 if nbytes == 0 else to_read - nbytes
181-
received = []
182-
while to_read > limit:
183-
# print("Bytes to read:", to_read)
184-
avail = self.available()
185-
if avail:
186-
stamp = time.monotonic()
187-
recv = _the_interface.socket_read(self._socknum, min(to_read, avail))
188-
received.append(recv)
189-
start = len(buffer) - to_read
190-
to_read -= len(recv)
191-
end = len(buffer) - to_read
192-
buffer[start:end] = bytearray(recv)
193-
gc.collect()
194-
elif received:
195-
# We've received some bytes but no more are available. So return
196-
# what we have.
107+
raise ValueError("nbytes must be 0 to len(buffer)")
108+
109+
last_read_time = time.monotonic()
110+
num_to_read = len(buffer) if nbytes == 0 else nbytes
111+
num_read = 0
112+
while num_read < num_to_read:
113+
num_avail = self._available()
114+
if num_avail > 0:
115+
last_read_time = time.monotonic()
116+
bytes_read = _the_interface.socket_read(
117+
self._socknum, min(num_to_read, num_avail)
118+
)
119+
buffer[num_read : num_read + len(bytes_read)] = bytes_read
120+
num_read += len(bytes_read)
121+
elif num_read > 0:
122+
# We got a message, but there are no more bytes to read, so we can stop.
197123
break
198-
if self._timeout > 0 and time.monotonic() - stamp > self._timeout:
124+
# No bytes yet, or more byte requested.
125+
if self._timeout > 0 and time.monotonic() - last_read_time > self._timeout:
199126
break
200-
gc.collect()
201-
return len(buffer) - to_read
202-
203-
def read(self, size=0):
204-
"""Read up to 'size' bytes from the socket, this may be buffered internally!
205-
If 'size' isnt specified, return everything in the buffer.
206-
NOTE: This method is deprecated and will be removed.
207-
"""
208-
return self.recv(size)
127+
return num_read
209128

210129
def settimeout(self, value):
211-
"""Set the read timeout for sockets, if value is 0 it will block"""
130+
"""Set the read timeout for sockets.
131+
If value is 0 socket reads will block until a message is available.
132+
"""
212133
self._timeout = value
213134

214-
def available(self):
135+
def _available(self):
215136
"""Returns how many bytes of data are available to be read (up to the MAX_PACKET length)"""
216-
if self.socknum != NO_SOCKET_AVAIL:
137+
if self._socknum != NO_SOCKET_AVAIL:
217138
return min(_the_interface.socket_available(self._socknum), MAX_PACKET)
218139
return 0
219140

220-
def connected(self):
141+
def _connected(self):
221142
"""Whether or not we are connected to the socket"""
222-
if self.socknum == NO_SOCKET_AVAIL:
143+
if self._socknum == NO_SOCKET_AVAIL:
223144
return False
224-
if self.available():
145+
if self._available():
225146
return True
226-
status = _the_interface.socket_status(self.socknum)
147+
status = _the_interface.socket_status(self._socknum)
227148
result = status not in (
228149
adafruit_esp32spi.SOCKET_LISTEN,
229150
adafruit_esp32spi.SOCKET_CLOSED,
@@ -239,11 +160,6 @@ def connected(self):
239160
self._socknum = NO_SOCKET_AVAIL
240161
return result
241162

242-
@property
243-
def socknum(self):
244-
"""The socket number"""
245-
return self._socknum
246-
247163
def close(self):
248164
"""Close the socket, after reading whatever remains"""
249165
_the_interface.socket_close(self._socknum)

0 commit comments

Comments
 (0)