[nio-cvs] r51 - in branches/home/psmith/restructure/src: buffer io

psmith at common-lisp.net psmith at common-lisp.net
Tue Jan 23 05:52:10 UTC 2007


Author: psmith
Date: Tue Jan 23 00:52:09 2007
New Revision: 51

Modified:
   branches/home/psmith/restructure/src/buffer/buffer.lisp
   branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp
   branches/home/psmith/restructure/src/io/async-fd.lisp
Log:
Added copy-buffer and recommend-buffer-size

Modified: branches/home/psmith/restructure/src/buffer/buffer.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/buffer.lisp	(original)
+++ branches/home/psmith/restructure/src/buffer/buffer.lisp	Tue Jan 23 00:52:09 2007
@@ -177,6 +177,18 @@
   (bytebuffer-write-vector bb (sb-ext:string-to-octets str :external-format external-format)))
 
 
+
+(defmethod copy-buffer ((old byte-buffer) (new byte-buffer))
+  (assert (<= (buffer-capacity old) (buffer-capacity new)))
+  (%memcpy (buffer-buf new) (buffer-buf old) (buffer-capacity old)))
+
+;void *memcpy(void *dest, const void *src, size_t n);
+(cffi:defcfun ("memcpy" %memcpy) :pointer
+  (dest :pointer)
+  (src :pointer)
+  (len :int))
+
+;void *memset(void *s, int c, size_t n);
 (cffi:defcfun ("memset" %memset) :pointer
   (buffer :pointer)
   (byte :int)
@@ -203,6 +215,10 @@
 
     (format t "Mybuf (after get-string): ~A~%" mybuf)
 
+    (let ((test-copy (byte-buffer 1024)))
+      (copy-buffer mybuf test-copy)
+      (format t "new copy: ~A~%" test-copy))
+
     (format t "Mybuf (after clear): ~A~%" (clear mybuf))
 
     (free-buffer mybuf)

Modified: branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp
==============================================================================
--- branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp	(original)
+++ branches/home/psmith/restructure/src/buffer/nio-buffer-package.lisp	Tue Jan 23 00:52:09 2007
@@ -27,5 +27,5 @@
 (defpackage :nio-buffer (:use :cl)
 	    
 	    (:export
-	     byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string flip clear buffer-position
+	     byte-buffer free-buffer remaining inc-position get-string buffer-buf bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string flip clear buffer-position copy-buffer
 	     ))

Modified: branches/home/psmith/restructure/src/io/async-fd.lisp
==============================================================================
--- branches/home/psmith/restructure/src/io/async-fd.lisp	(original)
+++ branches/home/psmith/restructure/src/io/async-fd.lisp	Tue Jan 23 00:52:09 2007
@@ -28,49 +28,24 @@
 
 (declaim (optimize (debug 3) (speed 3) (space 0)))
 
-;;; FFI
-
-
-
-;;; CLASSES
-
-
-
-
-
-
 (defclass async-fd ()
   ((write-fd :initarg :write-fd
   	     :accessor write-fd)
-;;   (write-queue :initform nil)
-
    (read-fd :initarg :read-fd
             :accessor read-fd)
-
    (foreign-read-buffer :initform (byte-buffer 1024)
                         :accessor foreign-read-buffer)
    (foreign-write-buffer :initform (byte-buffer 1024)
 			 :accessor foreign-write-buffer)
-
-;;   (lisp-read-buffer :initform (make-uint8-seq 1024))
-;;   (lisp-read-buffer-write-ptr :initform 0)
-
    (read-ready :initform nil
 	         :accessor read-ready
 	         :documentation "Have we been notified as read ready and not received EAGAIN from %read?")
    (write-ready :initform nil
 	          :accessor write-ready
 	          :documentation "Have we been notified as write ready and not received EAGAIN from %write?")
-
    (close-pending :initform nil)
-
-;;   (accept-filter :initform nil)
-;;   (read-callback :initform nil) 
-
    (socket :initarg :socket
-	   :accessor socket)
-
-   ))
+	   :accessor socket)))
 
 
 (defmethod print-object ((async-fd async-fd) stream)
@@ -93,7 +68,6 @@
     sm))
 
 ;;override this in concrete SM for close
-;(defmethod process-close((async-fd async-fd)reason)())
 (defmethod process-close((async-fd async-fd)reason)())
 
 
@@ -103,30 +77,23 @@
   (with-slots (close-pending) async-fd
       (setf close-pending t)))
 
