Revised.
[riece] / lisp / riece-server.el
index 1500d46..e0ffa28 100644 (file)
@@ -105,9 +105,30 @@ the `riece-server-keyword-map' variable."
 (put 'riece-with-server-buffer 'lisp-indent-function 1)
 (put 'riece-with-server-buffer 'edebug-form-spec '(form body))
 
+(defun riece-flush-send-queue (process reset)
+  (with-current-buffer (process-buffer process)
+    (let ((length 0)
+         string)
+      (if reset
+         (setq riece-send-size 0))
+      (while (and riece-send-queue
+                 (<= riece-send-size riece-max-send-size))
+       (setq string (riece-encode-coding-string (car riece-send-queue))
+             length (length string))
+       (if (> length riece-max-send-size)
+           (message "Long message (%d > %d)" length riece-max-send-size)
+         (setq riece-send-size (+ riece-send-size length))
+         (if (<= riece-send-size riece-max-send-size)
+             (process-send-string process string)))
+       (setq riece-send-queue (cdr riece-send-queue)))
+      (if riece-send-queue
+         (riece-run-at-time riece-send-delay nil
+                            #'riece-flush-send-queue process t)))))
+
 (defun riece-process-send-string (process string)
   (with-current-buffer (process-buffer process)
-    (process-send-string process (riece-encode-coding-string string))))
+    (setq riece-send-queue (nconc riece-send-queue (list string))))
+  (riece-flush-send-queue process nil))
 
 (defun riece-current-server-name ()
   (or riece-overriding-server-name
@@ -140,7 +161,9 @@ the `riece-server-keyword-map' variable."
                                        "-open-server")))
     (unless function
       (error "\"%S\" is not supported" protocol))
-    (setq process (funcall function server server-name))
+    (condition-case nil
+       (setq process (funcall function server server-name))
+      (error))
     (when process
       (with-current-buffer (process-buffer process)
        (make-local-variable 'riece-protocol)
@@ -177,6 +200,9 @@ the `riece-server-keyword-map' variable."
     (make-local-variable 'riece-server-name)
     (make-local-variable 'riece-read-point)
     (setq riece-read-point (point-min))
+    (make-local-variable 'riece-send-queue)
+    (make-local-variable 'riece-send-size)
+    (setq riece-send-size 0)
     (make-local-variable 'riece-obarray)
     (setq riece-obarray (make-vector riece-obarray-size 0))
     (make-local-variable 'riece-coding-system)