1 : |
ktanaka |
1.1 |
(defconst SYS_select 93) |
2 : |
|
|
(defconst SYS_socket 97) |
3 : |
|
|
(defconst SYS_accept 99) |
4 : |
|
|
(defconst SYS_bind 104) |
5 : |
|
|
(defconst SYS_listen 106) |
6 : |
|
|
(defconst SYS_shutdown 134) |
7 : |
|
|
(defconst SOCK_STREAM 1) |
8 : |
|
|
(defconst AF_INET 2) |
9 : |
|
|
(defconst INADDR_ANY (make-string 4)) |
10 : |
|
|
(defun select (nfds rmask) |
11 : |
|
|
(syscall SYS_select nfds rmask 0 0 0)) |
12 : |
|
|
(defun socket () |
13 : |
|
|
(syscall SYS_socket AF_INET SOCK_STREAM 0)) |
14 : |
|
|
(defun accept (sock addr addrlen) |
15 : |
|
|
(syscall SYS_accept sock addr addrlen)) |
16 : |
|
|
; for SPARC only |
17 : |
|
|
(defun htons (i) i) |
18 : |
|
|
(defun make-sockaddr (family port addr) |
19 : |
|
|
(let ((ret (make-string 16)) |
20 : |
|
|
(newport (htons port))) |
21 : |
|
|
(sset ret 0 (logand 255 (logshift family -8))) |
22 : |
|
|
(sset ret 1 (logand 255 family)) |
23 : |
|
|
(sset ret 2 (logand 255 (logshift newport -8))) |
24 : |
|
|
(sset ret 3 (logand 255 newport)) |
25 : |
|
|
(string-amend ret addr 4))) |
26 : |
|
|
(defun bind (sock family port addr) |
27 : |
|
|
(syscall SYS_bind sock (make-sockaddr family port addr) 16)) |
28 : |
|
|
(defun listen (sock count) |
29 : |
|
|
(syscall SYS_listen sock count)) |
30 : |
|
|
; |
31 : |
|
|
(defun shutdown (sock how) |
32 : |
|
|
(syscall SYS_shutdown sock how)) |
33 : |
|
|
; |
34 : |
|
|
(defun selbitref (bitarray index) |
35 : |
|
|
(bref bitarray (- 31 index))) |
36 : |
|
|
; |
37 : |
|
|
(defun selbitset (bitarray index value) |
38 : |
|
|
(bset bitarray (- 31 index) value)) |
39 : |
|
|
|
40 : |
|
|
; |
41 : |
|
|
(defun test (port) |
42 : |
|
|
(lets ((sock (socket)) |
43 : |
|
|
(origmask (make-string 4)) |
44 : |
|
|
(rmask (make-string 4)) |
45 : |
|
|
(newfd)(fdlist)(s1)) |
46 : |
|
|
(bind sock AF_INET port INADDR_ANY) |
47 : |
|
|
(listen sock 5) |
48 : |
|
|
(selbitset origmask sock t) |
49 : |
|
|
(loop |
50 : |
|
|
(do ((l fdlist (cdr l))) |
51 : |
|
|
((atom l)) |
52 : |
|
|
(prind (list "stream-modes = " (stream-mode (cadar l))))) |
53 : |
|
|
(loop |
54 : |
|
|
(string-amend rmask origmask) |
55 : |
|
|
(and (<= 0 (select 32 rmask))(exit))) |
56 : |
|
|
(do ((l fdlist (cdr l))) |
57 : |
|
|
((atom l)) |
58 : |
|
|
(prind (list "stream-modes = " (stream-mode (cadar l))))) |
59 : |
|
|
(cond ((selbitref rmask sock) |
60 : |
|
|
(setq newfd (accept sock (make-sockaddr AF_INET 1211 INADDR_ANY) |
61 : |
|
|
(make-string 4))) |
62 : |
|
|
(push (list newfd |
63 : |
|
|
(appendopen (stream newfd))) fdlist) |
64 : |
|
|
(prind (list "newfd=" newfd)) |
65 : |
|
|
(selbitset origmask newfd t))) |
66 : |
|
|
(do ((l fdlist (cdr l))) |
67 : |
|
|
((atom l)) |
68 : |
|
|
(cond ((selbitref rmask (caar l)) |
69 : |
|
|
(prind (list "message from =" (caar l))) |
70 : |
|
|
(prind (list "stream-modes = " (stream-mode (cadar l)) |
71 : |
|
|
(stream-mode (cadar l)))) |
72 : |
|
|
(prind (list "openfiles " openfiles)) |
73 : |
|
|
(let ((curfd (caar l)) |
74 : |
|
|
(rstream (cadar l)) |
75 : |
|
|
(err:end-of-file (function (lambda (x (y)) |
76 : |
|
|
(setq fdlist (remq (assq curfd fdlist) fdlist)) |
77 : |
|
|
(selbitset origmask curfd nil) |
78 : |
|
|
(prind (list "shutdown " (shutdown curfd 2))) |
79 : |
|
|
; (close rstream) |
80 : |
|
|
; (close wstream) |
81 : |
|
|
(prind (list "openfiles " openfiles)) |
82 : |
|
|
(exit))))) |
83 : |
|
|
(setq r (readline (cadar l)))) |
84 : |
|
|
(prind (list "read =" r)) |
85 : |
|
|
; (setq r (eval r)) |
86 : |
|
|
(prind (list "write = " r)) |
87 : |
|
|
(print r (cadar l)))))))) |
88 : |
|
|
|
89 : |
|
|
|
90 : |
|
|
|