cl-rw

Layered streams for Common Lisp
git clone https://logand.com/git/cl-rw.git/
Log | Files | Refs

commit affefbc60cb38d935cfbf9d27fbf602d6ce02865
parent 61c7ed4988beaf4188f8df6555577f95780d0fb9
Author: Tomas Hlavaty <tom@logand.com>
Date:   Fri, 18 Apr 2014 09:03:18 +0200

improve rw.socket and port to ecl

Diffstat:
Mcl-rw.asd | 2+-
Mhttp.lisp | 4++--
Msocket.lisp | 39++++++++++++++++++++++++++++++---------
3 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/cl-rw.asd b/cl-rw.asd @@ -33,7 +33,7 @@ :author "Tomas Hlavaty" :maintainer "Tomas Hlavaty" :licence "MIT" - :depends-on (#+sbcl :sb-concurrency) + :depends-on (#+sbcl :sb-concurrency #+(or sbcl ecl) :sb-bsd-sockets) :serial t :components ((:file "rw") (:file "filesystem") diff --git a/http.lisp b/http.lisp @@ -122,7 +122,7 @@ (string (rw.uri:parse url))) (declare (ignore fragment)) (assert (equal "http" scheme)) - (with-open-stream (s (rw.socket:make-active-tcp-socket host (or port 80))) + (with-open-stream (s (rw.socket:make-tcp-client-socket host (or port 80))) (write-query s :get :http-1.0 path query-string) (write-headers (or headers `(("Host" . ,(if port @@ -350,7 +350,7 @@ ;; TODO also thread limit ;; TODO also thread pool (defun server (host port handler &key quit) - (let ((s (rw.socket:make-passive-tcp-socket host port))) + (let ((s (rw.socket:make-tcp-server-socket host port))) (rw.concurrency:make-thread (format nil "RW.HTTP:SERVER-LOOP ~s ~s" host port) (lambda () diff --git a/socket.lisp b/socket.lisp @@ -1,14 +1,23 @@ (defpackage :rw.socket (:use :cl) (:export :accept - :make-passive-tcp-socket - :make-active-tcp-socket)) + :make-tcp-server-socket + :make-tcp-client-socket)) (in-package :rw.socket) -(defun make-passive-tcp-socket (host port) - #-ccl - (error "TODO port RW.SOCKET:MAKE-PASSIVE-TCP-SOCKET") +(defun make-tcp-server-socket (host port &key backlog) + #-(or sbcl ecl ccl) + (error "TODO port RW.SOCKET:MAKE-TCP-SERVER-SOCKET") + #+(or sbcl ecl) + (let ((x (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp))) + (setf (sb-bsd-sockets:sockopt-reuse-address x) t) + (sb-bsd-sockets:socket-bind x + (car (sb-bsd-sockets:host-ent-addresses + (sb-bsd-sockets:get-host-by-name host))) + port) + (sb-bsd-sockets:socket-listen x (or backlog 5)) + x) #+ccl (ccl:make-socket :connect :passive :address-family :internet @@ -18,9 +27,16 @@ :local-port port :reuse-address t)) -(defun make-active-tcp-socket (host port) - #-ccl - (error "TODO port RW.SOCKET:MAKE-ACTIVE-TCP-SOCKET") +(defun make-tcp-client-socket (host port) + #-(or sbcl ecl ccl) + (error "TODO port RW.SOCKET:MAKE-TCP-CLIENT-SOCKET") + #+(or sbcl ecl) + (let ((x (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp))) + (sb-bsd-sockets:socket-connect x + (car (sb-bsd-sockets:host-ent-addresses + (sb-bsd-sockets:get-host-by-name host))) + port) + x) #+ccl (ccl:make-socket :connect :active :address-family :internet @@ -59,7 +75,12 @@ ;; fd (defun accept (socket) - #-ccl + #-(or sbcl ecl ccl) (error "TODO port RW.SOCKET:ACCEPT") + #+(or sbcl ecl) + (sb-bsd-sockets:socket-make-stream (sb-bsd-sockets:socket-accept socket) + :input t + :output t + :auto-close t) #+ccl (ccl:accept-connection socket))