diff src/channel.c @ 8122:39532ee7dd43 v7.4.1355

commit https://github.com/vim/vim/commit/ed5a78e11c80c7b13b233149fd4273b71fc96262 Author: Bram Moolenaar <Bram@vim.org> Date: Fri Feb 19 21:05:03 2016 +0100 patch 7.4.1355 Problem: Win32 console and GUI handle channels differently. Solution: Consolidate code between Win32 console and GUI.
author Christian Brabandt <cb@256bit.org>
date Fri, 19 Feb 2016 21:15:05 +0100
parents 4aea0b0aa714
children 310dd635e8c9
line wrap: on
line diff
--- a/src/channel.c
+++ b/src/channel.c
@@ -1508,17 +1508,11 @@ channel_free_all(void)
     static int
 channel_wait(channel_T *channel, sock_T fd, int timeout)
 {
-#if defined(HAVE_SELECT) && !defined(FEAT_GUI_W32)
-    struct timeval	tval;
-    fd_set		rfds;
-    int			ret;
-
     if (timeout > 0)
 	ch_logn(channel, "Waiting for up to %d msec", timeout);
 
-
 # ifdef WIN32
-    if (channel->CH_SOCK == CHAN_FD_INVALID)
+    if (fd != channel->CH_SOCK)
     {
 	DWORD	nread;
 	int	diff;
@@ -1537,44 +1531,48 @@ channel_wait(channel_T *channel, sock_T 
 	     * TODO: increase the sleep time when looping more often */
 	    Sleep(5);
 	}
-	return FAIL;
     }
+    else
 #endif
+    {
+#if defined(FEAT_GUI_W32)
+	/* Can't check socket for Win32 GUI, always return OK. */
+	ch_log(channel, "Can't check, assuming there is something to read");
+	return OK;
+#else
+# if defined(HAVE_SELECT)
+	struct timeval	tval;
+	fd_set		rfds;
+	int			ret;
 
-    FD_ZERO(&rfds);
-    FD_SET((int)fd, &rfds);
-    tval.tv_sec = timeout / 1000;
-    tval.tv_usec = (timeout % 1000) * 1000;
-    for (;;)
-    {
-	ret = select((int)fd + 1, &rfds, NULL, NULL, &tval);
-# ifdef EINTR
-	if (ret == -1 && errno == EINTR)
-	    continue;
-# endif
-	if (ret <= 0)
+	FD_ZERO(&rfds);
+	FD_SET((int)fd, &rfds);
+	tval.tv_sec = timeout / 1000;
+	tval.tv_usec = (timeout % 1000) * 1000;
+	for (;;)
 	{
-	    ch_log(channel, "Nothing to read");
-	    return FAIL;
+	    ret = select((int)fd + 1, &rfds, NULL, NULL, &tval);
+#  ifdef EINTR
+	    SOCK_ERRNO;
+	    if (ret == -1 && errno == EINTR)
+		continue;
+#  endif
+	    if (ret > 0)
+		return OK;
+	    break;
 	}
-	break;
-    }
-#else
-# ifdef HAVE_POLL
-    struct pollfd	fds;
+# else
+	struct pollfd	fds;
 
-    if (timeout > 0)
-	ch_logn(channel, "Waiting for %d msec", timeout);
-    fds.fd = fd;
-    fds.events = POLLIN;
-    if (poll(&fds, 1, timeout) <= 0)
-    {
-	ch_log(channel, "Nothing to read");
-	return FAIL;
-    }
+	fds.fd = fd;
+	fds.events = POLLIN;
+	if (poll(&fds, 1, timeout) > 0)
+	    return OK;
 # endif
 #endif
-    return OK;
+    }
+    ch_log(channel, "Nothing to read");
+    return FAIL;
 }
 
 /*
@@ -1667,8 +1665,9 @@ channel_read(channel_T *channel, int whi
     }
 #endif
 
-    /* Reading a socket disconnection (readlen == 0), or a socket error. */
-    if (readlen <= 0)
+    /* Reading a socket disconnection (readlen == 0), or a socket error.
+     * TODO: call error callback. */
+    if (readlen <= 0 && channel->ch_job == NULL)
     {
 	/* Queue a "DETACH" netbeans message in the command queue in order to
 	 * terminate the netbeans session later. Do not end the session here
@@ -1836,6 +1835,35 @@ channel_fd2channel(sock_T fd, int *which
 	}
     return NULL;
 }
+
+    void
+channel_handle_events(void)
+{
+    channel_T	*channel;
+    int		which;
+    static int	loop = 0;
+
+    /* Skip heavily polling */
+    if (loop++ % 2)
+	return;
+
+    for (channel = first_channel; channel != NULL; channel = channel->ch_next)
+    {
+#  ifdef FEAT_GUI_W32
+	/* only check the pipes */
+	for (which = CHAN_OUT; which < CHAN_ERR; ++which)
+#  else
+#   ifdef CHANNEL_PIPES
+	/* check the socket and pipes */
+	for (which = CHAN_SOCK; which < CHAN_ERR; ++which)
+#   else
+	/* only check the socket */
+	which = CHAN_SOCK;
+#   endif
+#  endif
+	channel_read(channel, which, "channel_handle_events");
+    }
+}
 # endif
 
 /*
@@ -1969,7 +1997,7 @@ channel_poll_check(int ret_in, void *fds
 }
 # endif /* UNIX && !HAVE_SELECT */
 
-# if (!defined(FEAT_GUI_W32) && defined(HAVE_SELECT)) || defined(PROTO)
+# if (!defined(WIN32) && defined(HAVE_SELECT)) || defined(PROTO)
 /*
  * The type of "rfds" is hidden to avoid problems with the function proto.
  */
@@ -2034,7 +2062,7 @@ channel_select_check(int ret_in, void *r
 
     return ret;
 }
-# endif /* !FEAT_GUI_W32 && HAVE_SELECT */
+# endif /* !WIN32 && HAVE_SELECT */
 
 /*
  * Execute queued up commands.