Skip to content

Commit 5564372

Browse files
committed
Removed virtual + moved socketOptions ot read/write
1 parent 02b384a commit 5564372

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

libraries/WiFi/src/WiFiClient.cpp

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -331,23 +331,25 @@ int WiFiClient::getSocketOption(int level, int option, const void* value, size_t
331331
return res;
332332
}
333333

334-
335-
int WiFiClient::setTimeout(uint32_t seconds)
334+
void WiFiClient::setTimeout(uint32_t seconds)
336335
{
336+
_lastReadTimeout = Client::getTimeout();
337+
_lastWriteTimeout = _lastReadTimeout;
337338
Client::setTimeout(seconds * 1000); // This should be here?
338339
_timeout = seconds * 1000;
339-
if(fd() >= 0) {
340-
struct timeval tv;
341-
tv.tv_sec = seconds;
342-
tv.tv_usec = 0;
343-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
344-
return -1;
345-
}
346-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
347-
}
348-
else {
349-
return 0;
350-
}
340+
341+
// if(fd() >= 0) {
342+
// struct timeval tv;
343+
// tv.tv_sec = seconds;
344+
// tv.tv_usec = 0;
345+
// if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
346+
// return -1;
347+
// }
348+
// return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
349+
// }
350+
// else {
351+
// return 0;
352+
// }
351353
}
352354

353355
int WiFiClient::setOption(int option, int *value)
@@ -418,6 +420,18 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size)
418420
tv.tv_usec = WIFI_CLIENT_SELECT_TIMEOUT_US;
419421
retry--;
420422

423+
if(_lastWriteTimeout != _timeout){
424+
if(fd() >= 0){
425+
struct timeval tv;
426+
tv.tv_sec = _timeout/1000;
427+
tv.tv_usec = 0;
428+
if(setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
429+
{
430+
_lastWriteTimeout = _timeout;
431+
}
432+
}
433+
}
434+
421435
if(select(socketFileDescriptor + 1, NULL, &set, NULL, &tv) < 0) {
422436
return 0;
423437
}
@@ -477,6 +491,18 @@ size_t WiFiClient::write(Stream &stream)
477491

478492
int WiFiClient::read(uint8_t *buf, size_t size)
479493
{
494+
if(_lastReadTimeout != _timeout){
495+
if(fd() >= 0){
496+
struct timeval tv;
497+
tv.tv_sec = _timeout/1000;
498+
tv.tv_usec = 0;
499+
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) >= 0)
500+
{
501+
_lastReadTimeout = _timeout;
502+
}
503+
}
504+
}
505+
480506
int res = -1;
481507
if (_rxBuffer) {
482508
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;
@@ -91,7 +92,7 @@ class WiFiClient : public ESPLwIPClient
9192
int getSocketOption(int level, int option, const void* value, size_t size);
9293
int setOption(int option, int *value);
9394
int getOption(int option, int *value);
94-
int setTimeout(uint32_t seconds);
95+
void setTimeout(uint32_t seconds);
9596
int setNoDelay(bool nodelay);
9697
bool getNoDelay();
9798

0 commit comments

Comments
 (0)