Skip to content

Commit 61d0f79

Browse files
committed
WIP doesn't flush response before closing
1 parent 301f3e0 commit 61d0f79

File tree

8 files changed

+263
-52
lines changed

8 files changed

+263
-52
lines changed

ports/espressif/background.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,20 @@
4040
#include "common-hal/pulseio/PulseIn.h"
4141
#endif
4242

43+
#if CIRCUITPY_WEB_WORKFLOW
44+
#include "supervisor/shared/web_workflow/web_workflow.h"
45+
#endif
4346

4447
void port_background_task(void) {
4548
// Zero delay in case FreeRTOS wants to switch to something else.
4649
vTaskDelay(0);
4750
#if CIRCUITPY_PULSEIO
4851
pulsein_background();
4952
#endif
53+
54+
#if CIRCUITPY_WEB_WORKFLOW
55+
supervisor_web_workflow_background();
56+
#endif
5057
}
5158

5259
void port_start_background_task(void) {

ports/espressif/common-hal/socketpool/Socket.c

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#include "components/lwip/lwip/src/include/lwip/sys.h"
3838
#include "components/lwip/lwip/src/include/lwip/netdb.h"
3939

40+
#include "esp_log.h"
41+
42+
static const char *TAG = "socket";
43+
4044
STATIC socketpool_socket_obj_t *open_socket_handles[CONFIG_LWIP_MAX_SOCKETS];
4145

4246
void socket_user_reset(void) {
@@ -62,8 +66,7 @@ bool register_open_socket(socketpool_socket_obj_t *self) {
6266
return false;
6367
}
6468

65-
socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_obj_t *self,
66-
uint8_t *ip, uint32_t *port) {
69+
int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_t *port) {
6770
struct sockaddr_in accept_addr;
6871
socklen_t socklen = sizeof(accept_addr);
6972
int newsoc = -1;
@@ -81,7 +84,10 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
8184
newsoc = lwip_accept(self->num, (struct sockaddr *)&accept_addr, &socklen);
8285
// In non-blocking mode, fail instead of timing out
8386
if (newsoc == -1 && self->timeout_ms == 0) {
84-
mp_raise_OSError(MP_EAGAIN);
87+
if (errno != EAGAIN) {
88+
ESP_LOGE(TAG, "accept failed %d", errno);
89+
}
90+
return -MP_EAGAIN;
8591
}
8692
}
8793

@@ -90,8 +96,17 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
9096
memcpy((void *)ip, (void *)&accept_addr.sin_addr.s_addr, sizeof(accept_addr.sin_addr.s_addr));
9197
*port = accept_addr.sin_port;
9298
} else {
93-
mp_raise_OSError(ETIMEDOUT);
99+
return -ETIMEDOUT;
94100
}
101+
if (newsoc < 0) {
102+
return -MP_EBADF;
103+
}
104+
return newsoc;
105+
}
106+
107+
socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_obj_t *self,
108+
uint8_t *ip, uint32_t *port) {
109+
int newsoc = socketpool_socket_accept(self, ip, port);
95110

96111
if (newsoc > 0) {
97112
// Create the socket
@@ -108,7 +123,7 @@ socketpool_socket_obj_t *common_hal_socketpool_socket_accept(socketpool_socket_o
108123
lwip_fcntl(newsoc, F_SETFL, O_NONBLOCK);
109124
return sock;
110125
} else {
111-
mp_raise_OSError(MP_EBADF);
126+
mp_raise_OSError(-newsoc);
112127
return NULL;
113128
}
114129
}
@@ -125,17 +140,22 @@ bool common_hal_socketpool_socket_bind(socketpool_socket_obj_t *self,
125140
if (err != 0) {
126141
mp_raise_RuntimeError(translate("Cannot set socket options"));
127142
}
128-
int result = lwip_bind(self->num, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) == 0;
129-
return result;
143+
int result = lwip_bind(self->num, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
144+
ESP_LOGE(TAG, "bind result %d", result);
145+
return result == 0;
130146
}
131147

132-
void common_hal_socketpool_socket_close(socketpool_socket_obj_t *self) {
148+
void socketpool_socket_close(socketpool_socket_obj_t *self) {
133149
self->connected = false;
134150
if (self->num >= 0) {
135-
lwip_shutdown(self->num, 0);
151+
lwip_shutdown(self->num, SHUT_RDWR);
136152
lwip_close(self->num);
137153
self->num = -1;
138154
}
155+
}
156+
157+
void common_hal_socketpool_socket_close(socketpool_socket_obj_t *self) {
158+
socketpool_socket_close(self);
139159
// Remove socket record
140160
for (size_t i = 0; i < MP_ARRAY_SIZE(open_socket_handles); i++) {
141161
if (open_socket_handles[i] == self) {
@@ -199,7 +219,9 @@ bool common_hal_socketpool_socket_get_connected(socketpool_socket_obj_t *self) {
199219
}
200220

201221
bool common_hal_socketpool_socket_listen(socketpool_socket_obj_t *self, int backlog) {
202-
return lwip_listen(self->num, backlog) == 0;
222+
int result = lwip_listen(self->num, backlog);
223+
ESP_LOGE(TAG, "listen result %d", result);
224+
return result == 0;
203225
}
204226

205227
mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *self,
@@ -242,7 +264,8 @@ mp_uint_t common_hal_socketpool_socket_recvfrom_into(socketpool_socket_obj_t *se
242264
return received;
243265
}
244266

245-
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) {
267+
int socketpool_socket_recv_into(socketpool_socket_obj_t *self,
268+
const uint8_t *buf, uint32_t len) {
246269
int received = 0;
247270
bool timed_out = false;
248271

@@ -261,31 +284,52 @@ mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self,
261284

262285
// In non-blocking mode, fail instead of looping
263286
if (received == -1 && self->timeout_ms == 0) {
264-
mp_raise_OSError(MP_EAGAIN);
287+
if (errno != EAGAIN) {
288+
ESP_LOGE(TAG, "recv %d", errno);
289+
}
290+
return -MP_EAGAIN;
265291
}
266292
}
267293
} else {
268-
mp_raise_OSError(MP_EBADF);
294+
return -MP_EBADF;
269295
}
270296

271297
if (timed_out) {
272-
mp_raise_OSError(ETIMEDOUT);
298+
return -ETIMEDOUT;
273299
}
274300
return received;
275301
}
276302

277-
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) {
303+
mp_uint_t common_hal_socketpool_socket_recv_into(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) {
304+
int received = socketpool_socket_recv_into(self, buf, len);
305+
if (received < 0) {
306+
mp_raise_OSError(received);
307+
}
308+
return received;
309+
}
310+
311+
int socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) {
278312
int sent = -1;
279313
if (self->num != -1) {
280314
// LWIP Socket
281315
// TODO: deal with potential failure/add timeout?
282316
sent = lwip_send(self->num, buf, len, 0);
283317
} else {
284-
mp_raise_OSError(MP_EBADF);
318+
sent = -MP_EBADF;
285319
}
286320

287321
if (sent < 0) {
288-
mp_raise_OSError(errno);
322+
return -errno;
323+
}
324+
325+
return sent;
326+
}
327+
328+
mp_uint_t common_hal_socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len) {
329+
int sent = socketpool_socket_send(self, buf, len);
330+
331+
if (sent < 0) {
332+
mp_raise_OSError(-sent);
289333
}
290334
return sent;
291335
}

ports/espressif/common-hal/socketpool/SocketPool.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,9 @@ void common_hal_socketpool_socketpool_construct(socketpool_socketpool_obj_t *sel
4040
}
4141
}
4242

43-
void socketpool_socket(socketpool_socketpool_obj_t *self,
44-
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type
43+
bool socketpool_socket(socketpool_socketpool_obj_t *self,
44+
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type,
4545
socketpool_socket_obj_t *sock) {
46-
sock->type = socket_type;
47-
sock->family = addr_family;
48-
sock->ipproto = ipproto;
49-
sock->pool = self;
50-
sock->timeout_ms = (uint)-1;
51-
52-
// Create LWIP socket
53-
int socknum = -1;
54-
socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
55-
if (socknum < 0 || !register_open_socket(sock)) {
56-
mp_raise_RuntimeError(translate("Out of sockets"));
57-
}
58-
sock->num = socknum;
59-
// Sockets should be nonblocking in most cases
60-
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
61-
}
62-
63-
socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self,
64-
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) {
65-
6646
int addr_family;
6747
int ipproto;
6848
if (family == SOCKETPOOL_AF_INET) {
@@ -81,15 +61,37 @@ socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_
8161
} else { // SOCKETPOOL_SOCK_RAW
8262
socket_type = SOCK_RAW;
8363
}
64+
sock->type = socket_type;
65+
sock->family = addr_family;
66+
sock->ipproto = ipproto;
67+
sock->pool = self;
68+
sock->timeout_ms = (uint)-1;
69+
70+
// Create LWIP socket
71+
int socknum = -1;
72+
socknum = lwip_socket(sock->family, sock->type, sock->ipproto);
73+
if (socknum < 0) {
74+
return false;
75+
}
76+
sock->num = socknum;
77+
// Sockets should be nonblocking in most cases
78+
lwip_fcntl(socknum, F_SETFL, O_NONBLOCK);
79+
return true;
80+
}
8481

85-
if (addr_family == AF_INET6 || ipproto == IPPROTO_IPV6) {
82+
socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_t *self,
83+
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type) {
84+
if (family != SOCKETPOOL_AF_INET) {
8685
mp_raise_NotImplementedError(translate("Only IPv4 sockets supported"));
8786
}
8887

8988
socketpool_socket_obj_t *sock = m_new_obj_with_finaliser(socketpool_socket_obj_t);
9089
sock->base.type = &socketpool_socket_type;
9190

92-
socketpool_socket(self, family, type, sock);
91+
if (!socketpool_socket(self, family, type, sock) ||
92+
!register_open_socket(sock)) {
93+
mp_raise_RuntimeError(translate("Out of sockets"));
94+
}
9395
return sock;
9496
}
9597

shared-bindings/socketpool/Socket.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket___exit___obj, 4, 4,
7575
//| creating a new socket of type SOCK_STREAM.
7676
//| Returns a tuple of (new_socket, remote_address)"""
7777
//|
78-
STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
78+
STATIC mp_obj_t _socketpool_socket_accept(mp_obj_t self_in) {
7979
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
8080
uint8_t ip[4];
8181
uint32_t port;
@@ -87,7 +87,7 @@ STATIC mp_obj_t socketpool_socket_accept(mp_obj_t self_in) {
8787
tuple_contents[1] = netutils_format_inet_addr(ip, port, NETUTILS_BIG);
8888
return mp_obj_new_tuple(2, tuple_contents);
8989
}
90-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, socketpool_socket_accept);
90+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_accept_obj, _socketpool_socket_accept);
9191

