Clear all callbacks on close or error

This commit is contained in:
Alex Villacís Lasso
2020-12-23 16:41:29 -05:00
parent c35a1f2079
commit ddd0bc9290
2 changed files with 23 additions and 0 deletions

View File

@@ -690,6 +690,24 @@ void AsyncClient::_sockPoll(void)
} }
} }
void AsyncClient::_removeAllCallbacks(void)
{
_connect_cb = NULL;
_connect_cb_arg = NULL;
_discard_cb = NULL;
_discard_cb_arg = NULL;
_sent_cb = NULL;
_sent_cb_arg = NULL;
_error_cb = NULL;
_error_cb_arg = NULL;
_recv_cb = NULL;
_recv_cb_arg = NULL;
_timeout_cb = NULL;
_timeout_cb_arg = NULL;
_poll_cb = NULL;
_poll_cb_arg = NULL;
}
void AsyncClient::_close(void) void AsyncClient::_close(void)
{ {
//Serial.print("AsyncClient::_close: "); Serial.println(_socket); //Serial.print("AsyncClient::_close: "); Serial.println(_socket);
@@ -700,6 +718,7 @@ void AsyncClient::_close(void)
xSemaphoreGive(_asyncsock_mutex); xSemaphoreGive(_asyncsock_mutex);
if (_discard_cb) _discard_cb(_discard_cb_arg, this); if (_discard_cb) _discard_cb(_discard_cb_arg, this);
_removeAllCallbacks();
_clearWriteQueue(); _clearWriteQueue();
} }
@@ -713,6 +732,7 @@ void AsyncClient::_error(int8_t err)
if (_error_cb) _error_cb(_error_cb_arg, this, err); if (_error_cb) _error_cb(_error_cb_arg, this, err);
if (_discard_cb) _discard_cb(_discard_cb_arg, this); if (_discard_cb) _discard_cb(_discard_cb_arg, this);
_removeAllCallbacks();
_clearWriteQueue(); _clearWriteQueue();
} }
@@ -766,12 +786,14 @@ bool AsyncClient::send()
// of errors before all data was written. // of errors before all data was written.
void AsyncClient::_clearWriteQueue(void) void AsyncClient::_clearWriteQueue(void)
{ {
xSemaphoreTake(_write_mutex, (TickType_t)portMAX_DELAY);
while (_writeQueue.size() > 0) { while (_writeQueue.size() > 0) {
if (_writeQueue.front().owned) { if (_writeQueue.front().owned) {
::free(_writeQueue.front().data); ::free(_writeQueue.front().data);
} }
_writeQueue.pop_front(); _writeQueue.pop_front();
} }
xSemaphoreGive(_write_mutex);
} }
bool AsyncClient::free(){ bool AsyncClient::free(){

View File

@@ -193,6 +193,7 @@ class AsyncClient : public AsyncSocketBase
void _sockIsReadable(void); void _sockIsReadable(void);
void _sockPoll(void); void _sockPoll(void);
void _sockDelayedConnect(void); void _sockDelayedConnect(void);
void _removeAllCallbacks(void);
void _clearWriteQueue(void); void _clearWriteQueue(void);
friend void _tcpsock_dns_found(const char * name, struct ip_addr * ipaddr, void * arg); friend void _tcpsock_dns_found(const char * name, struct ip_addr * ipaddr, void * arg);