Skip to content

Conversation

@kaabia
Copy link

@kaabia kaabia commented Nov 9, 2025

This PR implements significant refactoring and stabilization for the EthernetServer class to handle hardware limits and TCP state transitions more robustly.

Summary of Improvements:

  1. Code Abstraction: The repetitive W5100 4-socket limit check is abstracted into the WIZNET_CHIP_LIMIT macro, reducing redundant code in available(), accept(), write(), and operator bool().
  2. Centralized Cleanup: Introduces a private helper, void _socketCleanup(), to handle client disconnections (CLOSE_WAIT) and close out unused CLOSED sockets, making the main public methods cleaner and more focused on their primary duties.
  3. Bug Fix (Server Initialization): Corrects EthernetServer::begin() to use Ethernet.socketClose() (instead of socketDisconnect()) if the initial socketListen fails, ensuring the socket is correctly returned to the available pool.
  4. Bug Fix (Write Function): Fixes a critical bug in write(const uint8_t*, size_t) where it now correctly accumulates and returns the actual sent_data length, rather than returning the requested buffer size regardless of success.
  5. Compilation Fix: Ensures the chip variable is correctly declared in operator bool() to satisfy the requirements of the WIZNET_CHIP_LIMIT macro.

…leanup

Reworks the EthernetServer class to improve maintainability, stabilize socket
management, and fix critical bugs related to data writing and port cleanup.

Key fixes and refactors:

- Abstracted W5100 socket limit logic into WIZNET_CHIP_LIMIT macro.
- Introduced _socketCleanup() for centralized CLOSE_WAIT/CLOSED logic.
- Fixed write() to correctly return sent byte count.
- Fixed begin() to use socketClose() on listen failure.
- Ensured proper variable declaration for operator bool().

Signed-off-by: Badr Bacem KAABIA <badrbacemkaabia@gmail.com>
@CLAassistant
Copy link

CLAassistant commented Nov 9, 2025

CLA assistant check
All committers have signed the CLA.

@kaabia kaabia changed the title (ethernet_server): Refactor: Stabilize EthernetServer and fix initialization bug (ethernet_server): Refactor EthernetServer and fix initialization bug Nov 9, 2025
@github-actions
Copy link

github-actions bot commented Nov 9, 2025

Memory usage change @ 1228ffe

