@@ -39,7 +39,39 @@ class ClientContext {
39
39
tcp_sent (pcb, &_s_sent);
40
40
tcp_err (pcb, &_s_error);
41
41
}
42
-
42
+
43
+ err_t abort (){
44
+ if (_pcb) {
45
+ DEBUGV (" :abort\r\n " );
46
+ tcp_arg (_pcb, NULL );
47
+ tcp_sent (_pcb, NULL );
48
+ tcp_recv (_pcb, NULL );
49
+ tcp_err (_pcb, NULL );
50
+ tcp_abort (_pcb);
51
+ _pcb = 0 ;
52
+ }
53
+ return ERR_ABRT;
54
+ }
55
+
56
+ err_t close (){
57
+ err_t err = ERR_OK;
58
+ if (_pcb) {
59
+ DEBUGV (" :close\r\n " );
60
+ tcp_arg (_pcb, NULL );
61
+ tcp_sent (_pcb, NULL );
62
+ tcp_recv (_pcb, NULL );
63
+ tcp_err (_pcb, NULL );
64
+ err = tcp_close (_pcb);
65
+ if (err != ERR_OK) {
66
+ DEBUGV (" :tc err %d\r\n " , err);
67
+ tcp_abort (_pcb);
68
+ err = ERR_ABRT;
69
+ }
70
+ _pcb = 0 ;
71
+ }
72
+ return err;
73
+ }
74
+
43
75
~ClientContext () {
44
76
}
45
77
@@ -58,22 +90,11 @@ class ClientContext {
58
90
}
59
91
60
92
void unref () {
61
- err_t err;
62
93
DEBUGV (" :ur %d\r\n " , _refcnt);
63
94
if (--_refcnt == 0 ) {
64
95
flush ();
65
- if (_pcb) {
66
- tcp_arg (_pcb, NULL );
67
- tcp_sent (_pcb, NULL );
68
- tcp_recv (_pcb, NULL );
69
- tcp_err (_pcb, NULL );
70
- err = tcp_close (_pcb);
71
- if (err != ERR_OK) {
72
- DEBUGV (" :tc err %d\r\n " , err);
73
- tcp_abort (_pcb);
74
- }
75
- _pcb = 0 ;
76
- }
96
+ close ();
97
+ if (_discard_cb) _discard_cb (_discard_cb_arg, this );
77
98
delete this ;
78
99
}
79
100
}
@@ -179,6 +200,13 @@ class ClientContext {
179
200
180
201
private:
181
202
203
+ err_t _sent (tcp_pcb* pcb, uint16_t len) {
204
+ DEBUGV (" :sent %d\r\n " , len);
205
+ _size_sent -= len;
206
+ if (_size_sent == 0 && _send_waiting) esp_schedule ();
207
+ return ERR_OK;
208
+ }
209
+
182
210
void _consume (size_t size) {
183
211
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
184
212
if (left > 0 ) {
@@ -204,21 +232,8 @@ class ClientContext {
204
232
205
233
if (pb == 0 ) // connection closed
206
234
{
207
- DEBUGV (" :rcl\r\n " );
208
- tcp_arg (pcb, NULL );
209
- tcp_sent (pcb, NULL );
210
- tcp_recv (pcb, NULL );
211
- tcp_err (pcb, NULL );
212
- // int error = tcp_close(pcb);
213
- // if (error != ERR_OK)
214
- {
215
- DEBUGV (" :rcla\r\n " );
216
- tcp_abort (pcb);
217
- _pcb = 0 ;
218
- return ERR_ABRT;
219
- }
220
- _pcb = 0 ;
221
- return ERR_OK;
235
+ DEBUGV (" :rcla\r\n " );
236
+ return abort ();
222
237
}
223
238
224
239
if (_rx_buf) {
@@ -231,27 +246,12 @@ class ClientContext {
231
246
_rx_buf = pb;
232
247
_rx_buf_offset = 0 ;
233
248
}
234
- // tcp_recved(pcb, received);
235
- // pbuf_free(pb);
236
249
return ERR_OK;
237
250
}
238
251
239
252
void _error (err_t err) {
240
253
DEBUGV (" :er %d\r\n " , err);
241
-
242
- if (_pcb) {
243
- tcp_arg (_pcb, NULL );
244
- tcp_sent (_pcb, NULL );
245
- tcp_recv (_pcb, NULL );
246
- tcp_err (_pcb, NULL );
247
- err = tcp_close (_pcb);
248
- if (err != ERR_OK) {
249
- DEBUGV (" :tc err %d\r\n " , err);
250
- tcp_abort (_pcb);
251
- }
252
- }
253
- _pcb = 0 ;
254
-
254
+ close ();
255
255
if (_size_sent && _send_waiting) {
256
256
esp_schedule ();
257
257
}
@@ -261,13 +261,6 @@ class ClientContext {
261
261
return ERR_OK;
262
262
}
263
263
264
- err_t _sent (tcp_pcb* pcb, uint16_t len) {
265
- DEBUGV (" :sent %d\r\n " , len);
266
- _size_sent -= len;
267
- if (_size_sent == 0 && _send_waiting) esp_schedule ();
268
- return ERR_OK;
269
- }
270
-
271
264
static err_t _s_recv (void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
272
265
return reinterpret_cast <ClientContext*>(arg)->_recv (tpcb, pb, err);
273
266
}
0 commit comments