-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcl.lisp
60 lines (52 loc) · 2.34 KB
/
abcl.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
(in-package :trivial-sockets)
(defun resolve-hostname (name)
(cond
((eql name :any) "0.0.0.0")
((typep name '(vector number 4)) (format nil "~{~A~^.~}" (coerce name 'list)))
(t name)))
(defun open-stream (peer-host peer-port
&key (local-host :any) (local-port 0)
(external-format :default)
(element-type 'character)
(protocol :tcp))
(unless (and (eql local-host :any) (eql local-port 0))
(error 'unsupported :feature :bind))
(unless (eql protocol :tcp)
(error 'unsupported :feature `(:protocol ,protocol)))
(unless (eql external-format :default)
(error 'unsupported :feature :external-format))
(handler-bind ((error (lambda (c) (error 'socket-error :nested-error c))))
(ext:get-socket-stream
(ext:make-socket (resolve-hostname peer-host) peer-port)
:element-type element-type)))
(defun open-server (&key (host :any) (port 0)
(reuse-address t)
(backlog 50)
(protocol :tcp))
"Returns a SERVER object and the port that was bound, as multiple values"
(unless (eql protocol :tcp)
(error 'unsupported :feature `(:protocol ,protocol)))
(unless (equal (resolve-hostname host) "0.0.0.0")
(error 'unsupported :feature :bind))
(unless (= backlog 50)
;; the default, as of jdk 1.4.2
(error 'unsupported :feature :backlog))
(handler-bind ((error (lambda (c) (error 'socket-error :nested-error c))))
(let ((sock (ext:make-server-socket port)))
(java:jcall (java:jmethod "java.net.ServerSocket" "setReuseAddress" "boolean")
sock
(java:make-immediate-object reuse-address :boolean))
(values sock
(java:jcall (java:jmethod "java.net.ServerSocket" "getLocalPort")
sock)))))
(defun close-server (server)
(ext:server-socket-close server))
(defun accept-connection (socket
&key
(external-format :default)
(element-type 'character))
(unless (eql external-format :default)
(error 'unsupported :feature :external-format))
(handler-bind ((error (lambda (c) (error 'socket-error :nested-error c))))
(ext:get-socket-stream (ext:socket-accept socket)
:element-type element-type)))