Skip to content

Commit

Permalink
LwipServer - manage connections as LwipClient so a copy always exists
Browse files Browse the repository at this point in the history
for shared_ptr
  • Loading branch information
JAndrassy committed Dec 15, 2023
1 parent 05feca5 commit 4f02c81
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 77 deletions.
1 change: 1 addition & 0 deletions libraries/Ethernet/src/EthernetClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class EthernetClient : public lwipClient {
public:
EthernetClient() {}
EthernetClient(struct tcp_struct *tcpClient) : lwipClient(tcpClient) {}
EthernetClient(lwipClient& client) : lwipClient(client) {}
};

#endif
20 changes: 2 additions & 18 deletions libraries/Ethernet/src/EthernetServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,8 @@ class EthernetServer : public lwipServer {
EthernetServer(uint16_t port) : lwipServer(port) {}

EthernetClient available() {
accept();

for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
if (_tcp_client[n]->pcb != NULL) {
EthernetClient client(_tcp_client[n]);
uint8_t s = client.status();
if (s == TCP_ACCEPTED) {
if (client.available()) {
return client;
}
}
}
}
}

struct tcp_struct *default_client = NULL;
return EthernetClient(default_client);
lwipClient client = lwipServer::available();
return EthernetClient(client);
}
};

Expand Down
1 change: 1 addition & 0 deletions libraries/WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class WiFiClient : public lwipClient {
public:
WiFiClient() {}
WiFiClient(struct tcp_struct *tcpClient) : lwipClient(tcpClient) {}
WiFiClient(lwipClient& client) : lwipClient(client) {}
};

#endif
Expand Down
20 changes: 2 additions & 18 deletions libraries/WiFi/src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,8 @@ class WiFiServer : public lwipServer {
WiFiServer(uint16_t port) : lwipServer(port) {}

WiFiClient available() {
accept();

for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
if (_tcp_client[n]->pcb != NULL) {
WiFiClient client(_tcp_client[n]);
uint8_t s = client.status();
if (s == TCP_ACCEPTED) {
if (client.available()) {
return client;
}
}
}
}
}

struct tcp_struct *default_client = NULL;
return WiFiClient(default_client);
lwipClient client = lwipServer::available();
return WiFiClient(client);
}
};

