@@ -175,11 +175,11 @@ class WiFiClientSocketHandle {
175
175
}
176
176
};
177
177
178
- WiFiClient::WiFiClient ():_connected(false ),next(NULL )
178
+ WiFiClient::WiFiClient ():_connected(false ),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS), next(NULL )
179
179
{
180
180
}
181
181
182
- WiFiClient::WiFiClient (int fd):_connected(true ),next(NULL )
182
+ WiFiClient::WiFiClient (int fd):_connected(true ),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS), next(NULL )
183
183
{
184
184
clientSocketHandle.reset (new WiFiClientSocketHandle (fd));
185
185
_rxBuffer.reset (new WiFiClientRxBuffer (fd));
@@ -208,10 +208,11 @@ void WiFiClient::stop()
208
208
209
209
int WiFiClient::connect (IPAddress ip, uint16_t port)
210
210
{
211
- return connect (ip,port,WIFI_CLIENT_DEF_CONN_TIMEOUT_MS );
212
- }
213
- int WiFiClient::connect (IPAddress ip, uint16_t port, int32_t timeout )
211
+ return connect (ip,port,_timeout );
212
+ }
213
+ int WiFiClient::connect (IPAddress ip, uint16_t port, int32_t timeout)
214
214
{
215
+ _timeout = timeout;
215
216
int sockfd = socket (AF_INET, SOCK_STREAM, 0 );
216
217
if (sockfd < 0 ) {
217
218
log_e (" socket: %d" , errno);
@@ -230,7 +231,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
230
231
FD_ZERO (&fdset);
231
232
FD_SET (sockfd, &fdset);
232
233
tv.tv_sec = 0 ;
233
- tv.tv_usec = timeout * 1000 ;
234
+ tv.tv_usec = _timeout * 1000 ;
234
235
235
236
#ifdef ESP_IDF_VERSION_MAJOR
236
237
int res = lwip_connect (sockfd, (struct sockaddr *)&serveraddr, sizeof (serveraddr));
@@ -243,13 +244,13 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
243
244
return 0 ;
244
245
}
245
246
246
- res = select (sockfd + 1 , nullptr , &fdset, nullptr , timeout <0 ? nullptr : &tv);
247
+ res = select (sockfd + 1 , nullptr , &fdset, nullptr , _timeout <0 ? nullptr : &tv);
247
248
if (res < 0 ) {
248
249
log_e (" select on fd %d, errno: %d, \" %s\" " , sockfd, errno, strerror (errno));
249
250
close (sockfd);
250
251
return 0 ;
251
252
} else if (res == 0 ) {
252
- log_i (" select returned due to timeout %d ms for fd %d" , timeout , sockfd);
253
+ log_i (" select returned due to timeout %d ms for fd %d" , _timeout , sockfd);
253
254
close (sockfd);
254
255
return 0 ;
255
256
} else {
@@ -270,6 +271,14 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
270
271
}
271
272
}
272
273
274
+ #define ROE_WIFICLIENT (x,msg ) { if (((x)<0 )) { log_e (" LWIP Socket config of " msg " failed." ); return -1 ; }}
275
+ ROE_WIFICLIENT (lwip_setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof (tv))," SO_RCVTIMEO" );
276
+ ROE_WIFICLIENT (lwip_setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof (tv))," SO_SNDTIMEO" );
277
+
278
+ // These are also set in WiFiClientSecure, should be set here too?
279
+ // ROE_WIFICLIENT(lwip_setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(enable)),"TCP_NODELAY");
280
+ // ROE_WIFICLIENT (lwip_setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable)),"SO_KEEPALIVE");
281
+
273
282
fcntl ( sockfd, F_SETFL, fcntl ( sockfd, F_GETFL, 0 ) & (~O_NONBLOCK) );
274
283
clientSocketHandle.reset (new WiFiClientSocketHandle (sockfd));
275
284
_rxBuffer.reset (new WiFiClientRxBuffer (sockfd));
@@ -279,9 +288,10 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
279
288
280
289
int WiFiClient::connect (const char *host, uint16_t port)
281
290
{
282
- return connect (host,port,WIFI_CLIENT_DEF_CONN_TIMEOUT_MS);
283
- }
284
- int WiFiClient::connect (const char *host, uint16_t port, int32_t timeout )
291
+ return connect (host,port,_timeout);
292
+ }
293
+
294
+ int WiFiClient::connect (const char *host, uint16_t port, int32_t timeout)
285
295
{
286
296
IPAddress srv ((uint32_t )0 );
287
297
if (!WiFiGenericClass::hostByName (host, srv)){
@@ -301,14 +311,20 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
301
311
302
312
int WiFiClient::setTimeout (uint32_t seconds)
303
313
{
304
- Client::setTimeout (seconds * 1000 );
305
- struct timeval tv;
306
- tv.tv_sec = seconds;
307
- tv.tv_usec = 0 ;
308
- if (setSocketOption (SO_RCVTIMEO, (char *)&tv, sizeof (struct timeval )) < 0 ) {
309
- return -1 ;
314
+ Client::setTimeout (seconds * 1000 ); // This should be here?
315
+ _timeout = seconds * 1000 ;
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 ;
310
327
}
311
- return setSocketOption (SO_SNDTIMEO, (char *)&tv, sizeof (struct timeval ));
312
328
}
313
329
314
330
int WiFiClient::setOption (int option, int *value)
0 commit comments