Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LwipServer - manage connections as LwipClient so a copy always exists for shared_ptr #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 7 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,8 @@ 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()) {
_tcp_client[n] = lwipClient();
}
}
}
Expand All @@ -65,21 +58,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 +78,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
Loading