Ethernet
Ethernet copied to clipboard
(ethernet_server): Refactor EthernetServer and fix initialization bug
This PR implements significant refactoring and stabilization for the EthernetServer class to handle hardware limits and TCP state transitions more robustly.
Summary of Improvements:
-
Code Abstraction: The repetitive W5100 4-socket limit check is abstracted into the
WIZNET_CHIP_LIMITmacro, reducing redundant code inavailable(),accept(),write(), andoperator bool(). -
Centralized Cleanup: Introduces a private helper,
void _socketCleanup(), to handle client disconnections (CLOSE_WAIT) and close out unusedCLOSEDsockets, making the main public methods cleaner and more focused on their primary duties. -
Bug Fix (Server Initialization): Corrects
EthernetServer::begin()to useEthernet.socketClose()(instead ofsocketDisconnect()) if the initialsocketListenfails, ensuring the socket is correctly returned to the available pool. -
Bug Fix (Write Function): Fixes a critical bug in
write(const uint8_t*, size_t)where it now correctly accumulates and returns the actualsent_datalength, rather than returning the requested buffersizeregardless of success. -
Compilation Fix: Ensures the
chipvariable is correctly declared inoperator bool()to satisfy the requirements of theWIZNET_CHIP_LIMITmacro.