|
69 | 69 | #define IP_ADD_MEMBERSHIP 0x400
|
70 | 70 | #define IP_DROP_MEMBERSHIP 0x401
|
71 | 71 |
|
| 72 | +#define TCP_NODELAY TF_NODELAY |
| 73 | + |
72 | 74 | // For compatibilily with older lwIP versions.
|
73 | 75 | #ifndef ip_set_option
|
74 | 76 | #define ip_set_option(pcb, opt) ((pcb)->so_options |= (opt))
|
75 | 77 | #endif
|
76 | 78 | #ifndef ip_reset_option
|
77 | 79 | #define ip_reset_option(pcb, opt) ((pcb)->so_options &= ~(opt))
|
78 | 80 | #endif
|
| 81 | +#ifndef tcp_set_flags |
| 82 | +#define tcp_set_flags(pcb, set_flags) do { (pcb)->flags |= (set_flags); } while (0) |
| 83 | +#endif |
| 84 | +#ifndef tcp_clear_flags |
| 85 | +#define tcp_clear_flags(pcb, clear_flags) do { (pcb)->flags &= ~(clear_flags); } while (0) |
| 86 | +#endif |
79 | 87 |
|
80 | 88 | // A port can define these hooks to provide concurrency protection
|
81 | 89 | #ifndef MICROPY_PY_LWIP_ENTER
|
@@ -742,9 +750,10 @@ static mp_uint_t lwip_tcp_send(lwip_socket_obj_t *socket, const byte *buf, mp_ui
|
742 | 750 | MICROPY_PY_LWIP_REENTER
|
743 | 751 | }
|
744 | 752 |
|
745 |
| - // If the output buffer is getting full then send the data to the lower layers |
746 |
| - if (err == ERR_OK && tcp_sndbuf(socket->pcb.tcp) < TCP_SND_BUF / 4) { |
747 |
| - err = tcp_output(socket->pcb.tcp); |
| 753 | + // Use nagle algorithm to determine when to send segment buffer (can be |
| 754 | + // disabled with TCP_NODELAY socket option) |
| 755 | + if (err == ERR_OK) { |
| 756 | + err = tcp_output_nagle(socket->pcb.tcp); |
748 | 757 | }
|
749 | 758 |
|
750 | 759 | MICROPY_PY_LWIP_EXIT
|
@@ -1440,6 +1449,17 @@ static mp_obj_t lwip_socket_setsockopt(size_t n_args, const mp_obj_t *args) {
|
1440 | 1449 | break;
|
1441 | 1450 | }
|
1442 | 1451 |
|
| 1452 | + // level: IPPROTO_TCP |
| 1453 | + case TCP_NODELAY: { |
| 1454 | + mp_int_t val = mp_obj_get_int(args[3]); |
| 1455 | + if (val) { |
| 1456 | + tcp_set_flags(socket->pcb.tcp, opt); |
| 1457 | + } else { |
| 1458 | + tcp_clear_flags(socket->pcb.tcp, opt); |
| 1459 | + } |
| 1460 | + break; |
| 1461 | + } |
| 1462 | + |
1443 | 1463 | default:
|
1444 | 1464 | printf("Warning: lwip.setsockopt() not implemented\n");
|
1445 | 1465 | }
|
@@ -1829,6 +1849,9 @@ static const mp_rom_map_elem_t mp_module_lwip_globals_table[] = {
|
1829 | 1849 | { MP_ROM_QSTR(MP_QSTR_IPPROTO_IP), MP_ROM_INT(0) },
|
1830 | 1850 | { MP_ROM_QSTR(MP_QSTR_IP_ADD_MEMBERSHIP), MP_ROM_INT(IP_ADD_MEMBERSHIP) },
|
1831 | 1851 | { MP_ROM_QSTR(MP_QSTR_IP_DROP_MEMBERSHIP), MP_ROM_INT(IP_DROP_MEMBERSHIP) },
|
| 1852 | + |
| 1853 | + { MP_ROM_QSTR(MP_QSTR_IPPROTO_TCP), MP_ROM_INT(IP_PROTO_TCP) }, |
| 1854 | + { MP_ROM_QSTR(MP_QSTR_TCP_NODELAY), MP_ROM_INT(TCP_NODELAY) }, |
1832 | 1855 | };
|
1833 | 1856 |
|
1834 | 1857 | static MP_DEFINE_CONST_DICT(mp_module_lwip_globals, mp_module_lwip_globals_table);
|
|
0 commit comments