Skip to content

Commit af09d06

Browse files
committed
LwipServer - manage connections as LwipClient so a copy always exists
for shared_ptr
1 parent 05feca5 commit af09d06

File tree

9 files changed

+30
-77
lines changed

9 files changed

+30
-77
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class EthernetClient : public lwipClient {
88
public:
99
EthernetClient() {}
1010
EthernetClient(struct tcp_struct *tcpClient) : lwipClient(tcpClient) {}
11+
EthernetClient(lwipClient& client) : lwipClient(client) {}
1112
};
1213

1314
#endif

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

+2-18
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,8 @@ class EthernetServer : public lwipServer {
1212
EthernetServer(uint16_t port) : lwipServer(port) {}
1313

1414
EthernetClient available() {
15-
accept();
16-
17-
for (int n = 0; n < MAX_CLIENT; n++) {
18-
if (_tcp_client[n] != NULL) {
19-
if (_tcp_client[n]->pcb != NULL) {
20-
EthernetClient client(_tcp_client[n]);
21-
uint8_t s = client.status();
22-
if (s == TCP_ACCEPTED) {
23-
if (client.available()) {
24-
return client;
25-
}
26-
}
27-
}
28-
}
29-
}
30-
31-
struct tcp_struct *default_client = NULL;
32-
return EthernetClient(default_client);
15+
lwipClient client = lwipServer::available();
16+
return EthernetClient(client);
3317
}
3418
};
3519

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class WiFiClient : public lwipClient {
88
public:
99
WiFiClient() {}
1010
WiFiClient(struct tcp_struct *tcpClient) : lwipClient(tcpClient) {}
11+
WiFiClient(lwipClient& client) : lwipClient(client) {}
1112
};
1213

1314
#endif

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

+2-18
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,8 @@ class WiFiServer : public lwipServer {
1111
WiFiServer(uint16_t port) : lwipServer(port) {}
1212

1313
WiFiClient available() {
14-
accept();
15-
16-
for (int n = 0; n < MAX_CLIENT; n++) {
17-
if (_tcp_client[n] != NULL) {
18-
if (_tcp_client[n]->pcb != NULL) {
19-
WiFiClient client(_tcp_client[n]);
20-
uint8_t s = client.status();
21-
if (s == TCP_ACCEPTED) {
22-
if (client.available()) {
23-
return client;
24-
}
25-
}
26-
}
27-
}
28-
}
29-
30-
struct tcp_struct *default_client = NULL;
31-
return WiFiClient(default_client);
14+
lwipClient client = lwipServer::available();
15+
return WiFiClient(client);
3216
}
3317
};
3418