Expand Down
17 changes: 10 additions & 7 deletions libraries/lwIpWrapper/src/lwipClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ extern "C" {

#include "lwipClient.h"

static void memPoolDeleter(struct tcp_struct* tcpClient)
{
mem_free(tcpClient);
}

/* -------------------------------------------------------------------------- */
lwipClient::lwipClient()
: _tcp_client(NULL)
{
}
/* -------------------------------------------------------------------------- */
Expand All @@ -17,15 +21,14 @@ lwipClient::lwipClient()
sketches but sock is ignored. */
/* -------------------------------------------------------------------------- */
lwipClient::lwipClient(uint8_t sock)
: _tcp_client(NULL)
{
}
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
lwipClient::lwipClient(struct tcp_struct* tcpClient)
: _tcp_client(tcpClient, memPoolDeleter)
{
_tcp_client = tcpClient;
}
/* -------------------------------------------------------------------------- */

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

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

uint32_t startTime = millis();
ip_addr_t ipaddr;
tcp_arg(_tcp_client->pcb, _tcp_client);
tcp_arg(_tcp_client->pcb, _tcp_client.get());
if (ERR_OK != tcp_connect(_tcp_client->pcb, u8_to_ip_addr(rawIPAddress(ip), &ipaddr), port, &tcp_connected_callback)) {
stop();
return 0;
Expand Down Expand Up @@ -215,7 +218,7 @@ void lwipClient::stop()

// close tcp connection if not closed yet
if (status() != TCP_CLOSING) {
tcp_connection_close(_tcp_client->pcb, _tcp_client);
tcp_connection_close(_tcp_client->pcb, _tcp_client.get());
}
}

Expand Down Expand Up @@ -243,7 +246,7 @@ uint8_t lwipClient::status()
lwipClient::operator bool()
{
/* -------------------------------------------------------------------------- */
return (_tcp_client && (_tcp_client->state != TCP_CLOSING));
return (_tcp_client != nullptr);
}

/* -------------------------------------------------------------------------- */
Expand Down
3 changes: 2 additions & 1 deletion libraries/lwIpWrapper/src/lwipClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "lwipMem.h"
#include "lwipTcp.h"
#include "lwipTypes.h"
#include <memory>

class lwipClient : public Client {

Expand Down Expand Up @@ -66,7 +67,7 @@ class lwipClient : public Client {
using Print::write;

private:
struct tcp_struct* _tcp_client;
std::shared_ptr<struct tcp_struct> _tcp_client;
uint16_t _timeout = 10000;
};

Expand Down
37 changes: 8 additions & 29 deletions libraries/lwIpWrapper/src/lwipServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ extern "C" {
lwipServer::lwipServer(uint16_t port)
{
_port = port;
for (int i = 0; i < MAX_CLIENT; i++) {
_tcp_client[i] = {};
}
_tcp_server = {};
}

Expand Down Expand Up @@ -50,12 +47,9 @@ void lwipServer::accept()
{
/* Free client if disconnected */
for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
lwipClient client(_tcp_client[n]);
if (client.status() == TCP_CLOSING) {
mem_free(_tcp_client[n]);
_tcp_client[n] = NULL;
}
if (_tcp_client[n] && !_tcp_client[n].connected()) {
Serial.println("clear");
_tcp_client[n] = lwipClient();
}
}
}
Expand All @@ -65,21 +59,12 @@ lwipClient lwipServer::available()
accept();

for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
if (_tcp_client[n]->pcb != NULL) {
lwipClient client(_tcp_client[n]);
uint8_t s = client.status();
if (s == TCP_ACCEPTED) {
if (client.available()) {
return client;
}
}
}
if (_tcp_client[n].available()) {
return _tcp_client[n];
}
}

struct tcp_struct* default_client = NULL;
return lwipClient(default_client);
return lwipClient();
}

size_t lwipServer::write(uint8_t b)
Expand All @@ -94,14 +79,8 @@ size_t lwipServer::write(const uint8_t* buffer, size_t size)
accept();

for (int n = 0; n < MAX_CLIENT; n++) {
if (_tcp_client[n] != NULL) {
if (_tcp_client[n]->pcb != NULL) {
lwipClient client(_tcp_client[n]);
uint8_t s = client.status();
if (s == TCP_ACCEPTED) {
n += client.write(buffer, size);
}
}
if (_tcp_client[n].status() == TCP_ACCEPTED) {
n += _tcp_client[n].write(buffer, size);
}
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/lwIpWrapper/src/lwipServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class lwipServer : public Server {
protected:
uint16_t _port;
struct tcp_struct _tcp_server;
struct tcp_struct* _tcp_client[MAX_CLIENT];
lwipClient _tcp_client[MAX_CLIENT];

void accept(void);

Expand Down
7 changes: 4 additions & 3 deletions libraries/lwIpWrapper/src/lwipTcp.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lwipTcp.h"
#include "lwipClient.h"

#if LWIP_TCP
static err_t tcp_recv_callback(void* arg, struct tcp_pcb* tpcb, struct pbuf* p, err_t err);
Expand Down Expand Up @@ -53,7 +54,7 @@ err_t tcp_accept_callback(void* arg, struct tcp_pcb* newpcb, err_t err)
{
err_t ret_err;
uint8_t accepted;
struct tcp_struct** tcpClient = (struct tcp_struct**)arg;
lwipClient* tcpClient = (lwipClient*)arg;

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

/* Looking for an empty socket */
for (uint16_t i = 0; i < MAX_CLIENT; i++) {
if (tcpClient[i] == NULL) {
tcpClient[i] = client;
if (!tcpClient[i]) {
tcpClient[i] = lwipClient(client);
accepted = 1;
break;
}
Expand Down

0 comments on commit 4f02c81

Please sign in to comment.