Skip to content

Commit

Permalink
SocketWrapper - MbedServer modernization (without available() and Print)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JAndrassy committed Dec 6, 2023
1 parent 7b95100 commit e15a0be
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libraries/Ethernet/examples/WebServer/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions libraries/Ethernet/src/EthernetServer.cpp
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 3 additions & 1 deletion libraries/Ethernet/src/EthernetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().")));
};

}
Expand Down
26 changes: 8 additions & 18 deletions libraries/SocketWrapper/src/MbedServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions libraries/SocketWrapper/src/MbedServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,34 @@ namespace arduino {

class MbedClient;

class MbedServer : public arduino::Server {
class MbedServer {

protected:
virtual NetworkInterface *getNetwork() = 0;
TCPSocket *sock = nullptr;
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;
Expand Down
4 changes: 4 additions & 0 deletions libraries/WiFi/src/WiFiServer.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion libraries/WiFi/src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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().")));
};

}
Expand Down

0 comments on commit e15a0be

Please sign in to comment.