-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Further improvements to the event-loop.
Signed-off-by: Stelian Ionescu <[email protected]>
- Loading branch information
Showing
6 changed files
with
182 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,13 @@ | |
:author "Stelian Ionescu <[email protected]>" | ||
:maintainer "Stelian Ionescu <[email protected]>" | ||
:licence "LLGPL-2.1" | ||
:depends-on (:io.multiplex :net.sockets) | ||
:depends-on (:io.streams :io.multiplex :net.sockets) | ||
:pathname (merge-pathnames (make-pathname :directory '(:relative "io.event")) | ||
*load-truename*) | ||
:serial t | ||
:components | ||
((:file "pkgdcl") | ||
(:file "io-buffer") | ||
(:file "io-channel") | ||
(:file "io-protocol"))) | ||
(:file "io-protocol") | ||
(:file "server-factory"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*- | ||
;;; | ||
;;; server-factory.lisp - TCP server factories. | ||
;;; | ||
;;; Copyright (C) 2007, Stelian Ionescu <[email protected]> | ||
;;; | ||
;;; This code is free software; you can redistribute it and/or | ||
;;; modify it under the terms of the version 2.1 of | ||
;;; the GNU Lesser General Public License as published by | ||
;;; the Free Software Foundation, as clarified by the | ||
;;; preamble found here: | ||
;;; http://opensource.franz.com/preamble.html | ||
;;; | ||
;;; This program is distributed in the hope that it will be useful, | ||
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
;;; GNU General Public License for more details. | ||
;;; | ||
;;; You should have received a copy of the GNU Lesser General | ||
;;; Public License along with this library; if not, write to the | ||
;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, | ||
;;; Boston, MA 02110-1301, USA | ||
|
||
(in-package :io.event) | ||
|
||
;;; Server factory | ||
|
||
(defclass server-factory () | ||
((protocol :initarg :protocol | ||
:accessor protocol-of))) | ||
|
||
(defgeneric on-connection-received (factory event-base socket)) | ||
|
||
(defmethod on-connection-received ((factory server-factory) | ||
(event-loop event-base) | ||
(socket active-socket)) | ||
) | ||
|
||
;;; Event LOOP | ||
|
||
(defclass event-loop (event-base) | ||
((sockets :initform (make-hash-table :test #'eql) | ||
:accessor sockets-of) | ||
(protocols :initform (make-hash-table :test #'eql) | ||
:accessor protocols-of))) | ||
|
||
(defgeneric listen-tcp (event-loop &key host port factory)) | ||
|
||
(defmethod listen-tcp ((event-loop event-loop) | ||
&key host port factory) | ||
(check-type host address) | ||
(check-type port (unsigned-byte 16)) | ||
(check-type factory server-factory) | ||
(let ((socket (make-socket :family (address-type host) | ||
:type :stream :connect :passive | ||
:local-host host :local-port port))) | ||
(setf (fd-non-blocking socket) t) | ||
(setf (gethash (fd-of socket) (sockets-of event-loop)) socket) | ||
(add-fd event-loop (fd-of socket) :read | ||
#'(lambda (fd event) | ||
(ecase event | ||
(:read | ||
(let ((peer (accept-connection socket))) | ||
(when peer | ||
(let* ((transport (make-instance 'tcp-transport | ||
:event-loop event-loop | ||
:socket socket)) | ||
(protocol (make-instance (protocol-of factory) | ||
:transport transport))) | ||
(setf (gethash (fd-of peer) (protocols-of event-loop)) | ||
(cons peer protocol)))))) | ||
(:error | ||
(error "Got an error on the server socket: ~A~%" socket))))))) |