fixed closing and opening of udp port
This commit is contained in:
@ -158,20 +158,21 @@ bool AsyncUdpListener::listen(const ip_addr_t *addr, uint16_t port)
|
||||
}
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
if (!_init())
|
||||
{
|
||||
ESP_LOGE(TAG, "failed to init");
|
||||
return false;
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
if (addr)
|
||||
{
|
||||
IP_SET_TYPE_VAL(_pcb->local_ip, addr->type);
|
||||
IP_SET_TYPE_VAL(_pcb->remote_ip, addr->type);
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "calling udp_bind()...");
|
||||
if (_udp_bind(_pcb, addr, port) != ERR_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "failed to bind");
|
||||
@ -262,30 +263,38 @@ void AsyncUdpListener::_udp_task_post(udp_pcb *_pcb, pbuf *pb, const ip_addr_t *
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncUdpListener::_init()
|
||||
void AsyncUdpListener::close()
|
||||
{
|
||||
if (_pcb)
|
||||
return true;
|
||||
|
||||
_pcb = udp_new();
|
||||
if (!_pcb)
|
||||
if (_connected)
|
||||
{
|
||||
ESP_LOGE(TAG, "udp_new() failed");
|
||||
return false;
|
||||
ESP_LOGI(TAG, "calling udp_diconnect()...");
|
||||
_udp_disconnect(_pcb);
|
||||
_connected = false;
|
||||
}
|
||||
|
||||
udp_recv(_pcb, &_udp_recv, (void *)this);
|
||||
if (_pcb)
|
||||
{
|
||||
ESP_LOGI(TAG, "calling udp_remove()...");
|
||||
udp_remove(_pcb);
|
||||
_pcb = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncUdpListener::_init()
|
||||
{
|
||||
if (!_pcb)
|
||||
{
|
||||
ESP_LOGI(TAG, "calling udp_new()...");
|
||||
_pcb = udp_new();
|
||||
if (!_pcb)
|
||||
{
|
||||
ESP_LOGE(TAG, "udp_new() failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "calling udp_recv()...");
|
||||
udp_recv(_pcb, &_udp_recv, (void *)this);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void AsyncUdpListener::close()
|
||||
{
|
||||
if (_pcb)
|
||||
{
|
||||
if (_connected)
|
||||
_udp_disconnect(_pcb);
|
||||
|
||||
_connected = false;
|
||||
}
|
||||
}
|
||||
|
@ -65,6 +65,7 @@ class AsyncUdpListener
|
||||
|
||||
public:
|
||||
AsyncUdpListener() = default;
|
||||
~AsyncUdpListener() { close(); }
|
||||
|
||||
bool listen(const ip_addr_t *addr, uint16_t port);
|
||||
|
||||
@ -93,9 +94,10 @@ public:
|
||||
|
||||
void _udp_task_post(udp_pcb *pcb, pbuf *pb, const ip_addr_t *addr, uint16_t port, struct netif *netif);
|
||||
|
||||
void close();
|
||||
|
||||
private:
|
||||
bool _init();
|
||||
void close();
|
||||
|
||||
private:
|
||||
cpputils::DelayedConstruction<espcpputils::queue> _udp_queue;
|
||||
|
Reference in New Issue
Block a user