[uim-commit] r805 - trunk/uim

yamaken at freedesktop.org yamaken at freedesktop.org
Sun Mar 20 19:04:25 PST 2005


Author: yamaken
Date: 2005-03-20 19:04:21 -0800 (Sun, 20 Mar 2005)
New Revision: 805

Modified:
   trunk/uim/uim-helper-client.c
   trunk/uim/uim-helper-server.c
   trunk/uim/uim-helper.c
Log:
* This commit makes efficiency of helper protocol handling better

* uim/uim-helper.c
  - (uim_helper_send_message):
    * Remove obsolete comment
    * Add a debug message
    * Simplify

* uim/uim-helper-client.c
  - (RECV_BUFFER_SIZE): New macro
  - (uim_recv_buf): New static variable
  - (uim_helper_read_proc):
    * Add EAGAIN handling
    * Make efficient
  - (uim_helper_get_message): Make efficient and simple

* uim/uim-helper-server.c
  - (struct client): Remove an unnecessary member 'rbuf'
  - (read_buf): New static variable
  - (get_unused_client, free_client): Remove rbuf handlings
  - (uim_helper_server_get_message): Removed since unnecessary
  - (parse_content): Rename to distribute_message_fragment() because
    the name is inappropriate
  - (distribute_message_fragment):
    * Renamed from parse_content()
    * Simplify
  - (proc_func): Rename to reflect_message_fragment()
  - (reflect_message_fragment):
    * Renamed from proc_func()
    * Make efficient and simple by removing receive buffer
  - (uim_helper_server_process_connection): Follow the renaming of
    proc_func()


Modified: trunk/uim/uim-helper-client.c
===================================================================
--- trunk/uim/uim-helper-client.c	2005-03-20 23:43:18 UTC (rev 804)
+++ trunk/uim/uim-helper-client.c	2005-03-21 03:04:21 UTC (rev 805)
@@ -1,4 +1,3 @@
-
 /*
 
   Copyright (c) 2003-2005 uim Project http://uim.freedesktop.org/
@@ -40,31 +39,36 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include "uim.h"
 #include "uim-helper.h"
 #include "context.h"
 
-static int uim_fd = -1;
-static void (*uim_disconnect_cb)(void);
 
 static void uim_helper_client_focus(uim_context uc, int flg);
 
 #define BUFFER_SIZE (32 * 1024)
+#define RECV_BUFFER_SIZE 1024
+
 /*Common buffer for some functions's temporary buffer.
   Pay attention for use.*/
 static char uim_help_buf[BUFFER_SIZE];
+static char uim_recv_buf[RECV_BUFFER_SIZE];
 static int uim_read_buf_size;
 static char *uim_read_buf;
 
+static int uim_fd = -1;
+static void (*uim_disconnect_cb)(void);
+
+
 static char *
 get_server_command(void)
 {
   return "uim-helper-server";
 }
 