9292
//| def bind(self, address: Tuple[str, int]) -> None:
9393
//| """Bind a socket to an address
@@ -120,12 +120,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_bind_obj, socketpool_socket_b
120120
//| def close(self) -> None:
121121
//| """Closes this Socket and makes its resources available to its SocketPool."""
122122
//|
123-
STATIC mp_obj_t socketpool_socket_close(mp_obj_t self_in) {
123+
STATIC mp_obj_t _socketpool_socket_close(mp_obj_t self_in) {
124124
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
125125
common_hal_socketpool_socket_close(self);
126126
return mp_const_none;
127127
}
128-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_close_obj, socketpool_socket_close);
128+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_close_obj, _socketpool_socket_close);
129129

130130
//| def connect(self, address: Tuple[str, int]) -> None:
131131
//| """Connect a socket to a remote address
@@ -208,7 +208,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_recvfrom_into_obj, socketpool
208208
//| :param int bufsize: optionally, a maximum number of bytes to read."""
209209
//| ...
210210
//|
211-
STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args) {
211+
STATIC mp_obj_t _socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args) {
212212
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(args[0]);
213213
if (common_hal_socketpool_socket_get_closed(self)) {
214214
// Bad file number.
@@ -238,7 +238,7 @@ STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args)
238238
mp_int_t ret = common_hal_socketpool_socket_recv_into(self, (byte *)bufinfo.buf, len);
239239
return mp_obj_new_int_from_uint(ret);
240240
}
241-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3, socketpool_socket_recv_into);
241+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3, _socketpool_socket_recv_into);
242242

