@@ -6,9 +6,13 @@ extern "C" {
6
6
7
7
#include " lwipClient.h"
8
8
9
+ static void memPoolDeleter (struct tcp_struct * tcpClient)
10
+ {
11
+ mem_free (tcpClient);
12
+ }
13
+
9
14
/* -------------------------------------------------------------------------- */
10
15
lwipClient::lwipClient ()
11
- : _tcp_client(NULL )
12
16
{
13
17
}
14
18
/* -------------------------------------------------------------------------- */
@@ -17,15 +21,14 @@ lwipClient::lwipClient()
17
21
sketches but sock is ignored. */
18
22
/* -------------------------------------------------------------------------- */
19
23
lwipClient::lwipClient (uint8_t sock)
20
- : _tcp_client(NULL )
21
24
{
22
25
}
23
26
/* -------------------------------------------------------------------------- */
24
27
25
28
/* -------------------------------------------------------------------------- */
26
29
lwipClient::lwipClient (struct tcp_struct * tcpClient)
30
+ : _tcp_client(tcpClient, memPoolDeleter)
27
31
{
28
- _tcp_client = tcpClient;
29
32
}
30
33
/* -------------------------------------------------------------------------- */
31
34
@@ -49,7 +52,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
49
52
/* -------------------------------------------------------------------------- */
50
53
if (_tcp_client == NULL ) {
51
54
/* 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 );
53
56
54
57
if (_tcp_client == NULL ) {
55
58
return 0 ;
@@ -69,7 +72,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
69
72
70
73
uint32_t startTime = millis ();
71
74
ip_addr_t ipaddr;
72
- tcp_arg (_tcp_client->pcb , _tcp_client);
75
+ tcp_arg (_tcp_client->pcb , _tcp_client. get () );
73
76
if (ERR_OK != tcp_connect (_tcp_client->pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port, &tcp_connected_callback)) {
74
77
stop ();
75
78
return 0 ;
@@ -215,7 +218,7 @@ void lwipClient::stop()
215
218
216
219
// close tcp connection if not closed yet
217
220
if (status () != TCP_CLOSING) {
218
- tcp_connection_close (_tcp_client->pcb , _tcp_client);
221
+ tcp_connection_close (_tcp_client->pcb , _tcp_client. get () );
219
222
}
220
223
}
221
224
@@ -243,7 +246,7 @@ uint8_t lwipClient::status()
243
246
lwipClient::operator bool ()
244
247
{
245
248
/* -------------------------------------------------------------------------- */
246
- return (_tcp_client && (_tcp_client-> state != TCP_CLOSING) );
249
+ return (_tcp_client != nullptr );
247
250
}
248
251
249
252
/* -------------------------------------------------------------------------- */
0 commit comments