-
Notifications
You must be signed in to change notification settings - Fork 3
/
sb-fastcgi-server.lisp
79 lines (71 loc) · 3.1 KB
/
sb-fastcgi-server.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
;;;
;;; sb-fastcgi : https://kdr2.com/project/sb-fastcgi.html
;;;
;;; Author : KDr2 <zhuo.dev@gmail.com> https://kdr2.com
;;;
;;; License : BSD License
;;;
(in-package :sb-fastcgi)
(defun server-on-fd (func fd &key (flags 0))
(fcgx-init)
(with-alien ((req (struct fcgx-request)))
(fcgx-init-request (addr req) fd flags)
(do ((rc (fcgx-accept (addr req)) (fcgx-accept (addr req))))
((< rc 0) "ACCEPT ERROR")
(funcall func (addr req))
(sb-fastcgi:fcgx-finish (addr req)))))
(defun server-on-fd-threaded (func fd &key (flags 0) (threads 4))
(fcgx-init)
(do ((count 0 (1+ count)))
((>= count (1- threads)) 'THREADS-START-DONE)
(sb-thread:make-thread (lambda ()
(server-on-fd func fd :flags flags))))
(server-on-fd func fd :flags flags))
(defun simple-server (func)
(server-on-fd func 0))
(defun simple-server-threaded (func &key (threads 4))
(server-on-fd-threaded func 0 :threads threads))
(defun socket-server (func &key
(inet-addr "0.0.0.0")
(sock-path nil)
(port 9000))
(let ((sock nil))
(if (and (stringp sock-path) (> (length sock-path) 0))
(progn
(if (probe-file sock-path) (delete-file sock-path))
(setf sock (make-instance 'sb-bsd-sockets::local-socket
:type :stream))
(sb-bsd-sockets:socket-bind sock sock-path))
(progn
(setf sock (make-instance 'sb-bsd-sockets::inet-socket
:type :stream
:protocol (sb-bsd-sockets::get-protocol-by-name "tcp")))
(setf (sb-bsd-sockets:sockopt-reuse-address sock) t)
(sb-bsd-sockets:socket-bind sock (sb-bsd-sockets:make-inet-address inet-addr) port)))
(unwind-protect
(progn
(sb-bsd-sockets:socket-listen sock 128)
(server-on-fd func (sb-bsd-sockets:socket-file-descriptor sock)))
(sb-bsd-sockets:socket-close sock))))
(defun socket-server-threaded (func &key
(inet-addr "0.0.0.0")
(port 9000)
(sock-path nil)
(threads 4))
(let ((sock nil))
(if (and (stringp sock-path) (> (length sock-path) 0))
(progn
(if (probe-file sock-path) (delete-file sock-path))
(setf sock (make-instance 'sb-bsd-sockets::local-socket
:type :stream))
(sb-bsd-sockets:socket-bind sock sock-path))
(progn
(setf sock (make-instance 'sb-bsd-sockets::inet-socket
:type :stream
:protocol (sb-bsd-sockets::get-protocol-by-name "tcp")))
(sb-bsd-sockets:socket-bind sock (sb-bsd-sockets:make-inet-address inet-addr) port)))
(sb-bsd-sockets:socket-listen sock 128)
(setf (sb-bsd-sockets:sockopt-reuse-address sock) t)
(server-on-fd-threaded func
(sb-bsd-sockets:socket-file-descriptor sock)
:threads threads)))