[uim-commit] r807 - trunk/uim

yamaken at freedesktop.org yamaken at freedesktop.org
Mon Mar 21 08:38:13 PST 2005


Author: yamaken
Date: 2005-03-21 08:38:10 -0800 (Mon, 21 Mar 2005)
New Revision: 807

Modified:
   trunk/uim/uim-helper-client.c
   trunk/uim/uim-helper-server.c
   trunk/uim/uim-helper.c
   trunk/uim/uim-helper.h
Log:
* This commit resurrects the whole message buffering to fix possible
  message conflict and corruption

* uim/uim-helper.h
  - (uim_helper_buffer_append, uim_helper_buffer_shift,
    uim_helper_buffer_get_message): New private function
* uim/uim-helper.c
  - (uim_helper_buffer_append): New function
  - (uim_helper_buffer_shift): New function generalized from
    shift_read_buffer()
  - (uim_helper_buffer_get_message): New function generalized from
    uim_helper_get_message()
* uim/uim-helper-client.c
  - (BUFFER_SIZE, uim_help_buf, uim_read_buf_size,
    uim_helper_client_focus): Removed
  - (uim_helper_client_focus_in, uim_helper_client_focus_out,
    uim_helper_client_get_prop_list, uim_helper_read_proc,
    uim_helper_get_message): Simplify
  - (shift_read_buffer): Removed to become generalized
    uim_helper_buffer_shift()
* uim/uim-helper-server.c
  - (struct client):
    * Resurrect the member 'rbuf'
    * Rename the member 'write_queue' to 'wbuf' to:
      - symmetrize with 'rbuf'
      - avoid misunderstanding about the variable as 'message queue'
        since I believe that the term 'queue' is used to express
        frame, packet or message-level queue in networking domain
        rather than raw FIFO for octet stream
  - (get_unused_client, free_client):
    * Resurrect the rbuf handlings
    * Follow the renaming of write_queue
  - (distribute_message_fragment): Rename to distribute_message()
  - (distribute_message):
    * Follow the renaming of write_queue
    * Simplify
  - (reflect_message_fragment):
    * Resurrect the whole message buffering to fix possible message
      conflict and corruption
    * Simplify
  - (shift_buffer): Remove to generalize to uim_helper_buffer_shift()
  - (uim_helper_server_process_connection):
    * Follow the renaming of write_queue
    * Follow the renaming of shift_buffer()


Modified: trunk/uim/uim-helper-client.c
===================================================================
--- trunk/uim/uim-helper-client.c	2005-03-21 07:28:33 UTC (rev 806)
+++ trunk/uim/uim-helper-client.c	2005-03-21 16:38:10 UTC (rev 807)
@@ -47,16 +47,11 @@
 #include "context.h"
 
 
-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;
@@ -149,90 +144,40 @@
 void
 uim_helper_client_focus_in(uim_context uc)
 {
-  uim_helper_client_focus(uc, 0);
+  if (uc)
+    uim_helper_send_message(uim_fd, "focus_in\n");
 }
 
 void
 uim_helper_client_focus_out(uim_context uc)
 {
-  uim_helper_client_focus(uc, 1);
+  if (uc)
+    uim_helper_send_message(uim_fd, "focus_out\n");
 }
 
