@@ -253,7 +253,7 @@ add_channel(void)
253
253
return NULL ;
254
254
255
255
channel -> ch_id = next_ch_id ++ ;
256
- ch_log (channel , "Opening channel\n" );
256
+ ch_log (channel , "Created channel\n" );
257
257
258
258
#ifdef CHANNEL_PIPES
259
259
for (which = CHAN_SOCK ; which <= CHAN_IN ; ++ which )
@@ -458,8 +458,12 @@ channel_gui_unregister(channel_T *channel)
458
458
459
459
#endif
460
460
461
+ static char * e_cannot_connect = N_ ("E902: Cannot connect to port" );
462
+
461
463
/*
462
464
* Open a socket channel to "hostname":"port".
465
+ * "waittime" is the time in msec to wait for the connection.
466
+ * When negative wait forever.
463
467
* Returns the channel for success.
464
468
* Returns NULL for failure.
465
469
*/
@@ -492,7 +496,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
492
496
493
497
if ((sd = socket (AF_INET , SOCK_STREAM , 0 )) == -1 )
494
498
{
495
- ch_error (NULL , "in socket() in channel_open().\n" );
499
+ ch_error (channel , "in socket() in channel_open().\n" );
496
500
PERROR ("E898: socket() in channel_open()" );
497
501
channel_free (channel );
498
502
return NULL ;
@@ -505,7 +509,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
505
509
server .sin_port = htons (port );
506
510
if ((host = gethostbyname (hostname )) == NULL )
507
511
{
508
- ch_error (NULL , "in gethostbyname() in channel_open()\n" );
512
+ ch_error (channel , "in gethostbyname() in channel_open()\n" );
509
513
PERROR ("E901: gethostbyname() in channel_open()" );
510
514
sock_close (sd );
511
515
channel_free (channel );
@@ -525,7 +529,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
525
529
)
526
530
{
527
531
SOCK_ERRNO ;
528
- ch_errorn (NULL , "channel_open: Connect failed with errno %d\n" ,
532
+ ch_errorn (channel , "channel_open: Connect failed with errno %d\n" ,
529
533
errno );
530
534
sock_close (sd );
531
535
channel_free (channel );
@@ -534,7 +538,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
534
538
}
535
539
536
540
/* Try connecting to the server. */
537
- ch_logsn (NULL , "Connecting to %s port %d" , hostname , port );
541
+ ch_logsn (channel , "Connecting to %s port %d\n " , hostname , port );
538
542
ret = connect (sd , (struct sockaddr * )& server , sizeof (server ));
539
543
SOCK_ERRNO ;
540
544
if (ret < 0 )
@@ -545,9 +549,9 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
545
549
#endif
546
550
)
547
551
{
548
- ch_errorn (NULL , "channel_open: Connect failed with errno %d\n" ,
552
+ ch_errorn (channel , "channel_open: Connect failed with errno %d\n" ,
549
553
errno );
550
- PERROR (_ ("E902: Cannot connect to port" ));
554
+ PERROR (_ (e_cannot_connect ));
551
555
sock_close (sd );
552
556
channel_free (channel );
553
557
return NULL ;
@@ -558,29 +562,62 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
558
562
{
559
563
struct timeval tv ;
560
564
fd_set wfds ;
561
-
565
+ #if defined(__APPLE__ ) && __APPLE__ == 1
566
+ # define PASS_RFDS
567
+ fd_set rfds ;
568
+
569
+ FD_ZERO (& rfds );
570
+ FD_SET (sd , & rfds );
571
+ /* On Mac a zero timeout almost never works. At least wait one
572
+ * millisecond. */
573
+ if (waittime == 0 )
574
+ waittime = 1 ;
575
+ #endif
562
576
FD_ZERO (& wfds );
563
577
FD_SET (sd , & wfds );
564
578
tv .tv_sec = waittime / 1000 ;
565
579
tv .tv_usec = (waittime % 1000 ) * 1000 ;
566
- ret = select ((int )sd + 1 , NULL , & wfds , NULL , & tv );
580
+
581
+ ch_logn (channel , "Waiting for connection (timeout %d msec)...\n" ,
582
+ waittime );
583
+ ret = select ((int )sd + 1 ,
584
+ #ifdef PASS_RFDS
585
+ & rfds ,
586
+ #else
587
+ NULL ,
588
+ #endif
589
+ & wfds , NULL , & tv );
590
+
567
591
if (ret < 0 )
568
592
{
569
593
SOCK_ERRNO ;
570
- ch_errorn (NULL , "channel_open: Connect failed with errno %d\n" ,
594
+ ch_errorn (channel , "channel_open: Connect failed with errno %d\n" ,
571
595
errno );
572
- PERROR (_ ("E902: Cannot connect to port" ));
596
+ PERROR (_ (e_cannot_connect ));
573
597
sock_close (sd );
574
598
channel_free (channel );
575
599
return NULL ;
576
600
}
601
+ #ifdef PASS_RFDS
602
+ if (ret == 0 && FD_ISSET (sd , & rfds ) && FD_ISSET (sd , & wfds ))
603
+ {
604
+ /* For OS X, this implies error. See tcp(4). */
605
+ ch_error (channel , "channel_open: Connect failed\n" );
606
+ EMSG (_ (e_cannot_connect ));
607
+ sock_close (sd );
608
+ channel_free (channel );
609
+ return NULL ;
610
+ }
611
+ #endif
577
612
if (!FD_ISSET (sd , & wfds ))
578
613
{
579
614
/* don't give an error, we just timed out. */
615
+ ch_error (channel , "Connection timed out\n" );
580
616
sock_close (sd );
581
617
channel_free (channel );
582
618
return NULL ;
583
619
}
620
+ ch_log (channel , "Connection made\n" );
584
621
}
585
622
586
623
if (waittime >= 0 )
@@ -600,7 +637,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
600
637
if ((sd = socket (AF_INET , SOCK_STREAM , 0 )) == -1 )
601
638
{
602
639
SOCK_ERRNO ;
603
- ch_log (NULL , "socket() retry in channel_open()\n" );
640
+ ch_log (channel , "socket() retry in channel_open()\n" );
604
641
PERROR ("E900: socket() retry in channel_open()" );
605
642
channel_free (channel );
606
643
return NULL ;
@@ -614,7 +651,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
614
651
while (retries -- && ((errno == ECONNREFUSED )
615
652
|| (errno == EINTR )))
616
653
{
617
- ch_log (NULL , "retrying...\n" );
654
+ ch_log (channel , "retrying...\n" );
618
655
mch_delay (3000L , TRUE);
619
656
ui_breakcheck ();
620
657
if (got_int )
@@ -633,7 +670,7 @@ channel_open(char *hostname, int port_in, int waittime, void (*close_cb)(void))
633
670
if (!success )
634
671
{
635
672
/* Get here when the server can't be found. */
636
- ch_error (NULL , "Cannot connect to port after retry\n" );
673
+ ch_error (channel , "Cannot connect to port after retry\n" );
637
674
PERROR (_ ("E899: Cannot connect to port after retry" ));
638
675
sock_close (sd );
639
676
channel_free (channel );
@@ -1434,7 +1471,7 @@ channel_wait(channel_T *channel, sock_T fd, int timeout)
1434
1471
int ret ;
1435
1472
1436
1473
if (timeout > 0 )
1437
- ch_logn (channel , "Waiting for %d msec\n" , timeout );
1474
+ ch_logn (channel , "Waiting for up to %d msec\n" , timeout );
1438
1475
1439
1476
1440
1477
# ifdef WIN32
@@ -1447,7 +1484,8 @@ channel_wait(channel_T *channel, sock_T fd, int timeout)
1447
1484
/* reading from a pipe, not a socket */
1448
1485
while (TRUE)
1449
1486
{
1450
- if (PeekNamedPipe ((HANDLE )fd , NULL , 0 , NULL , & nread , NULL ) && nread > 0 )
1487
+ if (PeekNamedPipe ((HANDLE )fd , NULL , 0 , NULL , & nread , NULL )
1488
+ && nread > 0 )
1451
1489
return OK ;
1452
1490
diff = deadline - GetTickCount ();
1453
1491
if (diff < 0 )
0 commit comments