#| Copyright (c) 2006 Risto Laakso All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |# (defpackage :nio-server (:use :cl :nio :event-notification) (:export start-server)) (in-package :nio-server) (declaim (optimize (debug 3) (speed 3) (space 0))) (defun trivial-accept (client) (declare (ignore client)) ;; (format t "Accepting connection from ~S:~D [~A].~%" host port proto) t) (defun start-server (connection-handler accept-filter &key (protocol :inet) (port (+ (random 60000) 1024)) (host "localhost") (accept-connection #'trivial-accept)) (let (sock (event-queue (make-event-queue)) (client-hash (make-hash-table :test 'eql)) ) (setq sock (ecase protocol (:inet (make-inet-socket)) (:inet6 (make-inet6-socket)))) (unless (ecase protocol (:inet (bind-inet-socket sock port host)) (:inet6 (bind-inet6-socket sock port host))) (error "Can't bind socket!")) (set-fd-nonblocking sock) (format t "~&Starting server on ~S port ~S.. (socket fd is ~D)~%" host port sock) (start-listen sock) (add-fd event-queue sock :read :trigger :level) (format t "waiting for events..~%") (force-output) (catch 'poll-error-exit (handler-bind ((poll-error #'(lambda (cond) (declare (ignore cond)) (format t "Poll-error, exiting..~%") (throw 'poll-error-exit nil)))) (loop for unix-epoll-events = (poll-events event-queue) do (loop for (fd . event) in unix-epoll-events do (cond ;; new connection ((= fd sock) (let ((async-fd (socket-accept fd))) #+nio-debug (format t "start-server - New conn: ~A~%" async-fd) (cond ((null async-fd) (format t "Accept failed.~%")) ;; accept connection ? ((set-fd-nonblocking (async-fd-read-fd async-fd)) (funcall accept-connection async-fd) (setf (gethash (async-fd-read-fd async-fd) client-hash) async-fd) (set-accept-filter async-fd accept-filter) (set-read-callback async-fd connection-handler) (add-async-fd event-queue async-fd :read-write) ; (add-async-fd event-queue async-fd :write) ) ;; no accept, close (t (format t "start-server - accept-connection closed~%") (close-async-fd async-fd))))) ;; socket i/o available (t (let ((async-fd (gethash fd client-hash))) #+nio-debug (format t "IO event ~A on ~A~%" event async-fd) (unless (null async-fd) (catch 'error-exit (handler-bind ((read-error #'(lambda (x) (declare (ignore x)) (format t "read-error, dropping ~A.~%" async-fd) (setf (gethash (async-fd-read-fd async-fd) client-hash) nil) (remove-async-fd event-queue async-fd :read) (remove-async-fd event-queue async-fd :write) (force-close-async-fd async-fd) (throw 'error-exit nil)))) (when (read-event-p event) (read-more async-fd)) (when (write-event-p event) (write-more async-fd))))) )) ))))) (ignore-errors (close-fd sock))))