@@ -628,9 +628,8 @@ channel_open(
628628 */
629629 while (TRUE)
630630 {
631- #ifndef WIN32
632- long elapsed_msec = 0 ;
633- #endif
631+ long elapsed_msec = 0 ;
632+ int waitnow ;
634633
635634 if (sd >= 0 )
636635 sock_close (sd );
@@ -688,7 +687,7 @@ channel_open(
688687 }
689688
690689 /* If connect() didn't finish then try using select() to wait for the
691- * connection to be made. */
690+ * connection to be made. For Win32 always use select() to wait. */
692691#ifndef WIN32
693692 if (errno != ECONNREFUSED )
694693#endif
@@ -702,19 +701,22 @@ channel_open(
702701 struct timeval start_tv ;
703702 struct timeval end_tv ;
704703#endif
704+ /* Limit the waittime to 50 msec. If it doesn't work within this
705+ * time we close the socket and try creating it again. */
706+ waitnow = waittime > 50 ? 50 : waittime ;
705707
706708 FD_ZERO (& rfds );
707709 FD_SET (sd , & rfds );
708710 FD_ZERO (& wfds );
709711 FD_SET (sd , & wfds );
710712
711- tv .tv_sec = waittime / 1000 ;
712- tv .tv_usec = (waittime % 1000 ) * 1000 ;
713+ tv .tv_sec = waitnow / 1000 ;
714+ tv .tv_usec = (waitnow % 1000 ) * 1000 ;
713715#ifndef WIN32
714716 gettimeofday (& start_tv , NULL );
715717#endif
716718 ch_logn (channel ,
717- "Waiting for connection (waittime %d msec)..." , waittime );
719+ "Waiting for connection (waiting %d msec)..." , waitnow );
718720 ret = select ((int )sd + 1 , & rfds , & wfds , NULL , & tv );
719721
720722 if (ret < 0 )
@@ -729,10 +731,16 @@ channel_open(
729731 }
730732
731733#ifdef WIN32
732- /* On Win32: select() is expected to work and wait for up to the
733- * waittime for the socket to be open. */
734+ /* On Win32: select() is expected to work and wait for up to
735+ * "waitnow" msec for the socket to be open. */
734736 if (FD_ISSET (sd , & wfds ))
735737 break ;
738+ elapsed_msec = waitnow ;
739+ if (waittime > 1 && elapsed_msec < waittime )
740+ {
741+ waittime -= elapsed_msec ;
742+ continue ;
743+ }
736744#else
737745 /* On Linux-like systems: See socket(7) for the behavior
738746 * After putting the socket in non-blocking mode, connect() will
@@ -778,17 +786,20 @@ channel_open(
778786 {
779787 /* The port isn't ready but we also didn't get an error.
780788 * This happens when the server didn't open the socket
781- * yet. Wait a bit and try again. */
782- mch_delay (waittime < 50 ? (long )waittime : 50L , TRUE);
783- ui_breakcheck ();
789+ * yet. Select() may return early, wait until the remaining
790+ * "waitnow" and try again. */
791+ waitnow -= elapsed_msec ;
792+ waittime -= elapsed_msec ;
793+ if (waitnow > 0 )
794+ {
795+ mch_delay ((long )waitnow , TRUE);
796+ ui_breakcheck ();
797+ waittime -= waitnow ;
798+ }
784799 if (!got_int )
785800 {
786- /* reduce the waittime by the elapsed time and the 50
787- * msec delay (or a bit more) */
788- waittime -= elapsed_msec ;
789- if (waittime > 50 )
790- waittime -= 50 ;
791- else
801+ if (waittime <= 0 )
802+ /* give it one more try */
792803 waittime = 1 ;
793804 continue ;
794805 }
0 commit comments