1
0
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:
Ivan Grokhotkov
2015-04-12 01:09:45 +08:00
parent 89fe3daa85
commit b026daf43b
5 changed files with 170 additions and 9 deletions

View File

@ -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();
}

View File

@ -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();
};

View File

@ -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)