243243
//| def send(self, bytes: ReadableBuffer) -> int:
244244
//| """Send some bytes to the connected remote address.
@@ -247,7 +247,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socketpool_socket_recv_into_obj, 2, 3
247247
//| :param ~bytes bytes: some bytes to send"""
248248
//| ...
249249
//|
250-
STATIC mp_obj_t socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
250+
STATIC mp_obj_t _socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
251251
socketpool_socket_obj_t *self = MP_OBJ_TO_PTR(self_in);
252252
if (common_hal_socketpool_socket_get_closed(self)) {
253253
// Bad file number.
@@ -264,7 +264,7 @@ STATIC mp_obj_t socketpool_socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
264264
}
265265
return mp_obj_new_int_from_uint(ret);
266266
}
267-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, socketpool_socket_send);
267+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socketpool_socket_send_obj, _socketpool_socket_send);
268268

269269
//| def sendto(self, bytes: ReadableBuffer, address: Tuple[str, int]) -> int:
270270
//| """Send some bytes to a specific address.

shared-bindings/socketpool/Socket.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,11 @@ mp_uint_t common_hal_socketpool_socket_sendto(socketpool_socket_obj_t *self,
4747
const char *host, size_t hostlen, uint32_t port, const uint8_t *buf, uint32_t len);
4848
void common_hal_socketpool_socket_settimeout(socketpool_socket_obj_t *self, uint32_t timeout_ms);
4949

50+
// Non-allocating versions for internal use.
51+
int socketpool_socket_accept(socketpool_socket_obj_t *self, uint8_t *ip, uint32_t *port);
52+
void socketpool_socket_close(socketpool_socket_obj_t *self);
53+
int socketpool_socket_send(socketpool_socket_obj_t *self, const uint8_t *buf, uint32_t len);
54+
int socketpool_socket_recv_into(socketpool_socket_obj_t *self,
55+
const uint8_t *buf, uint32_t len);
56+
5057
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKET_H

shared-bindings/socketpool/SocketPool.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,10 @@ socketpool_socket_obj_t *common_hal_socketpool_socket(socketpool_socketpool_obj_
5252
mp_obj_t common_hal_socketpool_socketpool_gethostbyname(socketpool_socketpool_obj_t *self,
5353
const char *host);
5454

55+
// Non-allocating version for internal use. These sockets are not registered and, therefore, not
56+
// closed automatically.
57+
bool socketpool_socket(socketpool_socketpool_obj_t *self,
58+
socketpool_socketpool_addressfamily_t family, socketpool_socketpool_sock_t type,
59+
socketpool_socket_obj_t *sock);
60+
5561
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_SOCKETPOOL_SOCKETPOOL_H

0 commit comments

Comments
 (0)