|
|
|
@ -8,15 +8,15 @@
@@ -8,15 +8,15 @@
|
|
|
|
|
|
|
|
|
|
#include "UDPDevice.h" |
|
|
|
|
|
|
|
|
|
UDPDevice::UDPDevice(const char *ip, uint16_t port): |
|
|
|
|
UDPDevice::UDPDevice(const char *ip, uint16_t port, bool bcast): |
|
|
|
|
_ip(ip), |
|
|
|
|
_port(port) |
|
|
|
|
_port(port), |
|
|
|
|
_bcast(bcast) |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
UDPDevice::~UDPDevice() |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ssize_t UDPDevice::write(const uint8_t *buf, uint16_t n) |
|
|
|
@ -24,17 +24,33 @@ ssize_t UDPDevice::write(const uint8_t *buf, uint16_t n)
@@ -24,17 +24,33 @@ ssize_t UDPDevice::write(const uint8_t *buf, uint16_t n)
|
|
|
|
|
if (!socket.pollout(0)) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
return socket.send(buf, n); |
|
|
|
|
if (_connected) { |
|
|
|
|
return socket.send(buf, n); |
|
|
|
|
} |
|
|
|
|
return socket.sendto(buf, n, _ip, _port); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ssize_t UDPDevice::read(uint8_t *buf, uint16_t n) |
|
|
|
|
{ |
|
|
|
|
return socket.recv(buf, n, 0); |
|
|
|
|
ssize_t ret = socket.recv(buf, n, 0); |
|
|
|
|
if (!_connected && ret > 0) { |
|
|
|
|
const char *ip; |
|
|
|
|
uint16_t port; |
|
|
|
|
socket.last_recv_address(ip, port); |
|
|
|
|
_connected = socket.connect(ip, port); |
|
|
|
|
} |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool UDPDevice::open() |
|
|
|
|
{ |
|
|
|
|
return socket.connect(_ip, _port); |
|
|
|
|
if (_bcast) { |
|
|
|
|
// open now, then connect on first received packet
|
|
|
|
|
socket.set_broadcast(); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
_connected = socket.connect(_ip, _port); |
|
|
|
|
return _connected; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool UDPDevice::close() |
|
|
|
|