mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
Fixes to UDP so that it no longer has socket 0 hardcoded - all part of issue #436. UdpClass::begin now finds the first available free socket, or fails if they're all in use. UdpClass::stop added to release the socket once it is no longer needed. The global Udp object has also been removed and the examples updated to provide their own instance. Finally, in testing I noticed that the UdpNtpClient didn't print leading 0s if the minute or second was a single-digit, so have taken the opportunity to provide a simple fix for it.
This commit is contained in:
@ -32,10 +32,25 @@
|
||||
#include "Udp.h"
|
||||
|
||||
/* Start UDP socket, listening at local port PORT */
|
||||
void UdpClass::begin(uint16_t port) {
|
||||
uint8_t UdpClass::begin(uint16_t port) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = W5100.readSnSR(i);
|
||||
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
|
||||
_sock = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
_port = port;
|
||||
_sock = 0; //TODO: should not be hardcoded
|
||||
socket(_sock, SnMR::UDP, _port, 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Send packet contained in buf of length len to peer at specified ip, and port */
|
||||
@ -129,8 +144,15 @@ port = myPort;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Release any resources being used by this UdpClass instance */
|
||||
void UdpClass::stop()
|
||||
{
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return;
|
||||
|
||||
close(_sock);
|
||||
|
||||
EthernetClass::_server_port[_sock] = 0;
|
||||
_sock = MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
/* Create one global object */
|
||||
UdpClass Udp;
|
||||
|
Reference in New Issue
Block a user