Skip to content

Commit

Permalink
UDP fix - issues 73 and 74 (bportaluri#75)
Browse files Browse the repository at this point in the history
- stop() releases the port
- flush() discards the input data
- read() returns -1 in case of timeout
  • Loading branch information
JiriBilek authored and bportaluri committed Dec 5, 2016
1 parent cb4e3ae commit b17a842
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/WiFiEspUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ uint8_t WiFiEspUDP::begin(uint16_t port)
{
EspDrv::startClient("0", port, sock, UDP_MODE);

WiFiEspClass::allocateSocket(sock); // allocating the socket for the listener
WiFiEspClass::_server_port[sock] = port;
_sock = sock;
_port = port;
Expand Down Expand Up @@ -69,7 +70,13 @@ void WiFiEspUDP::stop()
if (_sock == NO_SOCKET_AVAIL)
return;

//ServerDrv::stopClient(_sock);
// Discard data that might be in the incoming buffer
flush();

// Stop the listener and return the socket to the pool
EspDrv::stopClient(_sock);
WiFiEspClass::_state[_sock] = NA_STATE;
WiFiEspClass::_server_port[_sock] = 0;

_sock = NO_SOCKET_AVAIL;
}
Expand Down Expand Up @@ -132,7 +139,10 @@ int WiFiEspUDP::read()
return -1;

bool connClose = false;
EspDrv::getData(_sock, &b, false, &connClose);

// Read the data and handle the timeout condition
if (! EspDrv::getData(_sock, &b, false, &connClose))
return -1; // Timeout occured

return b;
}
Expand All @@ -155,7 +165,10 @@ int WiFiEspUDP::peek()

void WiFiEspUDP::flush()
{
// TODO: a real check to ensure transmission has been completed
// Discard all input data
int count = available();
while (count-- > 0)
read();
}


Expand Down

0 comments on commit b17a842

Please sign in to comment.