Author: psmith Date: Sun Feb 4 15:53:04 2007 New Revision: 67
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 branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp branches/home/psmith/restructure/src/statemachine/state-machine.lisp Log: client side large packet OK
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 Sun Feb 4 15:53:04 2007 @@ -48,6 +48,9 @@ :initform 0 :accessor buffer-position :documentation "Index of next element to be read/written 0<=position<=limit") + (mark :initarg :position + :initform 0 + :documentation "A marked position") (buf :initarg :buf :accessor buffer-buf)))
@@ -159,6 +162,15 @@ (setf position remaining) (setf limit capacity))))
+(defmethod mark((bb byte-buffer)) + :documentation "mark a position in the buffer for subsequent use with reset" + (with-slots (position mark) bb + (setf mark position))) + +(defmethod reset((bb byte-buffer)) + (with-slots (position mark) bb + (setf position mark))) +
;Used to signal either an attempt has been made to write data to a buffer that is too small using a write (overflow) @@ -231,9 +243,7 @@ ;TODO move string-to-octets into nio-compat (defmethod bytebuffer-write-string((bb byte-buffer) str &optional (external-format :ascii)) :documentation "Returns number of bytes written to bytebuffer" - (let ((vec (sb-ext:string-to-octets str :external-format external-format))) - (when (< (remaining bb) (length vec)) (error 'buffer-too-small-error)) - (bytebuffer-write-vector bb vec))) + (bytebuffer-write-vector bb (sb-ext:string-to-octets str :external-format external-format)))
@@ -241,7 +251,7 @@ (assert (<= (buffer-capacity old) (buffer-capacity new))) (%memcpy (buffer-buf new) (buffer-buf old) (buffer-capacity old)) (setf (buffer-position new) (buffer-position old)) - (setf (buffer-limit new) (buffer-limit old))) + (setf (buffer-limit new) (buffer-capacity new)))
;void *memcpy(void *dest, const void *src, size_t n); (cffi:defcfun ("memcpy" %memcpy) :pointer
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 Sun Feb 4 15:53:04 2007 @@ -31,6 +31,6 @@ bytebuffer-write-vector bytebuffer-write-string bytebuffer-read-vector bytebuffer-read-string bytebuffer-read-8 bytebuffer-read-32 bytebuffer-write-8 bytebuffer-write-32 bytebuffer-insert-8 bytebuffer-insert-32 - flip unflip clear buffer-position copy-buffer buffer-capacity compact + flip unflip clear buffer-position copy-buffer buffer-capacity compact mark reset buffer-too-small-error ))
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 Sun Feb 4 15:53:04 2007 @@ -152,21 +152,34 @@
(defconstant +MAX-BUFFER-SIZE-BYTES+ (* 1024 1024))
-(defun realloc-buffer (async-fd buffer size) - (if (>= (length 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)))) + + +;(let ((buffer (foreign-read-buffer async-fd))) +; (if (>= (length 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))))) + + +(defmacro realloc-buffer(async-fd accessor size) + `(let ((buffer (,accessor ,async-fd))) + (if (>= (buffer-capacity buffer) size) + t + (let ((new-buffer (byte-buffer ,size))) + (copy-buffer buffer new-buffer) + (free-buffer buffer) + (setf (,accessor ,async-fd) new-buffer))))) +
;TODO actually deal with buffer allocation failure (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))))) + (:read (realloc-buffer async-fd foreign-read-buffer size)) + (:write (realloc-buffer async-fd foreign-write-buffer size)))))
Modified: branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp ============================================================================== --- branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp (original) +++ branches/home/psmith/restructure/src/protocol/yarpc/yarpc-packet-factory.lisp Sun Feb 4 15:53:04 2007 @@ -74,12 +74,19 @@
(defmethod write-bytes((packet call-method-packet) buf) #+nio-debug (format-log t "yarpc-packet-factory:write-bytes(call-method-packet) - writing ~%~A to ~%~A~%" packet buf) - (nio-buffer:bytebuffer-write-8 buf +CALL-METHOD-PACKET-ID+) - (nio-buffer:bytebuffer-write-32 buf 0) ; come back and write length later - (nio-buffer:bytebuffer-write-string buf (call-string packet)) - (nio-buffer:bytebuffer-insert-32 buf (buffer-position buf) 1) + (nio-buffer:mark buf) + (handler-case + (progn + (nio-buffer:bytebuffer-write-8 buf +CALL-METHOD-PACKET-ID+) + (nio-buffer:bytebuffer-write-32 buf 0) ; come back and write length later + (nio-buffer:bytebuffer-write-string buf (call-string packet)) + (nio-buffer:bytebuffer-insert-32 buf (buffer-position buf) 1) #+nio-debug (format-log t "yarpc-packet-factory:write-bytes(call-method-packet) - written ~%~A ~%" buf) - ) + ) + (buffer-too-small-error (err) + (nio-buffer:reset buf) + (error err)))) +
(defmethod get-packet-size ((packet call-method-packet)) (+ +yarpc-packet-header-size+ @@ -97,12 +104,18 @@
(defmethod write-bytes((packet method-response-packet) buf) #+nio-debug (format-log t "yarpc-packet-factory:write-bytes - writing ~A to ~A~%" packet buf) - (nio-buffer:bytebuffer-write-8 buf +METHOD-RESPONSE-PACKET-ID+) - (nio-buffer:bytebuffer-write-32 buf 0) ; come back and write length later - (nio-buffer:bytebuffer-write-string buf (write-to-string (response packet))) - (nio-buffer:bytebuffer-insert-32 buf (buffer-position buf) 1) + (nio-buffer:mark buf) + (handler-case + (progn + (nio-buffer:bytebuffer-write-8 buf +METHOD-RESPONSE-PACKET-ID+) + (nio-buffer:bytebuffer-write-32 buf 0) ; come back and write length later + (nio-buffer:bytebuffer-write-string buf (write-to-string (response packet))) + (nio-buffer:bytebuffer-insert-32 buf (buffer-position buf) 1) #+nio-debug (format-log t "yarpc-packet-factory:write-bytes - written ~A~%" buf) -) + ) + (buffer-too-small-error (err) + (nio-buffer:reset buf) + (error err))))
(defmethod get-packet-size ((packet method-response-packet)) (+ +yarpc-packet-header-size+
Modified: branches/home/psmith/restructure/src/statemachine/state-machine.lisp ============================================================================== --- branches/home/psmith/restructure/src/statemachine/state-machine.lisp (original) +++ branches/home/psmith/restructure/src/statemachine/state-machine.lisp Sun Feb 4 15:53:04 2007 @@ -71,9 +71,11 @@ (when outgoing-packet (handler-case (write-bytes outgoing-packet foreign-write-buffer) - (buffer-too-small-error (e) + (buffer-too-small-error (write-error1) (if (recommend-buffer-size sm :write (get-packet-size outgoing-packet)) - (write-bytes outgoing-packet foreign-write-buffer) + (handler-case + (write-bytes outgoing-packet foreign-write-buffer) + (buffer-too-small-error (write-error1) (format t "Failed to write packet after resize (something already in write buffer?, dropping packet ~A~% out buffer:~%~A~%" outgoing-packet foreign-write-buffer))) (format t "Failed to resize io buffer, dropping packet: ~A~%" outgoing-packet))))))))