-
 int uim_helper_init_client_fd(void (*disconnect_cb)(void))
 {
   int fd;
@@ -183,25 +187,26 @@
 void
 uim_helper_read_proc(int fd)
 {
-  char buf[BUFFER_SIZE];
   int rc;
+  size_t extended_read_buf_size;
 
   while (uim_helper_fd_readable(fd) > 0) {
     
-    rc = read(fd, buf, sizeof(buf) - 1);
-    buf[rc] = '\0';
+    rc = read(fd, uim_recv_buf, sizeof(uim_recv_buf) - 1);
+    uim_recv_buf[rc] = '\0';
     
-    if (rc == 0) {
+    if (rc == 0 || (rc < 0 && errno != EAGAIN)) {
       if (uim_disconnect_cb) {
 	uim_disconnect_cb();
       }
       uim_fd = -1;
       return;
     }
-    uim_read_buf = (char *)realloc(uim_read_buf, strlen(uim_read_buf) + strlen(buf) + 1);
-    strcat(uim_read_buf, buf);
+    extended_read_buf_size = strlen(uim_read_buf) + rc + 1;
+    uim_read_buf = (char *)realloc(uim_read_buf, extended_read_buf_size);
+    strcat(uim_read_buf, uim_recv_buf);
   }
-  uim_read_buf_size = strlen(uim_read_buf);
+  uim_read_buf_size = extended_read_buf_size;
   return;
 }
 
@@ -217,18 +222,17 @@
 char *
 uim_helper_get_message(void)
 {
-  int i;
-  char *buf;
+  size_t msg_size;
+  char *msg, *msg_term;
 
-  for (i = 0; i < uim_read_buf_size - 1; i++) {
-    if (uim_read_buf[i] == '\n' &&
-	uim_read_buf[i + 1] == '\n') {
-      buf = (char *)malloc(i + 2);
-      memcpy(buf, uim_read_buf, i + 1);
-      buf[i + 1] = '\0';
-      shift_read_buffer(i + 2);
-      return buf;
-    }
+  msg_term = strstr(uim_read_buf, "\n\n");
+  if (msg_term) {
+    msg_size = msg_term + 1 - uim_read_buf;
+    msg = (char *)malloc(msg_size + 1);
+    memcpy(msg, uim_read_buf, msg_size);
+    msg[msg_size] = '\0';
+    shift_read_buffer(msg_size + 1);
+    return msg;
   }
   return NULL;
 }

Modified: trunk/uim/uim-helper-server.c
===================================================================
--- trunk/uim/uim-helper-server.c	2005-03-20 23:43:18 UTC (rev 804)
+++ trunk/uim/uim-helper-server.c	2005-03-21 03:04:21 UTC (rev 805)
@@ -49,6 +49,12 @@
 #include "uim.h"
 #include "uim-helper.h"
 
+
+struct client {
+  int fd;
+  char *write_queue;
+};
+
 #define MAX_CLIENT 32
 #define BUFFER_SIZE 1024
 
@@ -62,12 +68,10 @@
 static int s_max_fd;
 
 static int nr_client_slots;
-static struct client {
-  int fd;
-  char *rbuf;
-  char *write_queue;
-} *clients;
+static struct client *clients;
 
+static char read_buf[BUFFER_SIZE];
+
 /*
   prepare file descriptor.
 */
@@ -139,7 +143,6 @@
   }
   nr_client_slots++;
   clients = realloc(clients, sizeof(struct client) * nr_client_slots);
-  clients[nr_client_slots - 1].rbuf = strdup("");
   clients[nr_client_slots - 1].write_queue = strdup("");
   return &clients[nr_client_slots - 1];
 }
@@ -147,10 +150,6 @@
 static void
 free_client(struct client *cl)
 {
-  if (cl->rbuf) {
-    free(cl->rbuf);
-    cl->rbuf = strdup("");
-  }
   if (cl->write_queue) {
     free(cl->write_queue);
     cl->write_queue = strdup("");
@@ -158,21 +157,21 @@
   cl->fd = -1;
 }
 
-
 static void
-parse_content(char *content, struct client *cl)
+distribute_message_fragment(char *fragment, struct client *cl)
 {
-  int i, content_len;
+  int i;
+  size_t fragment_len, extended_len;
+  char *write_queue;
 
-  content_len = strlen(content);
+  fragment_len = strlen(fragment);
 
   for (i = 0; i < nr_client_slots; i++) {
-    if (clients[i].fd == -1 || clients[i].fd == cl->fd) {
-      continue;
-    } else {
-      clients[i].write_queue = (char *)realloc(clients[i].write_queue,
-		      strlen(clients[i].write_queue) + content_len + 1);
-      strcat(clients[i].write_queue, content);
+    if (clients[i].fd != -1 && clients[i].fd != cl->fd) {
+      write_queue = clients[i].write_queue;
+      extended_len = strlen(write_queue) + fragment_len + 1;
+      clients[i].write_queue = (char *)realloc(write_queue, extended_len);
+      strcat(clients[i].write_queue, fragment);
       FD_SET(clients[i].fd, &s_fdset_write);
     }
   }
@@ -186,50 +185,22 @@
   buf[len - count] = '\0';
 }
 
-static char *
-uim_helper_server_get_message(char *buf)
-{
-  int i;
-  int len = strlen(buf);
-  char *ret;
-
-  for (i = 0; i < len - 1; i++) {
-    if (buf[i] == '\n' && buf[i + 1] == '\n') {
-      ret = (char *)malloc(i + 3);
-      memcpy(ret, buf, i + 2);
-      ret[i + 2] = '\0';
-      shift_buffer(buf, i + 2);
-      return ret;
-    }
-  }
-  return NULL;
-}
-
 static int
-proc_func(struct client *cl)
+reflect_message_fragment(struct client *cl)
 {
   int rc;
-  char buf[BUFFER_SIZE];
-  char *message;
 
   /* do read */
-  rc = read(cl->fd, buf, BUFFER_SIZE - 1);
+  rc = read(cl->fd, read_buf, BUFFER_SIZE - 1);
   if (rc <= 0) {
     if (rc < 0 && (errno == EAGAIN || errno == EINTR))
       return 0;
     return -1;
   }
 
-  buf[rc] = '\0';
+  read_buf[rc] = '\0';
+  distribute_message_fragment(read_buf, cl);
 
-  cl->rbuf = (char *)realloc(cl->rbuf, strlen(cl->rbuf) + strlen(buf) + 1);
-  strcat(cl->rbuf, buf);
-
-  while ((message = uim_helper_server_get_message(cl->rbuf))) {
-    /* process */
-    parse_content(message, cl);
-    free(message);
-  }
   return 1;
 }
 
@@ -335,7 +306,7 @@
 	if (clients[i].fd != -1 && FD_ISSET(clients[i].fd, &readfds)) {
 	  int result;
 	  /* actual process */
-	  result = proc_func(&clients[i]);
+	  result = reflect_message_fragment(&clients[i]);
 
 	  if (result < 0) {
 	    FD_CLR(clients[i].fd, &s_fdset_read);

Modified: trunk/uim/uim-helper.c
===================================================================
--- trunk/uim/uim-helper.c	2005-03-20 23:43:18 UTC (rev 804)
+++ trunk/uim/uim-helper.c	2005-03-21 03:04:21 UTC (rev 805)
@@ -92,50 +92,22 @@
   sig_t old_sigpipe;
   char *buf, *bufp;
 
-  if (fd < 0)
+  if (fd < 0 || !message)
     return;
 
-  if (!message)
-    return;
+  len = strlen(message) + 1;
+  buf = malloc(len + 1);
+  snprintf(buf, len + 1, "%s\n", message);
 
-  /* readable and cannot read any character, means disconnected.
-     so we should read here and proc such condition. */
-
-  /*
-    The assumption described above is not correct. uim_helper_fd()
-    does only select(2), which only indicates whether system is busy
-    or not. i.e. select(2) exists for non-blocking IO. Not indicates
-    connection availability.
-
-    What we have to do is:
-
-    - Don't use uim_helper_fd() for testing connection availability
-
-    - Determine connection error by EPIPE error of write(2) or some
-      appropriate methods
-
-    - Write all data even if uim_helper_fd() has returned 0
-      (i.e. retry until written all data). (uim_helper_fd() == 0) only
-      indicates system is busy.
-
-    I think that we should remove uim_helper_fd() for
-    writing. Blocking write is sufficient for uim.
-    
-    -- YamaKen 2005-02-07
-  */
-
-  len = strlen(message);
-  buf = malloc(len + 2);
-  snprintf(buf, len + 2,"%s\n", message);
-
   old_sigpipe = signal(SIGPIPE, SIG_IGN);
 
-  out_len = len + 1;
+  out_len = len;
   bufp = buf;
   while (out_len > 0) {
     if ((res = write(fd, bufp, out_len)) < 0) {
       if (errno == EAGAIN || errno == EINTR)
 	continue;
+      fprintf(stderr, "uim_helper_send_message(): unknown error\n");
       break;
     }
 



More information about the Uim-commit mailing list