Diff for: libraries/lwIpWrapper/src/lwipClient.cpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ extern "C" {
66

77
#include "lwipClient.h"
88

9+
static void memPoolDeleter(struct tcp_struct* tcpClient)
10+
{
11+
mem_free(tcpClient);
12+
}
13+
914
/* -------------------------------------------------------------------------- */
1015
lwipClient::lwipClient()
11-
: _tcp_client(NULL)
1216
{
1317
}
1418
/* -------------------------------------------------------------------------- */
@@ -17,15 +21,14 @@ lwipClient::lwipClient()
1721
sketches but sock is ignored. */
1822
/* -------------------------------------------------------------------------- */
1923
lwipClient::lwipClient(uint8_t sock)
20-
: _tcp_client(NULL)
2124
{
2225
}
2326
/* -------------------------------------------------------------------------- */
2427

2528
/* -------------------------------------------------------------------------- */
2629
lwipClient::lwipClient(struct tcp_struct* tcpClient)
30+
: _tcp_client(tcpClient, memPoolDeleter)
2731
{
28-
_tcp_client = tcpClient;
2932
}
3033
/* -------------------------------------------------------------------------- */
3134

@@ -49,7 +52,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
4952
/* -------------------------------------------------------------------------- */
5053
if (_tcp_client == NULL) {
5154
/* Allocates memory for client */
52-
_tcp_client = (struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct));
55+
_tcp_client.reset((struct tcp_struct*)mem_malloc(sizeof(struct tcp_struct)), memPoolDeleter);
5356

5457
if (_tcp_client == NULL) {
5558
return 0;
@@ -69,7 +72,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
6972

7073
uint32_t startTime = millis();
7174
ip_addr_t ipaddr;
72-
tcp_arg(_tcp_client->pcb, _tcp_client);
75+
tcp_arg(_tcp_client->pcb, _tcp_client.get());
7376
if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) {
7477
stop();
7578
return 0;
@@ -215,7 +218,7 @@ void lwipClient::stop()
215218

216219
// close tcp connection if not closed yet
217220
if (status() != TCP_CLOSING) {
218-
tcp_connection_close(_tcp_client->pcb, _tcp_client);
221+
tcp_connection_close(_tcp_client->pcb, _tcp_client.get());
219222
}
220223
}
221224

@@ -243,7 +246,7 @@ uint8_t lwipClient::status()
243246
lwipClient::operator bool()
244247
{
245248
/* -------------------------------------------------------------------------- */
246-
return (_tcp_client && (_tcp_client->state != TCP_CLOSING));
249+
return (_tcp_client != nullptr);
247250
}
248251

249252
/* -------------------------------------------------------------------------- */

Diff for: libraries/lwIpWrapper/src/lwipClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lwipMem.h"
1010
#include "lwipTcp.h"
1111
#include "lwipTypes.h"
12+
#include <memory>
1213

1314
class lwipClient : public Client {
1415

@@ -66,7 +67,7 @@ class lwipClient : public Client {
6667
using Print::write;
6768

6869
private:
69-
struct tcp_struct* _tcp_client;
70+
std::shared_ptr<struct tcp_struct> _tcp_client;
7071
uint16_t _timeout = 10000;
7172
};
7273

Diff for: libraries/lwIpWrapper/src/lwipServer.cpp

+7-29
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ extern "C" {
99
lwipServer::lwipServer(uint16_t port)
1010
{
1111
_port = port;
12-
for (int i = 0; i < MAX_CLIENT; i++) {
13-
_tcp_client[i] = {};
14-
}
1512
_tcp_server = {};
1613
}
1714

@@ -50,12 +47,8 @@ void lwipServer::accept()
5047
{
5148
/* Free client if disconnected */
5249
for (int n = 0; n < MAX_CLIENT; n++) {
53-
if (_tcp_client[n] != NULL) {
54-
lwipClient client(_tcp_client[n]);
55-
if (client.status() == TCP_CLOSING) {
56-
mem_free(_tcp_client[n]);
57-
_tcp_client[n] = NULL;
58-
}
50+
if (_tcp_client[n] && !_tcp_client[n].connected()) {
51+
_tcp_client[n] = lwipClient();
5952
}
6053
}
6154
}
@@ -65,21 +58,12 @@ lwipClient lwipServer::available()
6558
accept();
6659

6760
for (int n = 0; n < MAX_CLIENT; n++) {
68-
if (_tcp_client[n] != NULL) {
69-
if (_tcp_client[n]->pcb != NULL) {
70-
lwipClient client(_tcp_client[n]);
71-
uint8_t s = client.status();
72-
if (s == TCP_ACCEPTED) {
73-
if (client.available()) {
74-
return client;
75-
}
76-
}
77-
}
61+
if (_tcp_client[n].available()) {
62+
return _tcp_client[n];
7863
}
7964
}
8065

81-
struct tcp_struct* default_client = NULL;
82-
return lwipClient(default_client);
66+
return lwipClient();
8367
}
8468

8569
size_t lwipServer::write(uint8_t b)
@@ -94,14 +78,8 @@ size_t lwipServer::write(const uint8_t* buffer, size_t size)
9478
accept();
9579

9680
for (int n = 0; n < MAX_CLIENT; n++) {
97-
if (_tcp_client[n] != NULL) {
98-
if (_tcp_client[n]->pcb != NULL) {
99-
lwipClient client(_tcp_client[n]);
100-
uint8_t s = client.status();
101-
if (s == TCP_ACCEPTED) {
102-
n += client.write(buffer, size);
103-
}
104-
}
81+
if (_tcp_client[n].status() == TCP_ACCEPTED) {
82+
n += _tcp_client[n].write(buffer, size);
10583
}
10684
}
10785

Diff for: libraries/lwIpWrapper/src/lwipServer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class lwipServer : public Server {
1010
protected:
1111
uint16_t _port;
1212
struct tcp_struct _tcp_server;
13-
struct tcp_struct* _tcp_client[MAX_CLIENT];
13+
lwipClient _tcp_client[MAX_CLIENT];
1414

1515
void accept(void);
1616

Diff for: libraries/lwIpWrapper/src/lwipTcp.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "lwipTcp.h"
2+
#include "lwipClient.h"
23

34
#if LWIP_TCP
45
static err_t tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err);
@@ -53,7 +54,7 @@ err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err)
5354
{
5455
err_t ret_err;
5556
uint8_t accepted;
56-
struct tcp_struct** tcpClient = (struct tcp_struct**)arg;
57+
lwipClient* tcpClient = (lwipClient*)arg;
5758

5859
/* set priority for the newly accepted tcp connection newpcb */
5960
tcp_setprio(newpcb, TCP_PRIO_MIN);
@@ -69,8 +70,8 @@ err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err)
6970

7071
/* Looking for an empty socket */
7172
for (uint16_t i = 0; i < MAX_CLIENT; i++) {
72-
if (tcpClient[i] == NULL) {
73-
tcpClient[i] = client;
73+
if (!tcpClient[i]) {
74+
tcpClient[i] = lwipClient(client);
7475
accepted = 1;
7576
break;
7677
}

0 commit comments

Comments
 (0)