@@ -8,7 +8,6 @@ extern "C" {
8
8
9
9
/* -------------------------------------------------------------------------- */
10
10
lwipClient::lwipClient ()
11
- : _tcp_client(NULL )
12
11
{
13
12
}
14
13
/* -------------------------------------------------------------------------- */
@@ -17,15 +16,17 @@ lwipClient::lwipClient()
17
16
sketches but sock is ignored. */
18
17
/* -------------------------------------------------------------------------- */
19
18
lwipClient::lwipClient (uint8_t sock)
20
- : _tcp_client(NULL )
21
19
{
22
20
}
23
21
/* -------------------------------------------------------------------------- */
24
22
25
23
/* -------------------------------------------------------------------------- */
26
24
lwipClient::lwipClient (struct tcp_struct * tcpClient)
27
25
{
28
- _tcp_client = tcpClient;
26
+ if (tcpClient == NULL )
27
+ return ;
28
+ _tcp_client.reset (tcpClient,
29
+ [](struct tcp_struct *tcp_client) { (void ) tcp_client; } ); // empty deleter
29
30
}
30
31
/* -------------------------------------------------------------------------- */
31
32
@@ -49,7 +50,8 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
49
50
/* -------------------------------------------------------------------------- */
50
51
if (_tcp_client == NULL ) {
51
52
/* Allocates memory for client */
52
- _tcp_client = (struct tcp_struct *)mem_malloc (sizeof (struct tcp_struct ));
53
+ _tcp_client.reset ((struct tcp_struct *)mem_malloc (sizeof (struct tcp_struct )),
54
+ [](struct tcp_struct *tcp_client) { mem_free (tcp_client); } );
53
55
54
56
if (_tcp_client == NULL ) {
55
57
return 0 ;
@@ -69,7 +71,7 @@ int lwipClient::connect(IPAddress ip, uint16_t port)
69
71
70
72
uint32_t startTime = millis ();
71
73
ip_addr_t ipaddr;
72
- tcp_arg (_tcp_client->pcb , _tcp_client);
74
+ tcp_arg (_tcp_client->pcb , _tcp_client. get () );
73
75
if (ERR_OK != tcp_connect (_tcp_client->pcb , u8_to_ip_addr (rawIPAddress (ip), &ipaddr), port, &tcp_connected_callback)) {
74
76
stop ();
75
77
return 0 ;
@@ -215,7 +217,7 @@ void lwipClient::stop()
215
217
216
218
// close tcp connection if not closed yet
217
219
if (status () != TCP_CLOSING) {
218
- tcp_connection_close (_tcp_client->pcb , _tcp_client);
220
+ tcp_connection_close (_tcp_client->pcb , _tcp_client. get () );
219
221
}
220
222
}
221
223
0 commit comments