1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-10 04:22:05 +03:00

lwip2 updates: no more git sub-sub-module deps, faster checksum, backlog limitation and other fixes (#6887)

* upstream lwIP is now downloaded by a makefile, not subsubmoduled

* lwip2: upstream lwIP not sub-sub-modules anymore
lwip2: Allow IPv4 and IPv6 DNS and SNTP server configured via DHCP to co-exist (patch against upstream)

* lwip2: enable tcp-listen-with-backlog feature

* lwip2 submodule update:
- enable more efficient chksum algorithm thanks to Richard Allen
- enable tcp listener with backlog

* more comments, fix backlog management, fix API
* move default value definition in .cpp
because one must not believe it can be redefined before including WiFiServer.h

* improved backlog handling, it is no more a breaking change
This commit is contained in:
david gauchard
2020-02-18 06:54:50 +01:00
committed by GitHub
parent bc4f000c48
commit e752e96e9f
14 changed files with 134 additions and 34 deletions

View File

@ -35,12 +35,17 @@ extern "C" {
#include "lwip/opt.h"
#include "lwip/tcp.h"
#include "lwip/inet.h"
#include "lwip/init.h" // LWIP_VERSION_
#include <include/ClientContext.h>
#ifndef MAX_PENDING_CLIENTS_PER_PORT
#define MAX_PENDING_CLIENTS_PER_PORT 5
#endif
WiFiServer::WiFiServer(const IPAddress& addr, uint16_t port)
: _port(port)
, _addr(addr)
, _pcb(nullptr)
, _listen_pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
{
@ -49,7 +54,7 @@ WiFiServer::WiFiServer(const IPAddress& addr, uint16_t port)
WiFiServer::WiFiServer(uint16_t port)
: _port(port)
, _addr(IP_ANY_TYPE)
, _pcb(nullptr)
, _listen_pcb(nullptr)
, _unclaimed(nullptr)
, _discarded(nullptr)
{
@ -60,9 +65,14 @@ void WiFiServer::begin() {
}
void WiFiServer::begin(uint16_t port) {
return begin(port, MAX_PENDING_CLIENTS_PER_PORT);
}
void WiFiServer::begin(uint16_t port, uint8_t backlog) {
close();
if (!backlog)
return;
_port = port;
err_t err;
tcp_pcb* pcb = tcp_new();
if (!pcb)
return;
@ -70,20 +80,23 @@ void WiFiServer::begin(uint16_t port) {
pcb->so_options |= SOF_REUSEADDR;
// (IPAddress _addr) operator-converted to (const ip_addr_t*)
err = tcp_bind(pcb, _addr, _port);
if (err != ERR_OK) {
if (tcp_bind(pcb, _addr, _port) != ERR_OK) {
tcp_close(pcb);
return;
}
#if LWIP_VERSION_MAJOR == 1
tcp_pcb* listen_pcb = tcp_listen(pcb);
#else
tcp_pcb* listen_pcb = tcp_listen_with_backlog(pcb, backlog);
#endif
if (!listen_pcb) {
tcp_close(pcb);
return;
}
_pcb = listen_pcb;
_port = _pcb->local_port;
_listen_pcb = listen_pcb;
_port = _listen_pcb->local_port;
tcp_accept(listen_pcb, &WiFiServer::_s_accept);
tcp_arg(listen_pcb, (void*) this);
}
@ -111,6 +124,10 @@ WiFiClient WiFiServer::available(byte* status) {
(void) status;
if (_unclaimed) {
WiFiClient result(_unclaimed);
#if LWIP_VERSION_MAJOR != 1
_unclaimed->acceptPCB();
tcp_backlog_accepted(_unclaimed->getPCB());
#endif
_unclaimed = _unclaimed->next();
result.setNoDelay(getNoDelay());
DEBUGV("WS:av\r\n");
@ -122,9 +139,9 @@ WiFiClient WiFiServer::available(byte* status) {
}
uint8_t WiFiServer::status() {
if (!_pcb)
if (!_listen_pcb)
return CLOSED;
return _pcb->state;
return _listen_pcb->state;
}
uint16_t WiFiServer::port() const {
@ -132,11 +149,11 @@ uint16_t WiFiServer::port() const {
}
void WiFiServer::close() {
if (!_pcb) {
if (!_listen_pcb) {
return;
}
tcp_close(_pcb);
_pcb = nullptr;
tcp_close(_listen_pcb);
_listen_pcb = nullptr;
}
void WiFiServer::stop() {
@ -169,9 +186,36 @@ T* slist_append_tail(T* head, T* item) {
long WiFiServer::_accept(tcp_pcb* apcb, long err) {
(void) err;
DEBUGV("WS:ac\r\n");
#if LWIP_VERSION_MAJOR == 1
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this);
tcp_accepted(_listen_pcb);
#else
// backlog doc:
// http://lwip.100.n7.nabble.com/Problem-re-opening-listening-pbc-tt32484.html#a32494
// https://www.nongnu.org/lwip/2_1_x/group__tcp__raw.html#gaeff14f321d1eecd0431611f382fcd338
// lwip-v2: Tell ClientContext to not accept yet the connection (final 'false' below)
ClientContext* client = new ClientContext(apcb, &WiFiServer::_s_discard, this, false);
// increase lwIP's backlog
tcp_backlog_delayed(apcb);
// Optimization Path:
// when lwip-v1.4 is not allowed anymore,
// - _accept() should not create ClientContext anymore
// - apcb should be stored into some sort of linked list (->_unclaimed)
// (the linked list would store tcp_pcb* instead of ClientContext*)
// (TCP_PCB_EXTARGS might be used for that (lwIP's struct tcp_pcb))
// - on available(), get the pcb back and create the ClientContext
// (this is not done today for better source readability with lwip-1.4 around)
#endif
_unclaimed = slist_append_tail(_unclaimed, client);
tcp_accepted(_pcb);
return ERR_OK;
}