1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-07 16:23:38 +03:00

add close and abort methods and enable disconnect callback

This commit is contained in:
ficeto 2015-05-12 13:58:05 +03:00
parent 4425e0921f
commit 88c6ee418d

View File

@ -40,6 +40,38 @@ class ClientContext {
tcp_err(pcb, &_s_error); tcp_err(pcb, &_s_error);
} }
err_t abort(){
if(_pcb) {
DEBUGV(":abort\r\n");
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
tcp_abort(_pcb);
_pcb = 0;
}
return ERR_ABRT;
}
err_t close(){
err_t err = ERR_OK;
if(_pcb) {
DEBUGV(":close\r\n");
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
err = tcp_close(_pcb);
if(err != ERR_OK) {
DEBUGV(":tc err %d\r\n", err);
tcp_abort(_pcb);
err = ERR_ABRT;
}
_pcb = 0;
}
return err;
}
~ClientContext() { ~ClientContext() {
} }
@ -58,22 +90,10 @@ class ClientContext {
} }
void unref() { void unref() {
err_t err;
DEBUGV(":ur %d\r\n", _refcnt); DEBUGV(":ur %d\r\n", _refcnt);
if(--_refcnt == 0) { if(--_refcnt == 0) {
flush(); flush();
if(_pcb) { close();
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
err = tcp_close(_pcb);
if(err != ERR_OK) {
DEBUGV(":tc err %d\r\n", err);
tcp_abort(_pcb);
}
_pcb = 0;
}
delete this; delete this;
} }
} }
@ -179,6 +199,13 @@ class ClientContext {
private: private:
err_t _sent(tcp_pcb* pcb, uint16_t len) {
DEBUGV(":sent %d\r\n", len);
_size_sent -= len;
if(_size_sent == 0 && _send_waiting) esp_schedule();
return ERR_OK;
}
void _consume(size_t size) { void _consume(size_t size) {
ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size; ptrdiff_t left = _rx_buf->len - _rx_buf_offset - size;
if(left > 0) { if(left > 0) {
@ -204,21 +231,9 @@ class ClientContext {
if(pb == 0) // connection closed if(pb == 0) // connection closed
{ {
DEBUGV(":rcl\r\n"); if(_discard_cb) _discard_cb(_discard_cb_arg, this);
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_err(pcb, NULL);
// int error = tcp_close(pcb);
// if (error != ERR_OK)
{
DEBUGV(":rcla\r\n"); DEBUGV(":rcla\r\n");
tcp_abort(pcb); return abort();
_pcb = 0;
return ERR_ABRT;
}
_pcb = 0;
return ERR_OK;
} }
if(_rx_buf) { if(_rx_buf) {
@ -231,27 +246,12 @@ class ClientContext {
_rx_buf = pb; _rx_buf = pb;
_rx_buf_offset = 0; _rx_buf_offset = 0;
} }
// tcp_recved(pcb, received);
// pbuf_free(pb);
return ERR_OK; return ERR_OK;
} }
void _error(err_t err) { void _error(err_t err) {
DEBUGV(":er %d\r\n", err); DEBUGV(":er %d\r\n", err);
close();
if(_pcb) {
tcp_arg(_pcb, NULL);
tcp_sent(_pcb, NULL);
tcp_recv(_pcb, NULL);
tcp_err(_pcb, NULL);
err = tcp_close(_pcb);
if(err != ERR_OK) {
DEBUGV(":tc err %d\r\n", err);
tcp_abort(_pcb);
}
}
_pcb = 0;
if(_size_sent && _send_waiting) { if(_size_sent && _send_waiting) {
esp_schedule(); esp_schedule();
} }
@ -261,13 +261,6 @@ class ClientContext {
return ERR_OK; return ERR_OK;
} }
err_t _sent(tcp_pcb* pcb, uint16_t len) {
DEBUGV(":sent %d\r\n", len);
_size_sent -= len;
if(_size_sent == 0 && _send_waiting) esp_schedule();
return ERR_OK;
}
static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) { static err_t _s_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *pb, err_t err) {
return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err); return reinterpret_cast<ClientContext*>(arg)->_recv(tpcb, pb, err);
} }