Board flash % RAM for global variables %
arduino:avr:leonardo 💚 -62 - 0 -0.22 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:mega 💚 -62 - 0 -0.02 - 0.0 0 - 0 0.0 - 0.0
arduino:avr:nano 💚 -58 - 0 -0.19 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:nona4809 💚 -62 - 0 -0.13 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:uno2018 💚 -62 - 0 -0.13 - 0.0 0 - 0 0.0 - 0.0
arduino:sam:arduino_due_x_dbg ❔ -120 - +40 -0.02 - +0.01 N/A N/A
arduino:samd:arduino_zero_edbg ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrfox1200 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:mkrzero ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot ❔ -104 - +60 -0.04 - +0.02 0 - 0 0.0 - 0.0
Click for full report table
Board examples/AdvancedChatServer
flash
% examples/AdvancedChatServer
RAM for global variables
% examples/BarometricPressureWebServer
flash
% examples/BarometricPressureWebServer
RAM for global variables
% examples/ChatServer
flash
% examples/ChatServer
RAM for global variables
% examples/DhcpAddressPrinter
flash
% examples/DhcpAddressPrinter
RAM for global variables
% examples/DhcpChatServer
flash
% examples/DhcpChatServer
RAM for global variables
% examples/LinkStatus
flash
% examples/LinkStatus
RAM for global variables
% examples/PagerServer
flash
% examples/PagerServer
RAM for global variables
% examples/TelnetClient
flash
% examples/TelnetClient
RAM for global variables
% examples/UDPSendReceiveString
flash
% examples/UDPSendReceiveString
RAM for global variables
% examples/UdpNtpClient
flash
% examples/UdpNtpClient
RAM for global variables
% examples/WebClient
flash
% examples/WebClient
RAM for global variables
% examples/WebClientRepeating
flash
% examples/WebClientRepeating
RAM for global variables
% examples/WebServer
flash
% examples/WebServer
RAM for global variables
%
arduino:avr:leonardo -62 -0.22 0 0.0 -22 -0.08 0 0.0 -12 -0.04 0 0.0 -26 -0.09 0 0.0 -24 -0.08 0 0.0 0 0.0 0 0.0 -22 -0.08 0 0.0 0 0.0 0 0.0 -26 -0.09 0 0.0 -26 -0.09 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -14 -0.05 0 0.0
arduino:avr:mega -62 -0.02 0 0.0 -24 -0.01 0 0.0 -12 -0.0 0 0.0 -26 -0.01 0 0.0 -34 -0.01 0 0.0 0 0.0 0 0.0 -16 -0.01 0 0.0 0 0.0 0 0.0 -26 -0.01 0 0.0 -26 -0.01 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -14 -0.01 0 0.0
arduino:avr:nano -58 -0.19 0 0.0 -46 -0.15 0 0.0 -28 -0.09 0 0.0 -26 -0.08 0 0.0 -56 -0.18 0 0.0 0 0.0 0 0.0 -42 -0.14 0 0.0 0 0.0 0 0.0 -26 -0.08 0 0.0 -26 -0.08 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -34 -0.11 0 0.0
arduino:megaavr:nona4809 -62 -0.13 0 0.0 -2 -0.0 0 0.0 0 0.0 0 0.0 -26 -0.05 0 0.0 -14 -0.03 0 0.0 0 0.0 0 0.0 -12 -0.02 0 0.0 0 0.0 0 0.0 -26 -0.05 0 0.0 -26 -0.05 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -6 -0.01 0 0.0
arduino:megaavr:uno2018 -62 -0.13 0 0.0 -2 -0.0 0 0.0 0 0.0 0 0.0 -26 -0.05 0 0.0 -14 -0.03 0 0.0 0 0.0 0 0.0 -12 -0.02 0 0.0 0 0.0 0 0.0 -26 -0.05 0 0.0 -26 -0.05 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 -4 -0.01 0 0.0
arduino:sam:arduino_due_x_dbg -120 -0.02 N/A N/A 40 0.01 N/A N/A 40 0.01 N/A N/A 0 0.0 N/A N/A 40 0.01 N/A N/A 0 0.0 N/A N/A 40 0.01 N/A N/A 0 0.0 N/A N/A 0 0.0 N/A N/A 0 0.0 N/A N/A 0 0.0 N/A N/A 0 0.0 N/A N/A 40 0.01 N/A N/A
arduino:samd:arduino_zero_edbg -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkr1000 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrfox1200 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrgsm1400 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrnb1500 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrvidor4000 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrwan1300 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrwan1310 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrwifi1010 -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:mkrzero -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
arduino:samd:nano_33_iot -104 -0.04 0 0.0 60 0.02 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 60 0.02 0 0.0
Click for full report CSV
Board,examples/AdvancedChatServer<br>flash,%,examples/AdvancedChatServer<br>RAM for global variables,%,examples/BarometricPressureWebServer<br>flash,%,examples/BarometricPressureWebServer<br>RAM for global variables,%,examples/ChatServer<br>flash,%,examples/ChatServer<br>RAM for global variables,%,examples/DhcpAddressPrinter<br>flash,%,examples/DhcpAddressPrinter<br>RAM for global variables,%,examples/DhcpChatServer<br>flash,%,examples/DhcpChatServer<br>RAM for global variables,%,examples/LinkStatus<br>flash,%,examples/LinkStatus<br>RAM for global variables,%,examples/PagerServer<br>flash,%,examples/PagerServer<br>RAM for global variables,%,examples/TelnetClient<br>flash,%,examples/TelnetClient<br>RAM for global variables,%,examples/UDPSendReceiveString<br>flash,%,examples/UDPSendReceiveString<br>RAM for global variables,%,examples/UdpNtpClient<br>flash,%,examples/UdpNtpClient<br>RAM for global variables,%,examples/WebClient<br>flash,%,examples/WebClient<br>RAM for global variables,%,examples/WebClientRepeating<br>flash,%,examples/WebClientRepeating<br>RAM for global variables,%,examples/WebServer<br>flash,%,examples/WebServer<br>RAM for global variables,%
arduino:avr:leonardo,-62,-0.22,0,0.0,-22,-0.08,0,0.0,-12,-0.04,0,0.0,-26,-0.09,0,0.0,-24,-0.08,0,0.0,0,0.0,0,0.0,-22,-0.08,0,0.0,0,0.0,0,0.0,-26,-0.09,0,0.0,-26,-0.09,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-14,-0.05,0,0.0
arduino:avr:mega,-62,-0.02,0,0.0,-24,-0.01,0,0.0,-12,-0.0,0,0.0,-26,-0.01,0,0.0,-34,-0.01,0,0.0,0,0.0,0,0.0,-16,-0.01,0,0.0,0,0.0,0,0.0,-26,-0.01,0,0.0,-26,-0.01,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-14,-0.01,0,0.0
arduino:avr:nano,-58,-0.19,0,0.0,-46,-0.15,0,0.0,-28,-0.09,0,0.0,-26,-0.08,0,0.0,-56,-0.18,0,0.0,0,0.0,0,0.0,-42,-0.14,0,0.0,0,0.0,0,0.0,-26,-0.08,0,0.0,-26,-0.08,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-34,-0.11,0,0.0
arduino:megaavr:nona4809,-62,-0.13,0,0.0,-2,-0.0,0,0.0,0,0.0,0,0.0,-26,-0.05,0,0.0,-14,-0.03,0,0.0,0,0.0,0,0.0,-12,-0.02,0,0.0,0,0.0,0,0.0,-26,-0.05,0,0.0,-26,-0.05,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-6,-0.01,0,0.0
arduino:megaavr:uno2018,-62,-0.13,0,0.0,-2,-0.0,0,0.0,0,0.0,0,0.0,-26,-0.05,0,0.0,-14,-0.03,0,0.0,0,0.0,0,0.0,-12,-0.02,0,0.0,0,0.0,0,0.0,-26,-0.05,0,0.0,-26,-0.05,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,-4,-0.01,0,0.0
arduino:sam:arduino_due_x_dbg,-120,-0.02,N/A,N/A,40,0.01,N/A,N/A,40,0.01,N/A,N/A,0,0.0,N/A,N/A,40,0.01,N/A,N/A,0,0.0,N/A,N/A,40,0.01,N/A,N/A,0,0.0,N/A,N/A,0,0.0,N/A,N/A,0,0.0,N/A,N/A,0,0.0,N/A,N/A,0,0.0,N/A,N/A,40,0.01,N/A,N/A
arduino:samd:arduino_zero_edbg,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkr1000,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrfox1200,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrgsm1400,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrnb1500,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrvidor4000,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrwan1300,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrwan1310,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrwifi1010,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:mkrzero,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0
arduino:samd:nano_33_iot,-104,-0.04,0,0.0,60,0.02,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,60,0.02,0,0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants