Skip to content

Commit 4726f25

Browse files
authored
Merge pull request #793 from JAndrassy/mbedserver_modernization
SocketWrapper - MbedServer modernization (without available() and Print)
2 parents 22e6d91 + 18b696b commit 4726f25

File tree

11 files changed

+35
-218
lines changed

11 files changed

+35
-218
lines changed

Diff for: libraries/Ethernet/examples/AdvancedChatServer/AdvancedChatServer.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void setup() {
6767

6868
void loop() {
6969
// check for any new client connecting, and say hello (before any incoming data)
70-
EthernetClient newClient = server.available();
70+
EthernetClient newClient = server.accept();
7171
if (newClient) {
7272
for (byte i=0; i < 8; i++) {
7373
if (!clients[i]) {

Diff for: libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void getData() {
141141

142142
void listenForEthernetClients() {
143143
// listen for incoming clients
144-
EthernetClient client = server.available();
144+
EthernetClient client = server.accept();
145145
if (client) {
146146
Serial.println("Got a client");
147147
// an http request ends with a blank line

Diff for: libraries/Ethernet/examples/ChatServer/ChatServer.ino

-97
This file was deleted.

Diff for: libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino

-91
This file was deleted.

Diff for: libraries/Ethernet/examples/WebServer/WebServer.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void setup() {
6161

6262
void loop() {
6363
// listen for incoming clients
64-
EthernetClient client = server.available();
64+
EthernetClient client = server.accept();
6565
if (client) {
6666
Serial.println("new client");
6767
// an http request ends with a blank line

Diff for: libraries/Ethernet/src/EthernetServer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "EthernetServer.h"
22

33
arduino::EthernetClient arduino::EthernetServer::available(uint8_t* status) {
4+
return accept(status);
5+
}
6+
7+
arduino::EthernetClient arduino::EthernetServer::accept(uint8_t* status) {
48
EthernetClient client;
59
nsapi_error_t error;
610

Diff for: libraries/Ethernet/src/EthernetServer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ class EthernetServer : public MbedServer {
3131
}
3232

3333
public:
34+
EthernetServer() {}
3435
EthernetServer(uint16_t port)
3536
: MbedServer(port) {}
36-
EthernetClient available(uint8_t* status = nullptr);
37+
EthernetClient accept(uint8_t* status = nullptr);
38+
EthernetClient available(uint8_t* status = nullptr) __attribute__((deprecated("Use accept().")));
3739
};
3840

3941
}

Diff for: libraries/SocketWrapper/src/MbedServer.cpp

+8-18
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,29 @@ uint8_t arduino::MbedServer::status() {
55
return 0;
66
}
77

8+
void arduino::MbedServer::begin(uint16_t port) {
9+
_port = port;
10+
begin();
11+
}
12+
813
void arduino::MbedServer::begin() {
914
if (sock == nullptr) {
1015
sock = new TCPSocket();
1116
((TCPSocket *)sock)->open(getNetwork());
1217
}
1318
if (sock) {
19+
int enable = 1;
20+
sock->setsockopt(NSAPI_SOCKET, NSAPI_REUSEADDR, &enable, sizeof(int));
1421
sock->bind(_port);
1522
sock->listen(5);
1623
sock->set_blocking(false);
1724
}
1825
}
1926

20-
size_t arduino::MbedServer::write(uint8_t c) {
21-
if (sock) {
22-
sock->send(&c, 1);
23-
return 1;
24-
}
25-
return 0;
26-
}
27-
28-
size_t arduino::MbedServer::write(const uint8_t *buf, size_t size) {
29-
if (sock) {
30-
sock->send(buf, size);
31-
return size;
32-
}
33-
return 0;
34-
}
35-
36-
3727
// MUST be reimplemented (just copy/paste and replace MbedClient to *Client) since MbedClient is abstract
3828

3929
/*
40-
arduino::MbedClient arduino::MbedServer::available(uint8_t* status) {
30+
arduino::MbedClient arduino::MbedServer::accept(uint8_t* status) {
4131
MbedClient client;
4232
nsapi_error_t error;
4333
if (sock == nullptr) {

Diff for: libraries/SocketWrapper/src/MbedServer.h

+10-7
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,34 @@ namespace arduino {
3030

3131
class MbedClient;
3232

33-
class MbedServer : public arduino::Server {
33+
class MbedServer {
3434

3535
protected:
3636
virtual NetworkInterface *getNetwork() = 0;
3737
TCPSocket *sock = nullptr;
3838
uint16_t _port;
3939

4040
public:
41+
MbedServer()
42+
: _port(80){};
4143
MbedServer(uint16_t port)
4244
: _port(port){};
4345

4446
virtual ~MbedServer() {
47+
end();
48+
}
49+
void end() {
4550
if (sock) {
4651
delete sock;
4752
sock = nullptr;
4853
}
4954
}
55+
void begin(uint16_t port);
5056
void begin();
51-
virtual size_t write(uint8_t);
52-
virtual size_t write(const uint8_t *buf, size_t size);
5357
uint8_t status();
54-
55-
//virtual MbedClient available(uint8_t* status) = 0;
56-
57-
using Print::write;
58+
explicit operator bool() {
59+
return sock != nullptr;
60+
}
5861

5962
friend class MbedSocketClass;
6063
friend class MbedClient;

Diff for: libraries/WiFi/src/WiFiServer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "WiFiServer.h"
22

33
arduino::WiFiClient arduino::WiFiServer::available(uint8_t* status) {
4+
return accept(status);
5+
}
6+
7+
arduino::WiFiClient arduino::WiFiServer::accept(uint8_t* status) {
48
WiFiClient client;
59
nsapi_error_t error;
610
if (sock == nullptr) {

Diff for: libraries/WiFi/src/WiFiServer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ class WiFiServer : public MbedServer {
3131
}
3232

3333
public:
34+
WiFiServer() {}
3435
WiFiServer(uint16_t port)
3536
: MbedServer(port) {}
36-
WiFiClient available(uint8_t* status = nullptr);
37+
WiFiClient accept(uint8_t* status = nullptr);
38+
WiFiClient available(uint8_t* status = nullptr) __attribute__((deprecated("Use accept().")));
3739
};
3840

3941
}

0 commit comments

Comments
 (0)