schemelib/examples/udp_server.scm

103 lines
3.1 KiB
Scheme
Raw Normal View History

2020-07-09 08:03:55 +00:00
(load "../bindings/libc.scm")
2020-07-09 14:20:59 +00:00
(load "../bindings/utils.scm")
2020-07-09 10:51:53 +00:00
2020-07-09 10:15:00 +00:00
(define (udpsock-create fx)
2020-07-09 08:03:55 +00:00
(fx
(check-err
2020-07-09 10:12:38 +00:00
(socket 'AF_INET 'SOCK_DGRAM 'IPPROTO_IP)
2020-07-09 08:03:55 +00:00
"Unable to init UDP socket")))
2020-07-09 10:15:00 +00:00
(define (udpsock-reuseaddr sock)
(alloc
(ftype-sizeof int)
(lambda (activation)
(foreign-set! 'int activation 0 1)
(check-err
(setsockopt
sock
'SOL_SOCKET
'SO_REUSEADDR
activation
(ftype-sizeof int))
"Unable to set REUSE ADDRESS"))))
2020-07-10 08:32:46 +00:00
(define (configure-sockaddr_in! addr host port)
(ftype-set! sockaddr_in (common family) addr (domain->int 'AF_INET))
(ftype-set! sockaddr_in (port) addr (htons port))
(check-err
(inet_pton
2020-07-09 12:16:50 +00:00
'AF_INET
host
(ftype-pointer-address
2020-07-09 15:51:13 +00:00
(ftype-&ref sockaddr_in (addr) addr)))
2020-07-10 08:32:46 +00:00
"Unable to convert your IP address to binary"))
(define (build-sockaddr_in host port cb)
(alloc
(ftype-sizeof sockaddr_in)
(lambda (raw-addr)
(let ([addr (make-ftype-pointer sockaddr_in raw-addr)])
(configure-sockaddr_in! addr host port)
(cb addr)))))
(define (udpsock-bind sock host port)
(build-sockaddr_in
host port
(lambda (addr)
(bind
sock
(ftype-pointer-address addr)
(ftype-sizeof sockaddr_in)))))
2020-07-09 10:51:53 +00:00
2020-07-10 08:32:46 +00:00
(define (udpsock-io sock fx)
2020-07-09 14:49:25 +00:00
(letrec*
2020-07-09 13:59:25 +00:00
([bufsize 1500] [straddrsize 255]
[buf (foreign-alloc bufsize)]
2020-07-09 14:49:25 +00:00
[straddr (make-ftype-pointer char
2020-07-09 13:59:25 +00:00
(foreign-alloc straddrsize))]
2020-07-09 15:51:13 +00:00
[addrlen (make-ftype-pointer socklen_t
(foreign-alloc (ftype-sizeof socklen_t)))]
[_ (ftype-set! socklen_t () addrlen (ftype-sizeof sockaddr_in))]
2020-07-09 13:59:25 +00:00
[addr (make-ftype-pointer
sockaddr_in
(foreign-alloc (ftype-sizeof sockaddr_in)))]
2020-07-09 14:49:25 +00:00
[loop (lambda (ifx) (cond ((ifx) (loop ifx)) (#t #t)))])
2020-07-09 13:59:25 +00:00
2020-07-09 14:49:25 +00:00
(loop
(lambda ()
(let ([nread (recvfrom sock buf bufsize 'MSG_DEFAULT addr addrlen)])
(fx
(inet_ntop
'AF_INET
(ftype-pointer-address
2020-07-09 15:51:13 +00:00
(ftype-&ref sockaddr_in (addr) addr))
2020-07-09 14:49:25 +00:00
straddr
straddrsize)
2020-07-09 15:51:13 +00:00
(ntohs (ftype-ref sockaddr_in (port) addr))
2020-07-10 08:32:46 +00:00
buf
nread
(lambda (host port sendbuf sendbuflen)
(configure-sockaddr_in! addr host port)
(sendto sock sendbuf sendbuflen 'MSG_DEFAULT addr (ftype-sizeof sockaddr_in))
)))))
2020-07-09 13:59:25 +00:00
(foreign-free (ftype-pointer-address straddr))
(foreign-free (ftype-pointer-address addrlen))
(foreign-free (ftype-pointer-address addr))
(foreign-free buf)))
2020-07-09 10:15:00 +00:00
(udpsock-create
2020-07-09 10:51:53 +00:00
(lambda (sock)
(udpsock-reuseaddr sock)
2020-07-09 15:51:13 +00:00
(udpsock-bind sock "127.0.0.8" 1337)
2020-07-10 08:32:46 +00:00
(udpsock-io
2020-07-09 13:59:25 +00:00
sock
2020-07-10 08:32:46 +00:00
(lambda (host port buf nread send)
(let* ([charbuf (make-ftype-pointer char buf)]
[udpmsg (char*->string charbuf nread)])
(printf
"host: ~a, port: ~a, nread: ~a, buf: ~a~%"
host port nread udpmsg)
(send host port (ftype-pointer-address (string->char* udpmsg charbuf nread)) nread)
)))))