mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
Fix UdpClient semantics, add NTPClient example
UdpClient used to create a new socket for each begin/beginPacket call. This made bidirectional communication impossible. Fix #64, fix #53.
This commit is contained in:
@ -66,10 +66,10 @@ WiFiUDP::~WiFiUDP()
|
||||
/* Start WiFiUDP socket, listening at local port */
|
||||
uint8_t WiFiUDP::begin(uint16_t port)
|
||||
{
|
||||
if (_ctx)
|
||||
{
|
||||
if (_ctx) {
|
||||
_ctx->unref();
|
||||
}
|
||||
|
||||
_ctx = new UdpContext;
|
||||
ip_addr_t addr;
|
||||
addr.addr = INADDR_ANY;
|
||||
@ -78,13 +78,11 @@ uint8_t WiFiUDP::begin(uint16_t port)
|
||||
|
||||
uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port)
|
||||
{
|
||||
if (_ctx)
|
||||
{
|
||||
if (_ctx) {
|
||||
_ctx->unref();
|
||||
_ctx = 0;
|
||||
}
|
||||
|
||||
|
||||
ip_addr_t ifaddr;
|
||||
ifaddr.addr = (uint32_t) interfaceAddr;
|
||||
ip_addr_t multicast_addr;
|
||||
@ -95,6 +93,7 @@ uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, ui
|
||||
}
|
||||
|
||||
_ctx = new UdpContext;
|
||||
|
||||
if (!_ctx->listen(*IP_ADDR_ANY, port)) {
|
||||
return 0;
|
||||
}
|
||||
@ -134,9 +133,8 @@ int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
|
||||
ip_addr_t addr;
|
||||
addr.addr = ip;
|
||||
|
||||
if (_ctx)
|
||||
_ctx->unref();
|
||||
_ctx = new UdpContext;
|
||||
if (!_ctx)
|
||||
_ctx = new UdpContext;
|
||||
return (_ctx->connect(addr, port)) ? 1 : 0;
|
||||
}
|
||||
|
||||
@ -227,3 +225,10 @@ uint16_t WiFiUDP::remotePort()
|
||||
return _ctx->getRemotePort();
|
||||
}
|
||||
|
||||
uint16_t WiFiUDP::localPort()
|
||||
{
|
||||
if (!_ctx)
|
||||
return 0;
|
||||
|
||||
return _ctx->getLocalPort();
|
||||
}
|
||||
|
@ -86,6 +86,8 @@ public:
|
||||
virtual IPAddress remoteIP();
|
||||
// Return the port of the host who sent the current incoming packet
|
||||
virtual uint16_t remotePort();
|
||||
// Return the local port for outgoing packets
|
||||
uint16_t localPort();
|
||||
|
||||
};
|
||||
|
||||
|
@ -120,6 +120,14 @@ public:
|
||||
return ntohs(udphdr->src);
|
||||
}
|
||||
|
||||
uint16_t getLocalPort()
|
||||
{
|
||||
if (!_pcb)
|
||||
return 0;
|
||||
|
||||
return _pcb->local_port;
|
||||
}
|
||||
|
||||
bool next()
|
||||
{
|
||||
if (!_rx_buf)
|
||||
|
Reference in New Issue
Block a user