Skip to content

Commit

Permalink
Replace single-use static callback methods with lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Feb 6, 2019
1 parent 0716dbe commit b7c40bd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 78 deletions.
6 changes: 0 additions & 6 deletions Sming/SmingCore/Network/NetUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@

struct pbuf;
class String;
class TcpConnection;

struct DnsLookup {
TcpConnection* con;
int port;
};

class NetUtils
{
Expand Down
120 changes: 55 additions & 65 deletions Sming/SmingCore/Network/TcpConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,21 @@ bool TcpConnection::connect(const String& server, int port, bool useSsl, uint32_

debug_d("connect to: %s", server.c_str());
canSend = false; // Wait for connection

struct DnsLookup {
TcpConnection* con;
int port;
};
DnsLookup* look = new DnsLookup{this, port};
err_t dnslook = dns_gethostbyname(server.c_str(), &addr, staticDnsResponse, look);
err_t dnslook = dns_gethostbyname(server.c_str(), &addr,
[](const char* name, LWIP_IP_ADDR_T* ipaddr, void* arg) {
auto dlook = static_cast<DnsLookup*>(arg);
if(dlook != nullptr) {
dlook->con->internalOnDnsResponse(name, ipaddr, dlook->port);
delete dlook;
}
},
look);
if(dnslook == ERR_INPROGRESS) {
// Operation pending - see internalOnDnsResponse()
return true;
Expand Down Expand Up @@ -305,9 +318,36 @@ void TcpConnection::initialize(tcp_pcb* pcb)

tcp_nagle_disable(tcp);
tcp_arg(tcp, (void*)this);
tcp_sent(tcp, staticOnSent);
tcp_recv(tcp, staticOnReceive);
tcp_err(tcp, staticOnError);

tcp_sent(tcp, [](void* arg, tcp_pcb* tcp, uint16_t len) -> err_t {
auto con = static_cast<TcpConnection*>(arg);
return (con == nullptr) ? ERR_OK : con->internalOnSent(len);
});

tcp_recv(tcp, [](void* arg, tcp_pcb* tcp, pbuf* p, err_t err) -> err_t {
auto con = static_cast<TcpConnection*>(arg);
//Serial.println("echo_recv!");

if(con == nullptr) {
if(p != nullptr) {
/* Inform TCP that we have taken the data. */
tcp_recved(tcp, p->tot_len);
pbuf_free(p);
}
closeTcpConnection(tcp);
return ERR_OK;
} else {
return con->internalOnReceive(p, err);
}
});

tcp_err(tcp, [](void* arg, err_t err) {
auto con = static_cast<TcpConnection*>(arg);
if(con != nullptr) {
con->internalOnError(err);
}
});

tcp_poll(tcp, staticOnPoll, 4);

#ifdef NETWORK_DEBUG
Expand Down Expand Up @@ -349,24 +389,21 @@ void TcpConnection::flush()
bool TcpConnection::internalTcpConnect(IPAddress addr, uint16_t port)
{
NetUtils::FixNetworkRouting();
err_t res = tcp_connect(tcp, addr, port, staticOnConnected);
err_t res = tcp_connect(tcp, addr, port, [](void* arg, tcp_pcb* tcp, err_t err) -> err_t {
auto con = static_cast<TcpConnection*>(arg);
if(con == nullptr) {
debug_d("TCP connect ABORT");
//closeTcpConnection(tcp);
tcp_abort(tcp);
return ERR_ABRT;
} else {
return con->internalOnConnected(err);
}
});
debug_d("TCP connect result: %d", res);
return res == ERR_OK;
}

err_t TcpConnection::staticOnConnected(void* arg, tcp_pcb* tcp, err_t err)
{
auto con = static_cast<TcpConnection*>(arg);
if(con == nullptr) {
debug_d("OnConnected ABORT");
//closeTcpConnection(tcp);
tcp_abort(tcp);
return ERR_ABRT;
} else {
return con->internalOnConnected(err);
}
}

err_t TcpConnection::internalOnConnected(err_t err)
{
debug_d("TCP connected");
Expand Down Expand Up @@ -449,24 +486,6 @@ err_t TcpConnection::internalOnConnected(err_t err)
return res;
}

err_t TcpConnection::staticOnReceive(void* arg, tcp_pcb* tcp, pbuf* p, err_t err)
{
auto con = static_cast<TcpConnection*>(arg);
//Serial.println("echo_recv!");

if(con == nullptr) {
if(p != nullptr) {
/* Inform TCP that we have taken the data. */
tcp_recved(tcp, p->tot_len);
pbuf_free(p);
}
closeTcpConnection(tcp);
return ERR_OK;
} else {
return con->internalOnReceive(p, err);
}
}

err_t TcpConnection::internalOnReceive(pbuf* p, err_t err)
{
sleep = 0;
Expand Down Expand Up @@ -573,17 +592,6 @@ err_t TcpConnection::internalOnReceive(pbuf* p, err_t err)
return res;
}

err_t TcpConnection::staticOnSent(void* arg, tcp_pcb* tcp, uint16_t len)
{
auto con = static_cast<TcpConnection*>(arg);

if(con == nullptr) {
return ERR_OK;
} else {
return con->internalOnSent(len);
}
}

err_t TcpConnection::internalOnSent(uint16_t len)
{
sleep = 0;
Expand Down Expand Up @@ -617,15 +625,6 @@ err_t TcpConnection::internalOnPoll()
return res;
}

void TcpConnection::staticOnError(void* arg, err_t err)
{
auto con = static_cast<TcpConnection*>(arg);

if(con != nullptr) {
con->internalOnError(err);
}
}

void TcpConnection::internalOnError(err_t err)
{
tcp = nullptr; // IMPORTANT. No available connection after error!
Expand All @@ -634,15 +633,6 @@ void TcpConnection::internalOnError(err_t err)
debug_tcp("<TCP error");
}

void TcpConnection::staticDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, void* arg)
{
auto dlook = static_cast<DnsLookup*>(arg);
if(dlook != nullptr) {
dlook->con->internalOnDnsResponse(name, ipaddr, dlook->port);
delete dlook;
}
}

void TcpConnection::internalOnDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, int port)
{
if(ipaddr != nullptr) {
Expand Down
7 changes: 0 additions & 7 deletions Sming/SmingCore/Network/TcpConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,7 @@ class TcpConnection
void internalOnDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, int port);

private:
// Methods called directly from TCP stack
static err_t staticOnConnected(void* arg, tcp_pcb* tcp, err_t err);
static err_t staticOnReceive(void* arg, tcp_pcb* tcp, pbuf* p, err_t err);
static err_t staticOnSent(void* arg, tcp_pcb* tcp, uint16_t len);
static err_t staticOnPoll(void* arg, tcp_pcb* tcp);
static void staticOnError(void* arg, err_t err);
static void staticDnsResponse(const char* name, LWIP_IP_ADDR_T* ipaddr, void* arg);

static void closeTcpConnection(tcp_pcb* tpcb);

inline void checkSelfFree()
Expand Down

0 comments on commit b7c40bd

Please sign in to comment.