From 18b696b224f8c621cd95fb099e63e25c68245c96 Mon Sep 17 00:00:00 2001 From: Juraj Andrassy Date: Wed, 6 Dec 2023 20:34:11 +0100 Subject: [PATCH] SocketWrapper - MbedServer modernization (without available() and Print) MbedServer didn't manage clients for proper available() and print-to-all-clients. Now available() in derived server classes 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 operator bool. --- .../AdvancedChatServer/AdvancedChatServer.ino | 2 +- .../BarometricPressureWebServer.ino | 2 +- .../examples/ChatServer/ChatServer.ino | 97 ------------------- .../DhcpChatServer/DhcpChatServer.ino | 91 ----------------- .../Ethernet/examples/WebServer/WebServer.ino | 2 +- libraries/Ethernet/src/EthernetServer.cpp | 4 + libraries/Ethernet/src/EthernetServer.h | 4 +- libraries/SocketWrapper/src/MbedServer.cpp | 26 ++--- libraries/SocketWrapper/src/MbedServer.h | 17 ++-- libraries/WiFi/src/WiFiServer.cpp | 4 + libraries/WiFi/src/WiFiServer.h | 4 +- 11 files changed, 35 insertions(+), 218 deletions(-) delete mode 100644 libraries/Ethernet/examples/ChatServer/ChatServer.ino delete mode 100644 libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino diff --git a/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino b/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino index 630f27240..cde393ed4 100644 --- a/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino +++ b/libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino @@ -67,7 +67,7 @@ void setup() { void loop() { // check for any new client connecting, and say hello (before any incoming data) - EthernetClient newClient = server.available(); + EthernetClient newClient = server.accept(); if (newClient) { for (byte i=0; i < 8; i++) { if (!clients[i]) { diff --git a/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino b/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino index 6f26ef888..37ba334c4 100644 --- a/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino +++ b/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino @@ -141,7 +141,7 @@ void getData() { void listenForEthernetClients() { // listen for incoming clients - EthernetClient client = server.available(); + EthernetClient client = server.accept(); if (client) { Serial.println("Got a client"); // an http request ends with a blank line diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino deleted file mode 100644 index bfc310b9c..000000000 --- a/libraries/Ethernet/examples/ChatServer/ChatServer.ino +++ /dev/null @@ -1,97 +0,0 @@ -/* - Chat Server - - A simple server that distributes any incoming messages to all - connected clients. To use, telnet to your device's IP address and type. - You can see the client's input in the serial monitor as well. - Using an Arduino Wiznet Ethernet shield. - - Circuit: - * Ethernet shield attached to pins 10, 11, 12, 13 - - created 18 Dec 2009 - by David A. Mellis - modified 9 Apr 2012 - by Tom Igoe - - */ - -#include -#include -#include - -// The IP address will be dependent on your local network. -// gateway and subnet are optional: -IPAddress ip(192, 168, 1, 177); -IPAddress myDns(192, 168, 1, 1); -IPAddress gateway(192, 168, 1, 1); -IPAddress subnet(255, 255, 0, 0); - - -// telnet defaults to port 23 -EthernetServer server(23); -boolean alreadyConnected = false; // whether or not the client was connected previously - -void setup() { - // You can use Ethernet.init(pin) to configure the CS pin - //Ethernet.init(10); // Most Arduino shields - //Ethernet.init(5); // MKR ETH shield - //Ethernet.init(0); // Teensy 2.0 - //Ethernet.init(20); // Teensy++ 2.0 - //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet - //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet - - // initialize the ethernet device - Ethernet.begin(ip, myDns, gateway, subnet); - - // Open serial communications and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // Check for Ethernet hardware present - if (Ethernet.hardwareStatus() == EthernetNoHardware) { - Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); - while (true) { - delay(1); // do nothing, no point running without Ethernet hardware - } - } - if (Ethernet.linkStatus() == LinkOFF) { - Serial.println("Ethernet cable is not connected."); - } - - // start listening for clients - server.begin(); - - Serial.print("Chat server address:"); - Serial.println(Ethernet.localIP()); -} - -void loop() { - // wait for a new client: - EthernetClient client = server.available(); - - // when the client sends the first byte, say hello: - if (client) { - if (!alreadyConnected) { - // clear out the input buffer: - client.flush(); - Serial.println("We have a new client"); - client.println("Hello, client!"); - alreadyConnected = true; - } - - if (client.available() > 0) { - // read the bytes incoming from the client: - char thisChar = client.read(); - // echo the bytes back to the client: - server.write(thisChar); - // echo the bytes to the server as well: - Serial.write(thisChar); - } - } -} - - - diff --git a/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino b/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino deleted file mode 100644 index cbf2deadd..000000000 --- a/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino +++ /dev/null @@ -1,91 +0,0 @@ -/* - DHCP Chat Server - - A simple server that distributes any incoming messages to all - connected clients. To use, telnet to your device's IP address and type. - You can see the client's input in the serial monitor as well. - Using an Arduino Wiznet Ethernet shield. - - THis version attempts to get an IP address using DHCP - - Circuit: - * Ethernet shield attached to pins 10, 11, 12, 13 - - created 21 May 2011 - modified 9 Apr 2012 - by Tom Igoe - modified 02 Sept 2015 - by Arturo Guadalupi - Based on ChatServer example by David A. Mellis - - */ - -#include -#include -#include - -// The IP address will be dependent on your local network. -// gateway and subnet are optional: -IPAddress ip(192, 168, 1, 177); -IPAddress myDns(192, 168, 1, 1); -IPAddress gateway(192, 168, 1, 1); -IPAddress subnet(255, 255, 0, 0); - -// telnet defaults to port 23 -EthernetServer server(23); -boolean gotAMessage = false; // whether or not you got a message from the client yet - -void setup() { - - // Open serial communications and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } - - // start the Ethernet connection: - Serial.println("Trying to get an IP address using DHCP"); - if (Ethernet.begin() == 0) { - Serial.println("Failed to configure Ethernet using DHCP"); - // Check for Ethernet hardware present - if (Ethernet.hardwareStatus() == EthernetNoHardware) { - Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); - while (true) { - delay(1); // do nothing, no point running without Ethernet hardware - } - } - if (Ethernet.linkStatus() == LinkOFF) { - Serial.println("Ethernet cable is not connected."); - } - // initialize the Ethernet device not using DHCP: - Ethernet.begin(ip, myDns, gateway, subnet); - } - // print your local IP address: - Serial.print("My IP address: "); - Serial.println(Ethernet.localIP()); - - // start listening for clients - server.begin(); -} - -void loop() { - // wait for a new client: - EthernetClient client = server.available(); - - // when the client sends the first byte, say hello: - if (client) { - if (!gotAMessage) { - Serial.println("We have a new client"); - client.println("Hello, client!"); - gotAMessage = true; - } - - // read the bytes incoming from the client: - char thisChar = client.read(); - // echo the bytes back to the client: - server.write(thisChar); - // echo the bytes to the server as well: - Serial.print(thisChar); - } -} - diff --git a/libraries/Ethernet/examples/WebServer/WebServer.ino b/libraries/Ethernet/examples/WebServer/WebServer.ino index 6201495f7..8c712fd02 100644 --- a/libraries/Ethernet/examples/WebServer/WebServer.ino +++ b/libraries/Ethernet/examples/WebServer/WebServer.ino @@ -61,7 +61,7 @@ void setup() { void loop() { // listen for incoming clients - EthernetClient client = server.available(); + EthernetClient client = server.accept(); if (client) { Serial.println("new client"); // an http request ends with a blank line diff --git a/libraries/Ethernet/src/EthernetServer.cpp b/libraries/Ethernet/src/EthernetServer.cpp index 94f5854b6..7a383198a 100644 --- a/libraries/Ethernet/src/EthernetServer.cpp +++ b/libraries/Ethernet/src/EthernetServer.cpp @@ -1,6 +1,10 @@ #include "EthernetServer.h" arduino::EthernetClient arduino::EthernetServer::available(uint8_t* status) { + return accept(status); +} + +arduino::EthernetClient arduino::EthernetServer::accept(uint8_t* status) { EthernetClient client; nsapi_error_t error; diff --git a/libraries/Ethernet/src/EthernetServer.h b/libraries/Ethernet/src/EthernetServer.h index 3ddec326f..9374c3023 100644 --- a/libraries/Ethernet/src/EthernetServer.h +++ b/libraries/Ethernet/src/EthernetServer.h @@ -31,9 +31,11 @@ class EthernetServer : public MbedServer { } public: + EthernetServer() {} EthernetServer(uint16_t port) : MbedServer(port) {} - EthernetClient available(uint8_t* status = nullptr); + EthernetClient accept(uint8_t* status = nullptr); + EthernetClient available(uint8_t* status = nullptr) __attribute__((deprecated("Use accept()."))); }; } diff --git a/libraries/SocketWrapper/src/MbedServer.cpp b/libraries/SocketWrapper/src/MbedServer.cpp index 758c0208c..5f0881d3f 100644 --- a/libraries/SocketWrapper/src/MbedServer.cpp +++ b/libraries/SocketWrapper/src/MbedServer.cpp @@ -5,39 +5,29 @@ uint8_t arduino::MbedServer::status() { return 0; } +void arduino::MbedServer::begin(uint16_t port) { + _port = port; + begin(); +} + void arduino::MbedServer::begin() { if (sock == nullptr) { sock = new TCPSocket(); ((TCPSocket *)sock)->open(getNetwork()); } if (sock) { + int enable = 1; + sock->setsockopt(NSAPI_SOCKET, NSAPI_REUSEADDR, &enable, sizeof(int)); sock->bind(_port); sock->listen(5); sock->set_blocking(false); } } -size_t arduino::MbedServer::write(uint8_t c) { - if (sock) { - sock->send(&c, 1); - return 1; - } - return 0; -} - -size_t arduino::MbedServer::write(const uint8_t *buf, size_t size) { - if (sock) { - sock->send(buf, size); - return size; - } - return 0; -} - - // MUST be reimplemented (just copy/paste and replace MbedClient to *Client) since MbedClient is abstract /* -arduino::MbedClient arduino::MbedServer::available(uint8_t* status) { +arduino::MbedClient arduino::MbedServer::accept(uint8_t* status) { MbedClient client; nsapi_error_t error; if (sock == nullptr) { diff --git a/libraries/SocketWrapper/src/MbedServer.h b/libraries/SocketWrapper/src/MbedServer.h index 23017e296..c6a844e15 100644 --- a/libraries/SocketWrapper/src/MbedServer.h +++ b/libraries/SocketWrapper/src/MbedServer.h @@ -30,7 +30,7 @@ namespace arduino { class MbedClient; -class MbedServer : public arduino::Server { +class MbedServer { protected: virtual NetworkInterface *getNetwork() = 0; @@ -38,23 +38,26 @@ class MbedServer : public arduino::Server { uint16_t _port; public: + MbedServer() + : _port(80){}; MbedServer(uint16_t port) : _port(port){}; virtual ~MbedServer() { + end(); + } + void end() { if (sock) { delete sock; sock = nullptr; } } + void begin(uint16_t port); void begin(); - virtual size_t write(uint8_t); - virtual size_t write(const uint8_t *buf, size_t size); uint8_t status(); - - //virtual MbedClient available(uint8_t* status) = 0; - - using Print::write; + explicit operator bool() { + return sock != nullptr; + } friend class MbedSocketClass; friend class MbedClient; diff --git a/libraries/WiFi/src/WiFiServer.cpp b/libraries/WiFi/src/WiFiServer.cpp index cefbd0214..fb919eff9 100644 --- a/libraries/WiFi/src/WiFiServer.cpp +++ b/libraries/WiFi/src/WiFiServer.cpp @@ -1,6 +1,10 @@ #include "WiFiServer.h" arduino::WiFiClient arduino::WiFiServer::available(uint8_t* status) { + return accept(status); +} + +arduino::WiFiClient arduino::WiFiServer::accept(uint8_t* status) { WiFiClient client; nsapi_error_t error; if (sock == nullptr) { diff --git a/libraries/WiFi/src/WiFiServer.h b/libraries/WiFi/src/WiFiServer.h index ae293b374..3e20d7959 100644 --- a/libraries/WiFi/src/WiFiServer.h +++ b/libraries/WiFi/src/WiFiServer.h @@ -31,9 +31,11 @@ class WiFiServer : public MbedServer { } public: + WiFiServer() {} WiFiServer(uint16_t port) : MbedServer(port) {} - WiFiClient available(uint8_t* status = nullptr); + WiFiClient accept(uint8_t* status = nullptr); + WiFiClient available(uint8_t* status = nullptr) __attribute__((deprecated("Use accept()."))); }; }