-static void
-uim_helper_client_focus(uim_context uc, int flg)
-{
-  if (uim_fd < 0)
-    return;
-
-  if (!uc)
-    return;
-
-  if (flg == 0)
-    snprintf(uim_help_buf, BUFFER_SIZE, "focus_in\n");
-  else
-    snprintf(uim_help_buf, BUFFER_SIZE, "focus_out\n");
-
-  uim_helper_send_message(uim_fd, uim_help_buf);
-}
-
-
-
 void
 uim_helper_client_get_prop_list(void)
 {
-  snprintf(uim_help_buf, BUFFER_SIZE, "prop_list_get\n");
-  uim_helper_send_message(uim_fd, uim_help_buf);
+  uim_helper_send_message(uim_fd, "prop_list_get\n");
 }
 
 void
 uim_helper_read_proc(int fd)
 {
   int rc;
-  size_t extended_read_buf_size;
 
   while (uim_helper_fd_readable(fd) > 0) {
-    
-    rc = read(fd, uim_recv_buf, sizeof(uim_recv_buf) - 1);
-    uim_recv_buf[rc] = '\0';
-    
+    rc = read(fd, uim_recv_buf, sizeof(uim_recv_buf));
     if (rc == 0 || (rc < 0 && errno != EAGAIN)) {
-      if (uim_disconnect_cb) {
-	uim_disconnect_cb();
-      }
-      uim_fd = -1;
+      uim_helper_close_client_fd(fd);
       return;
     }
-    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 = uim_helper_buffer_append(uim_read_buf, uim_recv_buf, rc);
   }
-  uim_read_buf_size = extended_read_buf_size;
-  return;
 }
 
-static void
-shift_read_buffer(int count)
-{
-  memmove(uim_read_buf, &uim_read_buf[count],
-	  uim_read_buf_size - count);
-  uim_read_buf_size -= count;
-  uim_read_buf[uim_read_buf_size] = '\0';
-}
-
 char *
 uim_helper_get_message(void)
 {
-  size_t msg_size;
-  char *msg, *msg_term;
-
-  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;
+  return uim_helper_buffer_get_message(uim_read_buf);
 }

Modified: trunk/uim/uim-helper-server.c
===================================================================
--- trunk/uim/uim-helper-server.c	2005-03-21 07:28:33 UTC (rev 806)
+++ trunk/uim/uim-helper-server.c	2005-03-21 16:38:10 UTC (rev 807)
@@ -52,7 +52,8 @@
 
 struct client {
   int fd;
-  char *write_queue;
+  char *rbuf;
+  char *wbuf;
 };
 
 #define MAX_CLIENT 32
@@ -143,64 +144,61 @@
   }
   nr_client_slots++;
   clients = realloc(clients, sizeof(struct client) * nr_client_slots);
-  clients[nr_client_slots - 1].write_queue = strdup("");
+  clients[nr_client_slots - 1].rbuf = strdup("");
+  clients[nr_client_slots - 1].wbuf = strdup("");
   return &clients[nr_client_slots - 1];
 }
 
 static void
 free_client(struct client *cl)
 {
-  if (cl->write_queue) {
-    free(cl->write_queue);
-    cl->write_queue = strdup("");
+  if (cl->rbuf) {
+    free(cl->rbuf);
+    cl->rbuf = strdup("");
   }
+  if (cl->wbuf) {
+    free(cl->wbuf);
+    cl->wbuf = strdup("");
+  }
   cl->fd = -1;
 }
 
 static void
-distribute_message_fragment(char *fragment, struct client *cl)
+distribute_message(char *msg, struct client *cl)
 {
   int i;
-  size_t fragment_len, extended_len;
-  char *write_queue;
+  size_t msg_len;
 
-  fragment_len = strlen(fragment);
+  msg_len = strlen(msg);
 
   for (i = 0; i < nr_client_slots; i++) {
     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);
+      clients[i].wbuf = uim_helper_buffer_append(clients[i].wbuf, msg, msg_len);
       FD_SET(clients[i].fd, &s_fdset_write);
     }
   }
 }
 
-static void
-shift_buffer(char *buf, int count)
-{
-  int len = strlen(buf);
-  memmove(buf, &buf[count], len - count);
-  buf[len - count] = '\0';
-}
-
 static int
 reflect_message_fragment(struct client *cl)
 {
   int rc;
+  char *msg;
 
   /* do read */
-  rc = read(cl->fd, read_buf, BUFFER_SIZE - 1);
+  rc = read(cl->fd, read_buf, sizeof(read_buf));
   if (rc <= 0) {
     if (rc < 0 && (errno == EAGAIN || errno == EINTR))
       return 0;
     return -1;
   }
 
-  read_buf[rc] = '\0';
-  distribute_message_fragment(read_buf, cl);
+  cl->rbuf = uim_helper_buffer_append(cl->rbuf, read_buf, rc);
 
+  while ((msg = uim_helper_buffer_get_message(cl->rbuf))) {
+    distribute_message(msg, cl);
+    free(msg);
+  }
   return 1;
 }
 
