1 | #| |
---|
2 | Copyright (c) 2006 Risto Laakso |
---|
3 | All rights reserved. |
---|
4 | |
---|
5 | Redistribution and use in source and binary forms, with or without |
---|
6 | modification, are permitted provided that the following conditions |
---|
7 | are met: |
---|
8 | 1. Redistributions of source code must retain the above copyright |
---|
9 | notice, this list of conditions and the following disclaimer. |
---|
10 | 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | notice, this list of conditions and the following disclaimer in the |
---|
12 | documentation and/or other materials provided with the distribution. |
---|
13 | 3. The name of the author may not be used to endorse or promote products |
---|
14 | derived from this software without specific prior written permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
---|
17 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
---|
18 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
---|
19 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
---|
20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
---|
21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
22 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
23 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
25 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |# |
---|
27 | (in-package :nio) |
---|
28 | |
---|
29 | (declaim (optimize (debug 3) (speed 3) (space 0))) |
---|
30 | |
---|
31 | (defclass async-fd () |
---|
32 | ((write-fd :initarg :write-fd |
---|
33 | :accessor write-fd) |
---|
34 | (read-fd :initarg :read-fd |
---|
35 | :accessor read-fd) |
---|
36 | (foreign-read-buffer :initform (byte-buffer 2096) |
---|
37 | :accessor foreign-read-buffer) |
---|
38 | (foreign-write-buffer :initform (byte-buffer 2096) |
---|
39 | :accessor foreign-write-buffer) |
---|
40 | (read-ready :initform nil |
---|
41 | :accessor read-ready |
---|
42 | :documentation "Have we been notified as read ready and not received EAGAIN from %read?") |
---|
43 | (write-ready :initform nil |
---|
44 | :accessor write-ready |
---|
45 | :documentation "Have we been notified as write ready and not received EAGAIN from %write?") |
---|
46 | (close-pending :initform nil |
---|
47 | :accessor close-pending) |
---|
48 | ;TODO this is either an inet-socket if we are client side or a node is we are server side... |
---|
49 | (socket :initarg :socket |
---|
50 | :accessor socket |
---|
51 | :documentation "The remote node we are talking to"))) |
---|
52 | |
---|
53 | |
---|
54 | (defmethod print-object ((async-fd async-fd) stream) |
---|
55 | (with-slots (read-fd write-fd) async-fd |
---|
56 | (format stream "#<ASYNC-FD :read-fd ~D :write-fd ~D.>" |
---|
57 | read-fd write-fd))) |
---|
58 | |
---|
59 | ;;Implement this in concrete SM for read |
---|
60 | (defgeneric process-read (async-fd)) |
---|
61 | |
---|
62 | ;;Implement this in concrete SM for read |
---|
63 | (defgeneric process-write (async-fd)) |
---|
64 | |
---|
65 | ;;Implement this in concrete SM for timeout processing |
---|
66 | (defgeneric process-timeout (async-fd)) |
---|
67 | |
---|
68 | ;;SM factory |
---|
69 | (defun create-state-machine(sm-type read-fd write-fd socket) |
---|
70 | (let ((sm (make-instance sm-type :read-fd read-fd :write-fd write-fd :socket socket))) |
---|
71 | (format-log t "async-fd:create-state-machine - Created ~S~%" sm) |
---|
72 | (nio-buffer:clear (foreign-read-buffer sm)) |
---|
73 | (nio-buffer:clear (foreign-write-buffer sm)) |
---|
74 | sm)) |
---|
75 | |
---|
76 | ;;override this in concrete SM for close |
---|
77 | (defmethod process-close((async-fd async-fd)reason)()) |
---|
78 | |
---|
79 | |
---|
80 | (defmethod close-sm((async-fd async-fd)) |
---|
81 | :documentation "Mark the socket for close to write" |
---|
82 | #+nio-debug (format t "(mark for)close called with ~A~%" async-fd) |
---|
83 | (with-slots (close-pending) async-fd |
---|
84 | (setf close-pending t))) |
---|
85 | |
---|
86 | (define-condition read-error (error) |
---|
87 | ((errno :initform :errno |
---|
88 | :accessor read-error-errno))) |
---|
89 | |
---|
90 | ;; "Read more data from STATE-MACHINE." |
---|
91 | (defun read-more (state-machine) |
---|
92 | (with-slots (foreign-read-buffer read-fd) state-machine |
---|
93 | #+nio-debug (format t "read-more called with ~A~%" state-machine) |
---|
94 | #+nio-debug (format t "read-more - calling read() into ~A~%" foreign-read-buffer) |
---|
95 | (let ((new-bytes (%read read-fd (buffer-pointer foreign-read-buffer) (remaining foreign-read-buffer)))) |
---|
96 | #+nio-debug (format t "read-more : Read ~A bytes into ~A~%" new-bytes foreign-read-buffer) |
---|
97 | (cond |
---|
98 | ((< new-bytes 0) |
---|
99 | (let ((errno (get-errno))) |
---|
100 | (format-log t "async-fd:read-more - read-error, Errno: ~A~%" errno) |
---|
101 | (cond ((eql errno +ERRNO_EAGAIN+) |
---|
102 | (setf (read-ready state-machine) nil)) |
---|
103 | (t |
---|
104 | (close-fd (read-fd state-machine)) |
---|
105 | (error 'read-error :errno errno))))) |
---|
106 | ((= new-bytes 0) |
---|
107 | (format-log t "async-fd:read-more - EOF on ~A~%" state-machine) |
---|
108 | (error 'read-error));;(throw 'end-of-file nil) |
---|
109 | (t |
---|
110 | ;;Update buffer position |
---|
111 | (inc-position foreign-read-buffer new-bytes) |
---|
112 | #+nio-debug (format t "read-more : Updated buffer ~A~%" foreign-read-buffer) |
---|
113 | (when (> (remaining foreign-read-buffer) 0) |
---|
114 | (setf (read-ready state-machine) nil))))))) |
---|
115 | |
---|
116 | (defun close-async-fd (async-fd) |
---|
117 | "Close ASYNC-FD's fd after everything has been written from write-queue." |
---|
118 | #+nio-debug (format t "close-async-fd called with :async-fd ~A~%" async-fd) |
---|
119 | (with-slots (read-fd write-fd foreign-read-buffer foreign-write-buffer) async-fd |
---|
120 | (nio-buffer:flip foreign-write-buffer) |
---|
121 | #+nio-debug (format t "close-async-fd foreign-write-buffer ~A~%" foreign-write-buffer) |
---|
122 | (assert (eql (remaining foreign-write-buffer) 0)) |
---|
123 | ;; if write-queue is emtpy, close now |
---|
124 | (close-fd read-fd) |
---|
125 | (free-buffer foreign-read-buffer) |
---|
126 | (free-buffer foreign-write-buffer) |
---|
127 | (unless (= read-fd write-fd) (close-fd write-fd)))) |
---|
128 | |
---|
129 | |
---|
130 | (define-condition read-error (error) ()) |
---|
131 | (define-condition write-error (error) |
---|
132 | ((error-number :initarg :error))) |
---|
133 | |
---|
134 | (defun write-more (async-fd) |
---|
135 | "Write data from ASYNC-FD's foreign-write-buffer to the network \ |
---|
136 | Leaves foreign-write-buffer in state ready to be written to \ |
---|
137 | Sets write-ready appropriatly" |
---|
138 | #+nio-debug (format-log t "async-fd:write-more - called with ~A~%" async-fd) |
---|
139 | (with-slots (write-fd foreign-write-buffer close-pending) async-fd |
---|
140 | #+nio-debug (format t "async-fd:write-more - foreign-write-buffer b4 flip ~A~%" foreign-write-buffer) |
---|
141 | (nio-buffer:flip foreign-write-buffer) |
---|
142 | #+nio-debug (format t "async-fd:write-more -foreign-write-buffer after flip ~A~%" foreign-write-buffer) |
---|
143 | (let ((now-written 0)) |
---|
144 | (do ((total-written 0)) |
---|
145 | ((or (eql now-written -1) (eql (remaining foreign-write-buffer) 0)) total-written) |
---|
146 | (progn |
---|
147 | (setf now-written (%write write-fd (buffer-pointer foreign-write-buffer) (remaining foreign-write-buffer))) |
---|
148 | (when (not (eql now-written -1)) |
---|
149 | (inc-position foreign-write-buffer now-written) |
---|
150 | (incf total-written now-written))) |
---|
151 | #+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) |
---|
152 | ) |
---|
153 | |
---|
154 | (when (eql now-written -1) |
---|
155 | ;;Deal with failure |
---|
156 | (let ((err (get-errno))) |
---|
157 | #+nio-debug (format t "write-more - write returned -1 :errno ~A~%" err) |
---|
158 | (if (eql err 11) ;; eagain - failed to write whole buffer need to wait for next notify |
---|
159 | (setf (write-ready async-fd) nil) |
---|
160 | (progn |
---|
161 | (perror) |
---|
162 | (let ((err-cond (make-instance 'write-error :error err))) |
---|
163 | (close-fd (write-fd async-fd)); - deal with in nio-server? |
---|
164 | (error err-cond)))))) |
---|
165 | ;;update buffers |
---|
166 | (if (eql (remaining foreign-write-buffer) 0) |
---|
167 | (clear foreign-write-buffer) |
---|
168 | (compact foreign-write-buffer))) |
---|
169 | #+nio-debug (format t "write buffer after write :~A~%" foreign-write-buffer) |
---|
170 | (when (eql (buffer-position foreign-write-buffer) 0) |
---|
171 | (when close-pending (close-async-fd async-fd))))) |
---|
172 | |
---|
173 | |
---|
174 | (defconstant +MAX-BUFFER-SIZE-BYTES+ (* 1024 1024 15)) |
---|
175 | |
---|
176 | (defmacro realloc-buffer(async-fd accessor size) |
---|
177 | `(let ((buffer (,accessor ,async-fd))) |
---|
178 | (if (>= (buffer-capacity buffer) ,size) |
---|
179 | t |
---|
180 | (let ((new-buffer (byte-buffer ,size))) |
---|
181 | (copy-buffer buffer new-buffer) |
---|
182 | (free-buffer buffer) |
---|
183 | (setf (,accessor ,async-fd) new-buffer))))) |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | ;TODO actually deal with buffer allocation failure |
---|
188 | (defmethod recommend-buffer-size((async-fd async-fd) mode size) |
---|
189 | (if (> size +MAX-BUFFER-SIZE-BYTES+) nil |
---|
190 | (ecase mode |
---|
191 | (:read (realloc-buffer async-fd foreign-read-buffer size)) |
---|
192 | (:write (realloc-buffer async-fd foreign-write-buffer size))))) |
---|
193 | |
---|
194 | |
---|
195 | (defun force-close-async-fd (async-fd) |
---|
196 | "Drop ASYNC-FD's write-queue and close it." |
---|
197 | (free-buffer (slot-value async-fd 'foreign-write-buffer)) |
---|
198 | (close-async-fd async-fd)) |
---|
199 | |
---|
200 | |
---|
201 | (defun add-async-fd (event-queue async-fd mode) |
---|
202 | (ecase mode |
---|
203 | (:read-write (add-fd event-queue (slot-value async-fd 'write-fd) :read-write)))) |
---|
204 | |
---|
205 | |
---|
206 | (defun remove-async-fd (event-queue async-fd mode) |
---|
207 | (ecase mode |
---|
208 | (:read (remove-fd event-queue (slot-value async-fd 'read-fd) :read)) |
---|
209 | (:write (remove-fd event-queue (slot-value async-fd 'write-fd) :write)) |
---|
210 | (:read-write (remove-fd event-queue (slot-value async-fd 'write-fd) :read-write)))) |
---|
211 | |
---|
212 | |
---|
213 | (defun async-fd-read-fd (async-fd) |
---|
214 | (slot-value async-fd 'read-fd)) |
---|
215 | |
---|
216 | (defun async-fd-write-fd (async-fd) |
---|
217 | (slot-value async-fd 'write-fd)) |
---|
218 | |
---|
219 | |
---|
220 | (defun test-realloc() |
---|
221 | (let* ((sm (create-state-machine 'async-fd 1 1 6)) |
---|
222 | (pos-b4-resize (bytebuffer-write-string (foreign-read-buffer sm) "this string is OK"))) |
---|
223 | (recommend-buffer-size sm :read 4096) |
---|
224 | (assert (eql 4096 (buffer-capacity (foreign-read-buffer sm)))) |
---|
225 | (assert (eql 4096 (nio-buffer:buffer-limit (foreign-read-buffer sm)))) |
---|
226 | (assert (eql pos-b4-resize (nio-buffer:buffer-position (foreign-read-buffer sm)))))) |
---|
227 | |
---|