ArduinoCore-mbed icon indicating copy to clipboard operation
ArduinoCore-mbed copied to clipboard

SocketWrapper - MbedServer modernization (without available() and Print)

Open JAndrassy opened this issue 2 years ago • 1 comments

MbedServer didn't manage clients for proper available() and print-to-all-clients. Now available() in derived server classes WiFiClient and EthernetClient is deprecated and accept() is added with the same implementation. Inheriting from Print (Server) is removed. write methods for Print implementation are removed. They never worked.

New are constructor without parameters, begin with parameter port and end() as in the new WiFiS3 library.

The ChatServer examples never worked because they relay on Processing style server.available() and print-to-all-clients.. Removed.

ESP32 core and RP2040 core libraries too don't implement Processing style Servers. Processing style WiFiServer and EthernetServer for these cores and Mbed core is implemented in my NetAPIHelpers library. In my networking libraries WiFiEspAT and EthenetENC the print-to-all-clients is in separate class and in next major version I plan to remove server.available() and point to NetAPIHelpers Server.

This replaces PR https://github.com/arduino/ArduinoCore-mbed/pull/750 and https://github.com/arduino/ArduinoCore-mbed/pull/751

overview of Server implementations in libraries https://github.com/JAndrassy/Arduino-Networking-API/blob/main/ArduinoNetAPILibs.md#server-class

JAndrassy avatar Dec 06 '23 19:12 JAndrassy

@facchinm @pennam

now this is my last essential PR for Mbed Core networking. It does changes proposed by Alessandro Ranellucci, but including the consequences

https://github.com/arduino/ArduinoCore-mbed/pull/750 adds accept() in WiFiServer

consequences:

  • add accept in EthernetServer
  • deprecate available
  • modify examples to use accept() and remove Ethernet examples which require true server.available() (WiFi lib doesn't have them)
  • remove unimplemented Print inheritance and write methods, because print-to-all-clients is tied with server.available()

https://github.com/arduino/ArduinoCore-mbed/pull/751 adds end() in WiFiServer

consequences:

  • add end() in EthernetServer

other related server classes modernization as in WiFiS3:

  • add begin with parameter port and constructor without parameters
  • add operator bool

Some context:

The server.available() originates in Processing. It is an idea to simplify a TCP server for artists. But as the lack of proper server.available() here and in multiple third party libraries shows, users not expect that behavior. In most request-response-stop use-cases it doesn't even matter.

Class Server inherits from Print for the print-to-all-clients functionality. This functionality is tied with the available() method implementation. The implementation of the Server class should register all connected clients for correct implementation of available() and for print-to-all-clients functionality.

The base class Server can't declare required server methods available() and accept(), because they have the return type of the specific Client implementation of the library (for example EthernetClient in the Ethernet library). As a consequence, It is not possible to use the base class Server to work with an instance of an inherited server class. So Server is just disguised Print and without Print usage inheriting from Server class is useless.

JAndrassy avatar Feb 05 '24 10:02 JAndrassy

it is just a comment which is not true. it is same in the Ethernet library from where the example originates. I guess it had Serial.write in " // check for incoming data from all clients" section at some point in time.

https://github.com/arduino-libraries/Ethernet/commit/2304c69e08450ce509fdcca5a01f6a514136a93c

JAndrassy avatar Oct 03 '24 12:10 JAndrassy