@@ -269,8 +267,8 @@
 	  int ret, message_len, out_len;
 	  char *out;
 
-	  out = clients[i].write_queue;
-	  message_len = out_len = strlen(clients[i].write_queue);
+	  out = clients[i].wbuf;
+	  message_len = out_len = strlen(clients[i].wbuf);
 	  while (out_len > 0) {
 	    if ((ret = write(clients[i].fd, out, out_len)) < 0) {
 	      if (errno == EAGAIN) {
@@ -296,11 +294,11 @@
 	    }
 	  }
 	  if (out_len == 0) {
-	    free(clients[i].write_queue);
-	    clients[i].write_queue = strdup("");
+	    free(clients[i].wbuf);
+	    clients[i].wbuf = strdup("");
 	    FD_CLR(clients[i].fd, &s_fdset_write);
 	  } else {
-	    shift_buffer(clients[i].write_queue, message_len - out_len);
+	    uim_helper_buffer_shift(clients[i].wbuf, message_len - out_len);
 	  }
 	}
 	if (clients[i].fd != -1 && FD_ISSET(clients[i].fd, &readfds)) {

Modified: trunk/uim/uim-helper.c
===================================================================
--- trunk/uim/uim-helper.c	2005-03-21 07:28:33 UTC (rev 806)
+++ trunk/uim/uim-helper.c	2005-03-21 16:38:10 UTC (rev 807)
@@ -167,6 +167,48 @@
   return 0;
 }
 
+char *
+uim_helper_buffer_append(char *buf, const char *fragment, size_t fragment_size)
+{
+  size_t buf_size, extended_size;
+
+  buf_size = strlen(buf);
+  extended_size = buf_size + fragment_size + 1;
+  buf = (char *)realloc(buf, extended_size);
+  if (buf) {
+    memcpy(&buf[buf_size], fragment, fragment_size);
+    buf[extended_size - 1] = '\0';
+  }
+
+  return buf;
+}
+
+void
+uim_helper_buffer_shift(char *buf, int count)
+{
+  int len = strlen(buf);
+  memmove(buf, &buf[count], len - count);
+  buf[len - count] = '\0';
+}
+
+char *
+uim_helper_buffer_get_message(char *buf)
+{
+  size_t msg_size;
+  char *msg, *msg_term;
+
+  msg_term = strstr(buf, "\n\n");
+  if (msg_term) {
+    msg_size = msg_term + 2 - buf;
+    msg = (char *)malloc(msg_size + 1);
+    memcpy(msg, buf, msg_size);
+    msg[msg_size] = '\0';
+    uim_helper_buffer_shift(buf, msg_size);
+    return msg;
+  }
+  return NULL;
+}
+
 int
 is_setugid(void)
 {

Modified: trunk/uim/uim-helper.h
===================================================================
--- trunk/uim/uim-helper.h	2005-03-21 07:28:33 UTC (rev 806)
+++ trunk/uim/uim-helper.h	2005-03-21 16:38:10 UTC (rev 807)
@@ -54,6 +54,10 @@
 int uim_helper_check_connection_fd(int fd);
 int uim_helper_fd_readable(int fd);
 int uim_helper_fd_writable(int fd);
+char *uim_helper_buffer_append(char *buf,
+			       const char *fragment, size_t fragment_size);
+void uim_helper_buffer_shift(char *buf, int count);
+char *uim_helper_buffer_get_message(char *buf);
 
 #ifdef __cplusplus
 }



More information about the Uim-commit mailing list