zepto-lang / zepto

A schemy Lisp backed by Haskell
GNU General Public License v2.0
60 stars 2 forks source link

Add sockets #5

Closed hellerve closed 8 years ago

hellerve commented 8 years ago

This Pull Request adds low-level networking primitives to zepto. The following endpoints are included:

(net:socket "stream") ; this will create a socket
(net:get-addr-info "google.com" "80") ; this will look up information regarding an address
(net:connect sock addr-info) ; this will connect a socket to an address
(net:send sock data) ; this will send data over the socket
(net:recv sock (make-small 1024)) ; this will receive n bytes from the socket, where n is the second argument [blocking]
(net:bind-socket socket addr-info) ; this will bind a socket to an address
(net:listen socket) ; this will put the socket into listening mode
(net:accept socket) ; this will accept a connection from a socket [blocking] and return a list of the form [socket address-info]
(net:close-socket socket) ; this will close the socket. All future calls to it will fail

Also included are a few primitives and functions to make working with sockets (and bytevectors) more pleasant. Those are:

(string->byte-vector str) ; this will convert a utf8-string to a byte-vector
(byte-vector->string bv) ; the inverse to the above
(ascii-string->byte-vector str) ; this will only work on ascii strings; otherwise truncates the utf-8 value
(byte-vector->ascii-string str) ; the inverse
(catch-vm-error [unsafe code block]) ; this will catch any error from the virtual machine and return it as a string
(->bytes anything) ; this will convert anything to its bit representation. Due to ambiguities, this should not be used as a marshalling function

Cheers