source: branches/home/psmith/restructure/src/io/nio-server.lisp @ 48

Last change on this file since 48 was 48, checked in by psmith, 18 years ago

Tidied up logging

File size: 5.9 KB
Line 
1#|
2Copyright (c) 2006 Risto Laakso
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
81. Redistributions of source code must retain the above copyright
9   notice, this list of conditions and the following disclaimer.
102. 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.
133. 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
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25THIS 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(defun trivial-accept (client)
32  (declare (ignore client))
33;;  (format t "Accepting connection from ~S:~D [~A].~%" host port proto)
34  t)
35
36;TODO thread safety
37(defparameter +connected-sockets+ nil
38  "List of sockets that have been connected and are awaiting addition to the event-notification system")
39
40;loop over hashtable
41(defun process-async-fds (client-hash)
42  (maphash #'(lambda (k async-fd) 
43               (format t "Dealing with ~a => ~a~%" k async-fd)
44
45               ;process reads
46               (when (read-ready async-fd) (read-more async-fd))
47               (when (> (buffer-position (foreign-read-buffer async-fd)) 0)
48                 (process-read async-fd))
49
50               ;process-writes
51               (process-write async-fd)
52               (when (write-ready async-fd) (write-more async-fd)))
53           client-hash))
54
55
56                             
57
58(defun start-server (connection-handler accept-filter connection-type 
59                     &key 
60                     (protocol :inet) 
61                     (port (+ (random 60000) 1024)) 
62                     (host "localhost") 
63                     (accept-connection #'trivial-accept))
64                     
65
66  (let (sock
67        (event-queue (make-event-queue))
68        (client-hash (make-hash-table :test 'eql))
69        )
70
71    (setq sock (ecase protocol
72                 (:inet (make-inet-socket)) 
73                 (:inet6 (make-inet6-socket))))
74
75    (unless (ecase protocol 
76              (:inet (bind-inet-socket sock port host))
77              (:inet6 (bind-inet6-socket sock port host)))
78      (error "Can't bind socket!"))
79
80    (set-fd-nonblocking sock)
81
82    (format t "~&Starting server on ~S port ~S.. (socket fd is ~D)~%" host port sock)
83
84    (start-listen sock)
85
86    (add-fd event-queue sock :read :trigger :level)
87           
88    (format t "waiting for events..~%") (force-output)
89
90    (catch 'poll-error-exit
91      (handler-bind ((poll-error #'(lambda (cond) 
92                                     (declare (ignore cond))
93                                     (format t "Poll-error (errno ~A), exiting..~%" (get-errno))
94                                     (throw 'poll-error-exit nil))))
95       
96        (loop 
97           (let ((unix-epoll-events (poll-events event-queue)))
98             (loop for (fd . event) in unix-epoll-events do         
99                  (cond
100                    ;; new connection
101                    ((= fd sock)
102                     (let ((async-fd (socket-accept fd connection-type)))
103#+nio-debug                    (format t "start-server - New conn: ~A~%" async-fd)
104                       (cond
105                         ((null async-fd)
106                          (format t "Accept failed.~%"))
107
108                         ;; accept connection ?
109                         ((set-fd-nonblocking (async-fd-read-fd async-fd))
110                          (funcall accept-connection async-fd)
111                          (setf (gethash (async-fd-read-fd async-fd) client-hash) async-fd)
112                          (add-async-fd event-queue async-fd :read-write)
113;                         (add-async-fd event-queue async-fd :write)
114                          )
115
116                         ;; no accept, close
117                         (t
118                          (format t "start-server - accept-connection closed~%")
119                          (close-async-fd async-fd)))))
120
121
122                    ;; socket i/o available
123                    (t
124                     (let ((async-fd (gethash fd client-hash)))
125                       (format-log t "IO event ~A on ~A~%" event async-fd)
126                       (unless (null async-fd)
127                         (catch 'error-exit
128                           (handler-bind ((read-error #'(lambda (x) 
129                                                          (declare (ignore x))
130                                                          (format t "read-error, dropping ~A.~%" async-fd)
131                                                          (setf (gethash (async-fd-read-fd async-fd) client-hash) nil)
132                                                          (remove-async-fd event-queue async-fd :read)
133                                                          (remove-async-fd event-queue async-fd :write)
134                                                          (force-close-async-fd async-fd)
135                                                          (throw 'error-exit nil))))
136
137                             (when (read-event-p event) (setf (read-ready async-fd) t))
138                             (when (write-event-p event) (setf (write-ready async-fd) t)))))))))
139             (format t "Process client adds~%")
140
141                                        ;add outgoing sockets to event queue
142             (format t "start-server::sockets enqueued ~A~%" +connected-sockets+)
143             (loop for new-fd in +connected-sockets+ do
144                  (format t "Dealing with ~A~%" new-fd)
145                  (setf (gethash (async-fd-read-fd new-fd) client-hash) new-fd)
146                  (add-async-fd event-queue new-fd :read-write))
147
148                                        ;TODO thread safety
149             (setf +connected-sockets+ nil)
150             
151                                        ;loop over async-fd's processing where necessary
152             (process-async-fds client-hash)
153                  ))))
154    (ignore-errors 
155      (close-fd sock))))
156
157
158(defun add-connection (host port connection-type
159                   &key 
160                   (protocol :inet)
161                   
162                   )
163  (let ((sock nil))
164    (setq sock (ecase protocol
165                 (:inet (make-inet-socket)) 
166                 (:inet6 (make-inet6-socket))))
167   
168    (if (connect-inet-socket sock host port)
169        (let ((sm (create-state-machine connection-type sock sock sock)))
170          (push sm +connected-sockets+) 
171          (format t "add-connection::sockets enqueued ~A~%" +connected-sockets+)
172          (return-from add-connection sm))
173        (format t "Connect failed!!~A ~%" (get-errno)))))
174   
Note: See TracBrowser for help on using the repository browser.