[uim-commit] r609 - trunk/uim

ekato at freedesktop.org ekato at freedesktop.org
Tue Feb 8 01:30:44 PST 2005


Author: ekato
Date: 2005-02-08 01:30:41 -0800 (Tue, 08 Feb 2005)
New Revision: 609

Modified:
   trunk/uim/uim-helper-client.c
   trunk/uim/uim-helper-server.c
   trunk/uim/uim-helper.c
Log:
* uim/uim-helper-client.c : Use non blocking IO for helper socket.
* uim/uim-helper-server.c (parse_content) : Don't check
  uim_helper_fd_writable().
(proc_func) : Cosmetic change.
(uim_helper_server_process_connection) : Minor cleanup.
* uim/uim-helper.c (uim_helper_send_message) : If getting EAGAIN
  error with write(2), retry again after sleeping a while.


Modified: trunk/uim/uim-helper-client.c
===================================================================
--- trunk/uim/uim-helper-client.c	2005-02-08 04:17:44 UTC (rev 608)
+++ trunk/uim/uim-helper-client.c	2005-02-08 09:30:41 UTC (rev 609)
@@ -42,6 +42,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <fcntl.h>
 #include "uim.h"
 #include "uim-helper.h"
 #include "context.h"
@@ -70,6 +71,7 @@
   int fd;
   struct sockaddr_un server;
   char *path = uim_helper_get_pathname();
+  int flag;
   
   uim_fd = -1;
   
@@ -88,6 +90,16 @@
     return -1;
   }
   
+  if ((flag = fcntl(fd, F_GETFL)) == -1) {
+    close(fd);
+    return -1;
+  }
+
+  flag |= O_NONBLOCK;
+  if (fcntl(fd, F_SETFL, flag) == -1) {
+    close(fd);
+    return -1;
+  }
 #ifdef LOCAL_CREDS /* for NetBSD */
   /* Set the socket to receive credentials on the next message */
   {
@@ -97,7 +109,7 @@
 #endif
 
 
-  if(connect(fd, (struct sockaddr *)&server,sizeof(server)) == -1){
+  if (connect(fd, (struct sockaddr *)&server,sizeof(server)) == -1){
     int serv_pid = 0;
     FILE *serv_r = NULL, *serv_w = NULL;
     char buf[128];
@@ -188,7 +200,7 @@
 
   while (uim_helper_fd_readable(fd) > 0) {
     
-    rc = read(fd, buf, sizeof(buf)-1);
+    rc = read(fd, buf, sizeof(buf) - 1);
     buf[rc] = '\0';
     
     if (rc == 0) {

Modified: trunk/uim/uim-helper-server.c
===================================================================
--- trunk/uim/uim-helper-server.c	2005-02-08 04:17:44 UTC (rev 608)
+++ trunk/uim/uim-helper-server.c	2005-02-08 09:30:41 UTC (rev 609)
@@ -144,21 +144,23 @@
   content_len = strlen(content);
 
   for (i = 0; i < nr_client_slots; i++) {
-    if (clients[i].fd != -1 && clients[i].fd != cl->fd &&
-		    (uim_helper_fd_writable(clients[i].fd) > 0)) {
+    if (clients[i].fd == -1 || clients[i].fd == cl->fd) {
+      continue;
+    } else {
       out = content;
       out_len = content_len;
       while (out_len > 0) {
 	if ((ret = write(clients[i].fd, out, out_len)) < 0) {
-	  if (errno == EAGAIN || errno == EINTR)
+	  if (errno == EAGAIN || errno == EINTR) {
 	    continue;
+	  }
 
       	  if (errno == EPIPE) {
 	    close(clients[i].fd);
 	    free_client(&clients[i]);
 	  }
 	  break;
-        }
+	}
 
 	out += ret;
 	out_len -= ret;
@@ -175,18 +177,13 @@
 
   /* do read */
   rc = read(cl->fd, buf, BUFFER_SIZE - 1);
-  if (rc == 0) {
-    close(cl->fd);
+  if (rc <= 0) {
     return -1;
   }
 
-  if (rc < 0) {
-    return -1;
-  }
-
   buf[rc] = '\0';
 
-  cl->rbuf = (char *)realloc(cl->rbuf, strlen(cl->rbuf) + strlen(buf)+1);
+  cl->rbuf = (char *)realloc(cl->rbuf, strlen(cl->rbuf) + strlen(buf) + 1);
   strcat(cl->rbuf, buf);
 
   if (uim_helper_str_terminated(cl->rbuf)) {
@@ -212,7 +209,7 @@
     /* setup readfds */
     FD_ZERO(&readfds);
     FD_SET(serv_fd, &readfds);  
-    for (i = 0; i < nr_client_slots; i ++) {
+    for (i = 0; i < nr_client_slots; i++) {
       int fd = clients[i].fd;
       if (fd == -1) {
 	continue;
@@ -224,7 +221,7 @@
     }
 
     /* call select(), waiting until a file descriptor readable */
-    if (select(fd_biggest+1, &readfds, NULL, NULL, NULL) < 0) {
+    if (select(fd_biggest + 1, &readfds, NULL, NULL, NULL) < 0) {
       perror("select faild");
     }
 
@@ -253,19 +250,20 @@
 	write(cl->fd, buf, 1);
       }
 #endif
-    }    
-    
-    /* check data from clients reached */
-    for (i = 0; i < nr_client_slots; i ++) {
-      if (clients[i].fd != -1 &&
+    } else {
+      /* check data from clients reached */
+      for (i = 0; i < nr_client_slots; i++) {
+	if (clients[i].fd != -1 &&
 	  FD_ISSET(clients[i].fd, &readfds)) {
-	int result;
-	/* actual process */
-	result = proc_func(&clients[i]);
+	  int result;
+	  /* actual process */
+	  result = proc_func(&clients[i]);
 
-	if (result < 0) {
-	  close(clients[i].fd);
-	  free_client(&clients[i]);
+	  if (result < 0) {
+	    FD_CLR(clients[i].fd, &readfds);
+	    close(clients[i].fd);
+	    free_client(&clients[i]);
+	  }
 	}
       }
     }

Modified: trunk/uim/uim-helper.c
===================================================================
--- trunk/uim/uim-helper.c	2005-02-08 04:17:44 UTC (rev 608)
+++ trunk/uim/uim-helper.c	2005-02-08 09:30:41 UTC (rev 609)
@@ -128,8 +128,12 @@
   bufp = buf;
   while (out_len > 0) {
     if ((res = write(fd, bufp, out_len)) < 0) {
-      if (errno == EAGAIN || errno == EINTR)
+      if (errno == EAGAIN || errno == EINTR) {
+	//while (uim_helper_fd_writable(fd) == 0) {
+	  usleep((rand() % getpid()) * 10);
+	//}
 	continue;
+      }
       break;
     }
 



More information about the Uim-commit mailing list