Skip to content

Commit 373e93f

Browse files
committed
add uv demo
1 parent 4d97aff commit 373e93f

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

apps/test-uv.ss

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2+
;作者:evilbinary on 11/19/16.
3+
4+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5+
(import (scheme) (net uv-ffi) (cffi cffi) )
6+
7+
(define loop (uv-default-loop))
8+
(define server (cffi-alloc 10))
9+
(define addr (cffi-alloc 10))
10+
11+
(define ret 0)
12+
13+
(def-function-callback
14+
make-on-new-connection
15+
(void* int ) void)
16+
17+
(def-function-callback
18+
make-after-read
19+
(void* int void*) void)
20+
21+
(def-function-callback
22+
make-echo-alloc
23+
(void* int void*) void)
24+
25+
26+
(def-function-callback
27+
make-after-shutdown
28+
(void* int) void)
29+
30+
(def-function-callback
31+
make-on-close
32+
(void*) void)
33+
34+
(define on-close
35+
(make-on-close
36+
(lambda (peer)
37+
(cffi-free peer))))
38+
39+
(define after-shutdown
40+
(make-after-shutdown
41+
(lambda (req status)
42+
(printf "after-shutdown\n")
43+
;;(uv-close req->handle on-close )
44+
(cffi-free req))))
45+
46+
(define echo-alloc
47+
(make-echo-alloc
48+
(lambda (handle size buf)
49+
(printf "echo-alloc ~a\n" size)
50+
;;(set-buf-base buf (cffi-alloc size))
51+
;;(set-buf-len buf size)
52+
(let ((b (make-uv-buf-t
53+
(cffi-alloc size) size)))
54+
(lisp2struct b buf)
55+
(printf "alloc b ~a\n" b)
56+
)
57+
58+
)))
59+
60+
61+
(def-struct uv-buf-t
62+
(base void*)
63+
(len int))
64+
65+
66+
(define after-read
67+
(make-after-read
68+
(lambda (handle nread buf)
69+
(let ((a (make-uv-buf-t 0 0)))
70+
(printf "buf=>~a\n" (struct2lisp buf a))
71+
(printf "buf base ~a len=~a\n" (uv-buf-t-base a) (uv-buf-t-len a) )
72+
(printf "after-read nread=~a buf=~s\n" nread (cffi-string (uv-buf-t-base a)))
73+
)
74+
(if (< nread 0)
75+
(begin
76+
(if (not (= -1 nread))
77+
(printf "reqd error ~a\n" (uv-err-name nread)))
78+
(printf "free buf->base and shutdown\n")
79+
;;(cffi-free (get-buf-len buf))
80+
(uv-close handle 0)
81+
))
82+
(if (= 0 nread)
83+
(begin
84+
(printf "free buf->base\n")
85+
;;(cffi-free (get-buf-base buf))
86+
)))))
87+
88+
(uv-tcp-init loop server)
89+
(uv-ip4-addr "0.0.0.0" 7000 addr)
90+
91+
(uv-tcp-bind server addr 0)
92+
(set! ret (uv-listen server 128
93+
(make-on-new-connection
94+
(lambda (data status)
95+
(printf "status=~a\n" status)
96+
(let ((client (cffi-alloc 10)))
97+
(uv-tcp-init loop client)
98+
(if (= 0 (uv-accept server client))
99+
(uv-read-start client echo-alloc after-read)
100+
(uv-close client 0)))
101+
102+
))))
103+
104+
(printf "ret=~a ~a\n" ret (uv-err-name ret) )
105+
(uv-run loop 0)
106+

0 commit comments

Comments
 (0)