Skip to content

Commit f508b55

Browse files
author
Veijo Pesonen
committed
Drains TCP socket buffer from ESP8266 on close
1 parent c43a380 commit f508b55

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

ESP8266/ESP8266.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -552,26 +552,29 @@ void ESP8266::process_oob(uint32_t timeout, bool all) {
552552
int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout)
553553
{
554554
int32_t len;
555-
int32_t ret;
555+
int32_t ret = (int32_t)NSAPI_ERROR_WOULD_BLOCK;
556556

557557
_smutex.lock();
558558

559-
bool done = _parser.send("AT+CIPRECVDATA=%d,%lu", id, amount);
560-
if (!done) {
561-
_smutex.unlock();
562-
return NSAPI_ERROR_DEVICE_ERROR;
563-
}
564559
// NOTE: documentation v3.0 says '+CIPRECVDATA:<data_len>,' but it's not how the FW responds...
565-
done = _parser.recv("+CIPRECVDATA,%ld:", &len)
560+
bool done = _parser.send("AT+CIPRECVDATA=%d,%lu", id, amount)
561+
&& _parser.recv("+CIPRECVDATA,%ld:", &len)
566562
&& _parser.read((char*)data, len)
567563
&& _parser.recv("OK\n");
568564

569-
// Got data?
570565
if (done) {
571-
ret = len;
572-
} else {
573-
// Socket still open?
574-
ret = _socket_open[id].id != id ? 0 : (int32_t)NSAPI_ERROR_WOULD_BLOCK;
566+
_smutex.unlock();
567+
return len;
568+
}
569+
570+
// Socket closed, doesn't mean there couldn't be data left
571+
if (_socket_open[id].id != id) {
572+
done = _parser.send("AT+CIPRECVDATA=%d,%lu", id, amount)
573+
&& _parser.recv("+CIPRECVDATA,%ld:", &len)
574+
&& _parser.read((char*)data, len)
575+
&& _parser.recv("OK\n");
576+
577+
ret = done ? len : 0;
575578
}
576579

577580
_smutex.unlock();

0 commit comments

Comments
 (0)