@@ -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/* -------------------------------------------------------------------------- */
1015lwipClient::lwipClient ()
11- : _tcp_client(NULL )
1216{
1317}
1418/* -------------------------------------------------------------------------- */
@@ -17,15 +21,14 @@ lwipClient::lwipClient()
1721sketches but sock is ignored. */
1822/* -------------------------------------------------------------------------- */
1923lwipClient::lwipClient (uint8_t sock)
20- : _tcp_client(NULL )
2124{
2225}
2326/* -------------------------------------------------------------------------- */
2427
2528/* -------------------------------------------------------------------------- */
2629lwipClient::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()
243246lwipClient::operator bool ()
244247{
245248 /* -------------------------------------------------------------------------- */
246- return (_tcp_client && (_tcp_client-> state != TCP_CLOSING) );
249+ return (_tcp_client != nullptr );
247250}
248251
249252/* -------------------------------------------------------------------------- */
0 commit comments