Skip to content

Commit 4f9597d

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
add close and abort methods and enable disconnect callback
1 parent abe0003 commit 4f9597d

File tree

1 file changed

+45
-52
lines changed
  • hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/include

1 file changed

+45
-52
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/include/ClientContext.h

+45-52
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,39 @@ class ClientContext {
3939
tcp_sent(pcb, &_s_sent);
4040
tcp_err(pcb, &_s_error);
4141
}
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+
4375
~ClientContext() {
4476
}
4577

@@ -58,22 +90,10 @@ class ClientContext {
5890
}
5991

6092
void unref() {
61-
err_t err;
6293
DEBUGV(":ur %d\r\n", _refcnt);
6394
if(--_refcnt == 0) {
6495
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();
7797
delete this;
7898
}
7999
}
@@ -179,6 +199,13 @@ class ClientContext {
179199

180200
private:
181201

202+
err_t _sent(tcp_pcb* pcb, uint16_t len) {
203+
DEBUGV(":sent %d\r\n", len);
204+
_size_sent -= len;
205+
if(_size_sent == 0 && _send_waiting) esp_schedule();
206+
return ERR_OK;
207+
}
208+
182209
void _consume(size_t size) {
183210
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
184211
if(left > 0) {
@@ -204,21 +231,9 @@ class ClientContext {
204231

205232
if(pb == 0) // connection closed
206233
{
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;
234+
if(_discard_cb) _discard_cb(_discard_cb_arg, this);
235+
DEBUGV(":rcla\r\n");
236+
return abort();
222237
}
223238

224239
if(_rx_buf) {
@@ -231,27 +246,12 @@ class ClientContext {
231246
_rx_buf = pb;
232247
_rx_buf_offset = 0;
233248
}
234-
// tcp_recved(pcb, received);
235-
// pbuf_free(pb);
236249
return ERR_OK;
237250
}
238251

239252
void _error(err_t err) {
240253
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();
255255
if(_size_sent && _send_waiting) {
256256
esp_schedule();
257257
}
@@ -261,13 +261,6 @@ class ClientContext {
261261
return ERR_OK;
262262
}
263263

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-
271264
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
272265
return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err);
273266
}

0 commit comments

Comments
 (0)