@@ -175,11 +175,11 @@ class WiFiClientSocketHandle {
175175 }
176176};
177177
178- WiFiClient::WiFiClient ():_connected(false ),next(NULL )
178+ WiFiClient::WiFiClient ():_connected(false ),_timeout(WIFI_CLIENT_DEF_CONN_TIMEOUT_MS), next(NULL )
179179{
180180}
181181
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 )
183183{
184184 clientSocketHandle.reset (new WiFiClientSocketHandle (fd));
185185 _rxBuffer.reset (new WiFiClientRxBuffer (fd));
@@ -208,10 +208,11 @@ void WiFiClient::stop()
208208
209209int WiFiClient::connect (IPAddress ip, uint16_t port)
210210{
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)
214214{
215+ _timeout = timeout;
215216 int sockfd = socket (AF_INET, SOCK_STREAM, 0 );
216217 if (sockfd < 0 ) {
217218 log_e (" socket: %d" , errno);
@@ -230,7 +231,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
230231 FD_ZERO (&fdset);
231232 FD_SET (sockfd, &fdset);
232233 tv.tv_sec = 0 ;
233- tv.tv_usec = timeout * 1000 ;
234+ tv.tv_usec = _timeout * 1000 ;
234235
235236#ifdef ESP_IDF_VERSION_MAJOR
236237 int res = lwip_connect (sockfd, (struct sockaddr *)&serveraddr, sizeof (serveraddr));
@@ -243,13 +244,13 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
243244 return 0 ;
244245 }
245246
246- res = select (sockfd + 1 , nullptr , &fdset, nullptr , timeout <0 ? nullptr : &tv);
247+ res = select (sockfd + 1 , nullptr , &fdset, nullptr , _timeout <0 ? nullptr : &tv);
247248 if (res < 0 ) {
248249 log_e (" select on fd %d, errno: %d, \" %s\" " , sockfd, errno, strerror (errno));
249250 close (sockfd);
250251 return 0 ;
251252 } 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);
253254 close (sockfd);
254255 return 0 ;
255256 } else {
@@ -270,6 +271,14 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
270271 }
271272 }
272273
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+
273282 fcntl ( sockfd, F_SETFL, fcntl ( sockfd, F_GETFL, 0 ) & (~O_NONBLOCK) );
274283 clientSocketHandle.reset (new WiFiClientSocketHandle (sockfd));
275284 _rxBuffer.reset (new WiFiClientRxBuffer (sockfd));
@@ -279,9 +288,10 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
279288
280289int WiFiClient::connect (const char *host, uint16_t port)
281290{
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)
285295{
286296 IPAddress srv ((uint32_t )0 );
287297 if (!WiFiGenericClass::hostByName (host, srv)){
@@ -301,14 +311,20 @@ int WiFiClient::setSocketOption(int option, char* value, size_t len)
301311
302312int WiFiClient::setTimeout (uint32_t seconds)
303313{
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 ;
310327 }
311- return setSocketOption (SO_SNDTIMEO, (char *)&tv, sizeof (struct timeval ));
312328}
313329
314330int WiFiClient::setOption (int option, int *value)
0 commit comments