Skip to content

Commit 91b9610

Browse files
committed
Add socket check on setTimeout
1 parent 9dc648e commit 91b9610

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

Diff for: libraries/WiFi/src/WiFiClient.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,20 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
311311

312312
int WiFiClient::setTimeout(uint32_t seconds)
313313
{
314-
Client::setTimeout(seconds * 1000);
314+
Client::setTimeout(seconds * 1000); // This should be here?
315315
_timeout = seconds * 1000;
316-
struct timeval tv;
317-
tv.tv_sec = seconds;
318-
tv.tv_usec = 0;
319-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
320-
return -1;
316+
if(fd() >= 0) {
317+
struct timeval tv;
318+
tv.tv_sec = seconds;
319+
tv.tv_usec = 0;
320+
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
321+
return -1;
322+
}
323+
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
324+
}
325+
else {
326+
return 0;
321327
}
322-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
323328
}
324329

325330
int WiFiClient::setOption(int option, int *value)

Diff for: libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,18 @@ void WiFiClientSecure::setAlpnProtocols(const char **alpn_protos)
360360
int WiFiClientSecure::setTimeout(uint32_t seconds)
361361
{
362362
_timeout = seconds * 1000;
363-
struct timeval tv;
364-
tv.tv_sec = seconds;
365-
tv.tv_usec = 0;
366-
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
367-
return -1;
363+
if (sslclient->socket >= 0) {
364+
struct timeval tv;
365+
tv.tv_sec = seconds;
366+
tv.tv_usec = 0;
367+
if(setSocketOption(SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0) {
368+
return -1;
369+
}
370+
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
371+
}
372+
else {
373+
return 0;
368374
}
369-
return setSocketOption(SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
370375
}
371376
int WiFiClientSecure::setSocketOption(int option, char* value, size_t len)
372377
{

0 commit comments

Comments
 (0)