WiFiSever - Arduino API available() and accept() by JAndrassy · Pull Request #7605 · esp8266/Arduino · GitHub
Skip to content

WiFiSever - Arduino API available() and accept()#7605

Closed
JAndrassy wants to merge 1 commit into
esp8266:masterfrom
JAndrassy:server_api_pr
Closed

WiFiSever - Arduino API available() and accept()#7605
JAndrassy wants to merge 1 commit into
esp8266:masterfrom
JAndrassy:server_api_pr

Conversation

@JAndrassy

@JAndrassy JAndrassy commented Sep 17, 2020

Copy link
Copy Markdown
Contributor

According to documentation, the server.available() should return first client with data available.
https://www.arduino.cc/en/Reference/WiFiServerAvailable (esp8266 doc points there).

ESP8266WiFi library now does what a new EthernetServer.accept() method does: https://www.arduino.cc/en/Reference/EthernetServerAccept. It was added to Ethernet library by Paul Stoffregen in 2018 for Ethernet 2.0. I added it to UIPEthernet in February and I have it in my WiFiEspAT library too.

This PR shows a draft of a possible implementation of available(). (This quick implementation could let some client wait to long.)

My opinion is that it is strange to have a function like this available(). My explanation how they defined it like this, is that the networking libraries by Arduino are always remote APIs for a firmware and there it doesn't look so strange.

@d-a-v

d-a-v commented Sep 20, 2020

Copy link
Copy Markdown
Collaborator

@JAndrassy

Copy link
Copy Markdown
Contributor Author

In the years esp8266 Arduino exists, missed someone the true Arduino behaviour of available()? I think not.
If it would be my call, it would add accept() as alias to available() and document available() as it is now.

@JAndrassy

Copy link
Copy Markdown
Contributor Author

now I realized that the Arduino implementation of available() is tied with the write-to-all-clients functionality of Server base class as Print implementation. this is not implemented in ESP8266WiFi library.

WiFiServer telnetLogging; where every telnetLogging.print goes to all connected clients (or is ignored if there is no connected client) could be passed to any function or class which would take Print& log.

maybe a WiFiArduinoServer could inherit from WiFiServer and add the Arduino style available() function and the write-to-all-clients functionality. (same for SSL server)

@d-a-v

d-a-v commented Sep 21, 2020

Copy link
Copy Markdown
Collaborator

maybe a WiFiArduinoServer could inherit from WiFiServer and add the Arduino style available() function and the write-to-all-clients functionality. (same for SSL server)

That would be a very good first step.

@JAndrassy

Copy link
Copy Markdown
Contributor Author

maybe a WiFiArduinoServer could inherit from WiFiServer and add the Arduino style available() function and the write-to-all-clients functionality. (same for SSL server)

That would be a very good first step.

I rework this PR. Should I use WiFiClient or ClientContext to store the clients? (ClientContext with ref and unref)

@d-a-v

d-a-v commented Sep 21, 2020

Copy link
Copy Markdown
Collaborator

I think you have noticed that WiFiClient is just a wrapper around ClientContext.
If you don't intend to indeed use but only store the client, just use the simple version.

@JAndrassy

Copy link
Copy Markdown
Contributor Author

this will be the example called PagerServer, a simple server that echoes any incoming messages to all connected telnet clients.

void loop() {
  WiFiClient client = server.available(); // returns first client which has data to read
  if (client) { // client is true only if it is connected and has data to read
    String s = client.readStringUntil('\n'); // read the message incoming from one of the clients
    Serial.println(s); // print the message to Serial Monitor
    client.print("echo: "); // this is only for sending client
    server.println(s); // send the message to all connected clients
    server.flush(); // flush the buffers
  }
}

I think it is Arduino way elegant.

https://github.com/jandrassy/WiFiEspAT/blob/master/examples/Basic/PagerServer/PagerServer.ino

@JAndrassy JAndrassy closed this Sep 26, 2020
@JAndrassy

Copy link
Copy Markdown
Contributor Author

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