| 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 | (defpackage :nio-server (:use :cl :nio :event-notification) |
|---|
| 28 | (:export start-server)) |
|---|
| 29 | (in-package :nio-server) |
|---|
| 30 | |
|---|
| 31 | (declaim (optimize (debug 3) (speed 3) (space 0))) |
|---|
| 32 | |
|---|
| 33 | (defun trivial-accept (client) |
|---|
| 34 | (declare (ignore client)) |
|---|
| 35 | ;; (format t "Accepting connection from ~S:~D [~A].~%" host port proto) |
|---|
| 36 | t) |
|---|
| 37 | |
|---|
| 38 | (defun start-server (connection-handler accept-filter &key |
|---|
| 39 | (protocol :inet) (port (+ (random 60000) 1024)) (host "localhost") |
|---|
| 40 | (accept-connection #'trivial-accept)) |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | (let (sock |
|---|
| 44 | (event-queue (make-event-queue)) |
|---|
| 45 | (client-hash (make-hash-table :test 'eql)) |
|---|
| 46 | ) |
|---|
| 47 | |
|---|
| 48 | (setq sock (ecase protocol |
|---|
| 49 | (:inet (make-inet-socket)) |
|---|
| 50 | (:inet6 (make-inet6-socket)))) |
|---|
| 51 | |
|---|
| 52 | (unless (ecase protocol |
|---|
| 53 | (:inet (bind-inet-socket sock port host)) |
|---|
| 54 | (:inet6 (bind-inet6-socket sock port host))) |
|---|
| 55 | (error "Can't bind socket!")) |
|---|
| 56 | |
|---|
| 57 | (set-fd-nonblocking sock) |
|---|
| 58 | |
|---|
| 59 | (format t "~&Starting server on ~S port ~S.. (socket fd is ~D)~%" host port sock) |
|---|
| 60 | |
|---|
| 61 | (start-listen sock) |
|---|
| 62 | (add-fd event-queue sock :read) |
|---|
| 63 | |
|---|
| 64 | (format t "waiting for events..~%") (force-output) |
|---|
| 65 | |
|---|
| 66 | (catch 'poll-error-exit |
|---|
| 67 | (handler-bind ((poll-error #'(lambda (cond) |
|---|
| 68 | (declare (ignore cond)) |
|---|
| 69 | (format t "Poll-error, exiting..~%") |
|---|
| 70 | (throw 'poll-error-exit nil)))) |
|---|
| 71 | |
|---|
| 72 | (loop for unix-fds = (poll-events event-queue) do |
|---|
| 73 | |
|---|
| 74 | (loop for fd in unix-fds do |
|---|
| 75 | |
|---|
| 76 | (cond |
|---|
| 77 | |
|---|
| 78 | ;; new connection |
|---|
| 79 | ((= fd sock) |
|---|
| 80 | (let ((async-fd (socket-accept fd))) |
|---|
| 81 | |
|---|
| 82 | (cond |
|---|
| 83 | ((null async-fd) |
|---|
| 84 | (format t "Accept failed.~%")) |
|---|
| 85 | |
|---|
| 86 | ;; accept connection ? |
|---|
| 87 | ((funcall accept-connection async-fd) |
|---|
| 88 | (setf (gethash (async-fd-read-fd async-fd) client-hash) async-fd) |
|---|
| 89 | (set-accept-filter async-fd accept-filter) |
|---|
| 90 | (set-read-callback async-fd connection-handler) |
|---|
| 91 | (add-async-fd event-queue async-fd :read) |
|---|
| 92 | (add-async-fd event-queue async-fd :write) |
|---|
| 93 | ) |
|---|
| 94 | |
|---|
| 95 | ;; no accept, close |
|---|
| 96 | (t |
|---|
| 97 | (close-async-fd async-fd))))) |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | ;; socket i/o available |
|---|
| 101 | (t |
|---|
| 102 | (let ((async-fd (gethash fd client-hash))) |
|---|
| 103 | |
|---|
| 104 | (unless (null async-fd) |
|---|
| 105 | (catch 'error-exit |
|---|
| 106 | (handler-bind ((read-error #'(lambda (x) |
|---|
| 107 | (declare (ignore x)) |
|---|
| 108 | (format t "read-error, dropping ~A.~%" async-fd) |
|---|
| 109 | (setf (gethash (async-fd-read-fd async-fd) client-hash) nil) |
|---|
| 110 | (remove-async-fd event-queue async-fd :read) |
|---|
| 111 | (remove-async-fd event-queue async-fd :write) |
|---|
| 112 | (force-close-async-fd async-fd) |
|---|
| 113 | (throw 'error-exit nil)))) |
|---|
| 114 | |
|---|
| 115 | (read-more async-fd)))) |
|---|
| 116 | )) |
|---|
| 117 | ))))) |
|---|
| 118 | |
|---|
| 119 | (ignore-errors |
|---|
| 120 | (close-fd sock)))) |
|---|