@@ -89,7 +89,7 @@ static mp_obj_t socketpool_ip_addr_and_port_to_tuple(const ip_addr_t *addr, int
89
89
// socket API.
90
90
91
91
// Extension to lwIP error codes
92
- // Matches lwIP 2.0.3
92
+ // Matches lwIP 2.2.1
93
93
#undef _ERR_BADF
94
94
#define _ERR_BADF -17
95
95
static const int error_lookup_table [] = {
@@ -473,7 +473,12 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
473
473
474
474
MICROPY_PY_LWIP_ENTER
475
475
476
- u16_t available = tcp_sndbuf (socket -> pcb .tcp );
476
+ // If the socket is still connecting then don't let data be written to it.
477
+ // Otherwise, get the number of available bytes in the output buffer.
478
+ u16_t available = 0 ;
479
+ if (socket -> state != STATE_CONNECTING ) {
480
+ available = tcp_sndbuf (socket -> pcb .tcp );
481
+ }
477
482
478
483
if (available == 0 ) {
479
484
// Non-blocking socket
@@ -490,7 +495,8 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
490
495
// If peer fully closed socket, we would have socket->state set to ERR_RST (connection
491
496
// reset) by error callback.
492
497
// Avoid sending too small packets, so wait until at least 16 bytes available
493
- while (socket -> state >= STATE_CONNECTED && (available = tcp_sndbuf (socket -> pcb .tcp )) < 16 ) {
498
+ while (socket -> state == STATE_CONNECTING
499
+ || (socket -> state >= STATE_CONNECTED && (available = tcp_sndbuf (socket -> pcb .tcp )) < 16 )) {
494
500
MICROPY_PY_LWIP_EXIT
495
501
if (socket -> timeout != (unsigned )-1 && mp_hal_ticks_ms () - start > socket -> timeout ) {
496
502
* _errno = MP_ETIMEDOUT ;
@@ -531,9 +537,10 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
531
537
MICROPY_PY_LWIP_REENTER
532
538
}
533
539
534
- // If the output buffer is getting full then send the data to the lower layers
535
- if (err == ERR_OK && tcp_sndbuf (socket -> pcb .tcp ) < TCP_SND_BUF / 4 ) {
536
- err = tcp_output (socket -> pcb .tcp );
540
+ // Use nagle algorithm to determine when to send segment buffer (can be
541
+ // disabled with TCP_NODELAY socket option)
542
+ if (err == ERR_OK ) {
543
+ err = tcp_output_nagle (socket -> pcb .tcp );
537
544
}
538
545
539
546
MICROPY_PY_LWIP_EXIT
@@ -551,6 +558,12 @@ static mp_uint_t lwip_tcp_receive(socketpool_socket_obj_t *socket, byte *buf, mp
551
558
// Check for any pending errors
552
559
STREAM_ERROR_CHECK (socket );
553
560
561
+ if (socket -> state == STATE_LISTENING ) {
562
+ // original socket in listening state, not the accepted connection.
563
+ * _errno = MP_ENOTCONN ;
564
+ return -1 ;
565
+ }
566
+
554
567
if (socket -> incoming .pbuf == NULL ) {
555
568
556
569
// Non-blocking socket
0 commit comments