Skip to content

Commit 63f915d

Browse files
committed
Removed virtual + moved socketOptions ot read/write
1 parent ce2cd11 commit 63f915d

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

libraries/WiFi/src/WiFiClient.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,22 +310,25 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
310310
return res;
311311
}
312312

313-
int WiFiClient::setTimeout(uint32_t seconds)
313+
void WiFiClient::setTimeout(uint32_t seconds)
314314
{
315+
_lastReadTimeout = Client::getTimeout();
316+
_lastWriteTimeout = _lastReadTimeout;
315317
Client::setTimeout(seconds * 1000); // This should be here?
316318
_timeout = seconds * 1000;
317-
if(fd() >= 0) {
318-
struct timeval tv;
319-
tv.tv_sec = seconds;
320-
tv.tv_usec = 0;
321-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
322-
return -1;
323-
}
324-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
325-
}
326-
else {
327-
return 0;
328-
}
319+
320+
// if(fd() >= 0) {
321+
// struct timeval tv;
322+
// tv.tv_sec = seconds;
323+
// tv.tv_usec = 0;
324+
// if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
325+
// return -1;
326+
// }
327+
// return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
328+
// }
329+
// else {
330+
// return 0;
331+
// }
329332
}
330333

331334
int WiFiClient::setOption(int option, int *value)
@@ -400,6 +403,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
400403
tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
401404
retry--;
402405

406+
if(_lastWriteTimeout != _timeout){
407+
if(fd() >= 0){
408+
struct timeval tv;
409+
tv.tv_sec = _timeout/1000;
410+
tv.tv_usec = 0;
411+
if(setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
412+
{
413+
_lastWriteTimeout = _timeout;
414+
}
415+
}
416+
}
417+
403418
if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
404419
return 0;
405420
}
@@ -459,6 +474,18 @@ size_t WiFiClient::write(Stream &stream)
459474

460475
int WiFiClient::read(uint8_t *buf, size_t size)
461476
{
477+
if(_lastReadTimeout != _timeout){
478+
if(fd() >= 0){
479+
struct timeval tv;
480+
tv.tv_sec = _timeout/1000;
481+
tv.tv_usec = 0;
482+
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
483+
{
484+
_lastReadTimeout = _timeout;
485+
}
486+
}
487+
}
488+
462489
int res = -1;
463490
if (_rxBuffer) {
464491
res = _rxBuffer->read(buf, size);

libraries/WiFi/src/WiFiClient.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class ESPLwIPClient : public Client
3333
public:
3434
virtual int connect(IPAddress ip, uint16_t port, int32_t timeout) = 0;
3535
virtual int connect(const char *host, uint16_t port, int32_t timeout) = 0;
36-
virtual int setTimeout(uint32_t seconds) = 0;
3736
};
3837

3938
class WiFiClient : public ESPLwIPClient
@@ -43,6 +42,8 @@ class WiFiClient : public ESPLwIPClient
4342
std::shared_ptr<WiFiClientRxBuffer> _rxBuffer;
4443
bool _connected;
4544
int _timeout;
45+
int _lastWriteTimeout;
46+
int _lastReadTimeout;
4647

4748
public:
4849
WiFiClient *next;
@@ -89,7 +90,7 @@ class WiFiClient : public ESPLwIPClient
8990
int setSocketOption(int option, char* value, size_t len);
9091
int setOption(int option, int *value);
9192
int getOption(int option, int *value);
92-
int setTimeout(uint32_t seconds);
93+
void setTimeout(uint32_t seconds);
9394
int setNoDelay(bool nodelay);
9495
bool getNoDelay();
9596

0 commit comments

Comments
 (0)