X-Git-Url: https://cgit.sxemacs.org/?p=riece;a=blobdiff_plain;f=lisp%2Friece-server.el;h=9002676b315d36c0dcef8a31ce9f80ce89e8a73f;hp=1500d46b6e79e5e3c19440cd27ce513c199c072a;hb=312ba32461140a7c2b19a8c715181b378ca4456d;hpb=625aafd1d2928e4c74d15c0a1d776097aed8bb3c diff --git a/lisp/riece-server.el b/lisp/riece-server.el index 1500d46..9002676 100644 --- a/lisp/riece-server.el +++ b/lisp/riece-server.el @@ -105,9 +105,63 @@ 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)) +;; stolen (and renamed) from time-date.el. +(defun riece-seconds-to-time (seconds) + "Convert SECONDS (a floating point number) to a time value." + (list (floor seconds 65536) + (floor (mod seconds 65536)) + (floor (* (- seconds (ffloor seconds)) 1000000)))) + +;; stolen (and renamed) from time-date.el. +(defun riece-time-since (time) + "Return the time elapsed since TIME. +TIME should be either a time value or a date-time string." + (let* ((current (current-time)) + (rest (when (< (nth 1 current) (nth 1 time)) + (expt 2 16)))) + (list (- (+ (car current) (if rest -1 0)) (car time)) + (- (+ (or rest 0) (nth 1 current)) (nth 1 time))))) + +;; stolen (and renamed) from time-date.el. +(defun riece-time-less-p (t1 t2) + "Say whether time value T1 is less than time value T2." + (or (< (car t1) (car t2)) + (and (= (car t1) (car t2)) + (< (nth 1 t1) (nth 1 t2))))) + +(defun riece-flush-send-queue (process) + (with-current-buffer (process-buffer process) + (let ((length 0) + string) + (if (riece-time-less-p (riece-seconds-to-time riece-send-delay) + (riece-time-since riece-last-send-time)) + (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) + (process-send-string process string) + (setq riece-send-size (+ riece-send-size length))) + (setq riece-send-queue (cdr riece-send-queue))) + (if riece-send-queue + (progn + (if riece-debug + (message "%d bytes sent, %d bytes left" + riece-send-size + (apply #'+ (mapcar #'length riece-send-queue)))) + ;; schedule next send after a second + (riece-run-at-time riece-send-delay nil + #'riece-flush-send-queue process)) + (if riece-debug + (message "%d bytes sent" riece-send-size))) + (setq riece-last-send-time (current-time))))) + (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)) (defun riece-current-server-name () (or riece-overriding-server-name @@ -140,7 +194,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) @@ -176,6 +232,11 @@ the `riece-server-keyword-map' variable." (make-local-variable 'riece-channel-filter) (make-local-variable 'riece-server-name) (make-local-variable 'riece-read-point) + (make-local-variable 'riece-send-queue) + (make-local-variable 'riece-last-send-time) + (setq riece-last-send-time '(0 0 0)) + (make-local-variable 'riece-send-size) + (setq riece-send-size 0) (setq riece-read-point (point-min)) (make-local-variable 'riece-obarray) (setq riece-obarray (make-vector riece-obarray-size 0))