-;;; FUNCTIONS
-
 (define-condition read-error (error) ())
 
 ;;  "Read more data from STATE-MACHINE."
 (defun read-more (state-machine)
   (with-slots (foreign-read-buffer read-fd) state-machine
 #+nio-debug  (format t "read-more called with ~A~%" state-machine)
-
 #+nio-debug      (format t "read-more - calling read() into ~A~%" foreign-read-buffer) 
     (let ((new-bytes (%read read-fd (buffer-buf foreign-read-buffer) (remaining foreign-read-buffer))))
 #+nio-debug      (format t "read-more : Read ~A bytes into ~A~%" new-bytes foreign-read-buffer)
       (cond
        ((< new-bytes 0)
 	(progn
-
 ;;TODO if ret is -1 and errno is EAGAIN save state and wait for notification
-
 	  (format t "read-error - Errno: ~A~%" (get-errno))
 	  (error 'read-error)))
-
        ((= new-bytes 0)
 	nil);;(throw 'end-of-file nil)
-
        (t
 	  ;;Update buffer position
 	  (inc-position foreign-read-buffer new-bytes)
@@ -146,7 +113,6 @@
 
 (define-condition read-error (error) ())
 
-
 (defun write-more (async-fd)
   "Write data from ASYNC-FD's write bytebuffer"
   (format-log t "async-fd:write-more - called with ~A~%" async-fd)
@@ -154,23 +120,17 @@
 #+nio-debug  (format t "async-fd:write-more - foreign-write-buffer b4 flip ~A~%" foreign-write-buffer)
     (nio-buffer:flip foreign-write-buffer)
 #+nio-debug (format t "async-fd:write-more -foreign-write-buffer after flip ~A~%" foreign-write-buffer)
-
     (let ((now-written 0))
       (do ((total-written 0))
 	  ((or (eql now-written -1) (eql (remaining foreign-write-buffer) 0)) total-written)
 	(progn
 	  (setf now-written (%write write-fd (buffer-buf foreign-write-buffer) (remaining foreign-write-buffer)))
-
-
 	  (when (not (eql now-written -1))
 	    (inc-position foreign-write-buffer now-written)
 	    (incf total-written now-written)))
-#+nio-debug (format t "async-fd:write-more - after write :foreign-write-buffer ~A :now-written ~A :total-written ~A ~%" foreign-write-buffer now-written total-written)
-                                              )
-
-
+#+nio-debug (format t "async-fd:write-more - after write :foreign-write-buffer ~A :now-written ~A :total-written ~A ~%" foreign-write-buffer now-written total-written))
       (if (eql now-written -1)
-      ;;Deal with failure
+	  ;;Deal with failure
 	(let ((err (get-errno)))
 	  (format t "write-more - write returned -1 :errno ~A~%" err)
 	  (unless (eql err 11) ;; eagain - failed to write whole buffer need to wait for next notify
@@ -181,15 +141,33 @@
 	(if (eql (remaining foreign-write-buffer) 0)
 	    (clear foreign-write-buffer)
 	    (error 'not-implemented-yet))))
- 
+
 #+nio-debug    (format t "write buffer after write :~A~%" foreign-write-buffer)
       (when (eql (remaining foreign-write-buffer) 0)
 	(when close-pending (close-async-fd async-fd)))))
 
-    
 
+(defconstant +MAX-BUFFER-SIZE-BYTES+ (* 1024 1024))
 
+(defmacro check-buffer-size (buffer size)
+  `(>= (length ,buffer) ,size))
 
+(defun realloc-buffer (async-fd buffer size)
+  (if (check-buffer-size buffer size)
+      t
+      (let ((new-buffer (byte-buffer size)))
+	(copy-buffer buffer new-buffer)
+	(free-buffer buffer)
+	(setf (foreign-read-buffer async-fd) new-buffer))))
+
+
+;(recom
+(defmethod recommend-buffer-size((async-fd async-fd) mode size)
+  (if (> size +MAX-BUFFER-SIZE-BYTES+) nil
+      (ecase mode
+	(:read (realloc-buffer (foreign-read-buffer async-fd) size))
+	(:write (realloc-buffer (foreign-write-buffer async-fd) size)))))
+   
 
 
 (defun force-close-async-fd (async-fd)
@@ -200,10 +178,7 @@
 
 (defun add-async-fd (event-queue async-fd mode)
   (ecase mode
-;;    (:read (add-fd event-queue (slot-value async-fd 'read-fd) :read :trigger :level))
-;;    (:write (add-fd event-queue (slot-value async-fd 'write-fd) :write :trigger :level))
-    (:read-write (add-fd event-queue (slot-value async-fd 'write-fd) :read-write))
-))
+    (:read-write (add-fd event-queue (slot-value async-fd 'write-fd) :read-write))))
 
 
 (defun remove-async-fd (event-queue async-fd mode)



More information about the Nio-cvs mailing list