8. CHAPTER
FIFTYNINE
USING RINGLIBUV
In this chapter we will learn about using RingLibuv
Note: To use RingLibuv, Check ring/extensions/ringlibuv folder.
Information from the library website: http://libuv.org/
Libuv is a multi-platform support library with a focus on asynchronous I/O.
Feature highlights
⢠Full-featured event loop backed by epoll, kqueue, IOCP, event ports.
⢠Asynchronous TCP and UDP sockets
⢠Asynchronous DNS resolution
⢠Asynchronous ďŹle and ďŹle system operations
⢠File system events
⢠ANSI escape code controlled TTY
⢠IPC with socket sharing, using Unix domain sockets or named pipes (Windows)
⢠Child processes
⢠Thread pool
⢠Signal handling
⢠High resolution clock
⢠Threading and synchronization primitives
59.1 First Application using RingLibuv
Example:
load "libuv.ring"
func main
myloop = new_uv_loop_t()
uv_loop_init(myloop)
? "Now quitting"
uv_run(myloop, UV_RUN_DEFAULT)
545
9. Ring Documentation, Release 1.10
uv_loop_close(myloop)
destroy_uv_loop_t(myloop)
Output:
Now quitting
59.2 The Events Loop
Example:
load "libuv.ring"
counter = 0
idler = NULL
func main
idler = new_uv_idle_t()
uv_idle_init(uv_default_loop(), idler)
uv_idle_start(idler, "wait()")
? "Idling..."
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
uv_loop_close(uv_default_loop());
destroy_uv_idle_t(idler)
func wait
counter++
if counter >= 100000
uv_idle_stop(idler)
ok
Output:
Idling...
59.3 Server Example
Example:
load "libuv.ring"
? "Testing RingLibuv - Server Side"
DEFAULT_PORT = 13370
DEFAULT_BACKLOG = 1024
addr = new_sockaddr_in()
server = NULL
client = NULL
myloop = NULL
func main
myloop = uv_default_loop()
server = new_uv_tcp_t()
59.2. The Events Loop 546
10. Ring Documentation, Release 1.10
uv_tcp_init(myloop, server)
uv_ip4_addr("127.0.0.1", DEFAULT_PORT, addr)
uv_tcp_bind(server, addr, 0)
r = uv_listen(server, DEFAULT_BACKLOG, "newconnection()")
if r
? "Listen error " + uv_strerror(r)
return 1
ok
uv_run(myloop, UV_RUN_DEFAULT)
destroy_uv_tcp_t(server)
destroy_uv_sockaddr_in(addr)
func newconnection
? "New Connection"
aPara = uv_Eventpara(server,:connect)
nStatus = aPara[2]
if nStatus < 0
? "New connection error : " + nStatus
return
ok
client = new_uv_tcp_t()
uv_tcp_init(myloop, client)
if uv_accept(server, client) = 0
uv_read_start(client, uv_myalloccallback(), "echo_read()")
ok
func echo_read
aPara = uv_Eventpara(client,:read)
nRead = aPara[2]
buf = aPara[3]
if nRead > 0
req = new_uv_write_t()
wrbuf = uv_buf_init(get_uv_buf_t_base(buf), nread)
uv_write(req, client, wrbuf, 1, "echo_write()")
? uv_buf2str(wrbuf)
message = "message from the server to the client"
buf = new_uv_buf_t()
set_uv_buf_t_len(buf,len(message))
set_uv_buf_t_base(buf,varptr("message","char *"))
uv_write(req, client, buf, 1, "echo_write()")
ok
func echo_write
aPara = uv_Eventpara(client,:read)
req = aPara[1]
Output:
When we run the client, We will see the message âNew Connectionâ
Then the message âhello from the clientâ
Testing RingLibuv - Server Side
New Connection
hello from the client
59.3. Server Example 547