#| 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. |# (in-package :nio) (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) ;TODO thread safety (defparameter +connected-sockets-queue+ (nio-utils:concurrent-queue) "List of node objects that are to be connected to") ;loop over hashtable (defun process-async-fds (client-hash) (let ((removals nil)) (maphash #'(lambda (k async-fd) #+nio-debug2 (format-log t "Dealing with ~a => ~a~%" k async-fd) ;process reads (handler-case (progn (process-timeout async-fd) (when (read-ready async-fd) (read-more async-fd)) (when (> (buffer-position (foreign-read-buffer async-fd)) 0) (process-read async-fd)) ;process-writes (loop (when (and (write-ready async-fd) (> (buffer-position (foreign-write-buffer async-fd)) 0)) (write-more async-fd)) (when (write-ready async-fd) (assert (eql (buffer-position (foreign-write-buffer async-fd)) 0)) (process-write async-fd)) (unless (and (write-ready async-fd) (> (buffer-position (foreign-write-buffer async-fd)) 0)) (return))) ;process normal close (when (close-pending async-fd) (write-more async-fd) (push async-fd removals))) (read-error (re) (push async-fd removals)) (write-error (we) (push async-fd removals)) (timeout (to) (push async-fd removals)))) client-hash) (dolist (async-fd removals) (format-log t "nio-server:process-async-fds processing remove for ~a~%" async-fd) (close-sm async-fd) (setf (active-conn (socket async-fd)) nil) (remhash (async-fd-read-fd async-fd) client-hash)))) (defun start-server (connection-type &key (protocol :inet) (port 0) ;//if set then listen (host "127.0.0.1") (accept-connection #'trivial-accept)) (let (sock (event-queue (make-event-queue)) (client-hash (make-hash-table :test 'eql)) ) (when (not (eql port 0)) (format t "Binding to ~A:~A~%" host port) (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 (errno ~A), exiting..~%" (get-errno)) (throw 'poll-error-exit nil)))) (loop (let ((unix-epoll-events (poll-events event-queue))) (loop for (fd . event) in unix-epoll-events do (cond ;; new connection ((and sock (= fd sock)) (progn #+nio-debug (format t "start-server - incomming conn") (let ((async-fd (socket-accept fd connection-type))) #+nio-debug (format t "start-server - New conn: ~A~%" async-fd) (cond ((null async-fd) (format t "Accept failed.~%")) ;; accept connection ? ((funcall accept-connection async-fd) (sleep 0.1) (let ((nb-ret (set-fd-nonblocking (async-fd-read-fd async-fd)))) (format t "set bb ret: ~A :flags ~A~%" nb-ret (get-fd-flags (async-fd-read-fd async-fd))) (when (< nb-ret 0) (format t "Error setting socket non-blocking: ") (perror))) (setf (gethash (async-fd-read-fd async-fd) client-hash) async-fd) (add-async-fd event-queue async-fd :read-write)) ;; no accept, close (t (format-log 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-log t "nio-server::start-server - 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)))) (if (error-event-p event) (close-sm async-fd) (progn (when (read-event-p event) (setf (read-ready async-fd) t)) (when (write-event-p event) (setf (write-ready async-fd) t))))))))))) ;add outgoing sockets to event queue #+nio-debug2 (format-log t "nio-server:start-server - Processing new connections queue ~A~%" +connected-sockets-queue+) (loop for node = (nio-utils:take +connected-sockets-queue+ :blocking-call nil) until (null node) do #+nio-debug (format-log t "nio-server:start-server - adding node to nodes-list ~A~%" node) (push node *nodes-list*)) (with-connect-ready-nodes (a-node) #+nio-debug (format-log t "nio-server:start-server - attempting connection to node ~A~%" a-node) (let ((new-fd (connect a-node connection-type))) #+nio-debug (format-log t "nio-server:start-server - connect returned async-fd ~A~%" new-fd) (update-last-connect-attempt a-node) (when new-fd #+nio-debug (format-log t "nio-server:start-server - adding connection to nio thread ~A~%" new-fd) (set-fd-nonblocking (async-fd-read-fd new-fd)) (setf (gethash (async-fd-read-fd new-fd) client-hash) new-fd) (setf (active-conn a-node) new-fd) (add-async-fd event-queue new-fd :read-write)))) ;loop over async-fd's processing where necessary (process-async-fds client-hash) )))) (ignore-errors (close-fd sock)))) (defun connect(node connection-type &key (protocol :inet)) (format-log t "nio-server:connect - Called with: ~A ~A~%" protocol node) (let ((sock nil)) (setq sock (ecase protocol (:inet (make-inet-socket)) (:inet6 (make-inet6-socket)))) (if (connect-inet-socket sock node) (let ((sm (create-state-machine connection-type sock sock node))) (return-from connect sm)) (progn (format t "Connect failed!!~A ~%" (get-errno)) (close-fd sock) nil)))) (defun add-connection(node) (nio-utils:add +connected-sockets-queue+ node))