@@ -89,7 +89,7 @@ static mp_obj_t socketpool_ip_addr_and_port_to_tuple(const ip_addr_t *addr, int
8989// socket API.
9090
9191// Extension to lwIP error codes
92- // Matches lwIP 2.0.3
92+ // Matches lwIP 2.2.1
9393#undef _ERR_BADF
9494#define _ERR_BADF -17
9595static const int error_lookup_table [] = {
@@ -473,7 +473,12 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
473473
474474 MICROPY_PY_LWIP_ENTER
475475
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+ }
477482
478483 if (available == 0 ) {
479484 // Non-blocking socket
@@ -490,7 +495,8 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
490495 // If peer fully closed socket, we would have socket->state set to ERR_RST (connection
491496 // reset) by error callback.
492497 // 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 )) {
494500 MICROPY_PY_LWIP_EXIT
495501 if (socket -> timeout != (unsigned )-1 && mp_hal_ticks_ms () - start > socket -> timeout ) {
496502 * _errno = MP_ETIMEDOUT ;
@@ -531,9 +537,10 @@ static mp_uint_t lwip_tcp_send(socketpool_socket_obj_t *socket, const byte *buf,
531537 MICROPY_PY_LWIP_REENTER
532538 }
533539
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 );
537544 }
538545
539546 MICROPY_PY_LWIP_EXIT
@@ -551,6 +558,12 @@ static mp_uint_t lwip_tcp_receive(socketpool_socket_obj_t *socket, byte *buf, mp
551558 // Check for any pending errors
552559 STREAM_ERROR_CHECK (socket );
553560
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+
554567 if (socket -> incoming .pbuf == NULL ) {
555568
556569 // Non-blocking socket
0 commit comments