timeout = %Time{tv_sec: (Sw) 300}
event_base = HTTP.Event.base_new()
socket = Socket.listen("0.0.0.0", "57000")
echo_client = fn (socket, events, client_ev, client) do
if List.has?(events, :read) && List.has(events, :write) do
Buf.refill(client.buf_rw.r)
str = Buf.read_to_str(client.buf_rw.r)
Buf.write_str(client.buf_rw.w, str)
end
end
acceptor = fn (server_socket, events, acceptor_ev, void) do
if List.has?(events, :read) do
client = Socket.Buf.accept(server_socket)
client_ev = HTTP.Event.new(event_base, client.sockfd, [:read, :write],
echo_client, client)
puts(client_ev)
HTTP.Event.base_set(event_base, client_ev)
HTTP.Event.add(client_ev, timeout)
end
end
puts(socket.fd)
acceptor_ev = HTTP.Event.new(event_base, socket.fd, [:read], acceptor,
void)
puts(HTTP.Event.base_set(event_base, acceptor_ev))
puts(HTTP.Event.add(acceptor_ev, timeout))
r = HTTP.Event.dispatch(event_base)
if r do
e = errno()
puts("HTTP.Event.dispatch: #{r}: #{strerror(e)}")
end