Branch
Hash :
cf801550
Author :
Thomas de Grivel
Date :
2024-08-14T13:20:56
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
timeout = %Time{tv_sec: (Sw) 30}
event_base = HTTP.Event.base_new()
socket = Socket.listen("0.0.0.0", "57000")
echo_client = fn (socket, events, client_ev, client) do
puts("echo_client")
if List.has?(events, :read) do
s = Buf.read_until_str_into_str(client.buf_rw.r, "\n")
puts("read: #{s}")
if (s == "") do
puts("closing")
HTTP.Event.del(client_ev)
Socket.close(client.sockfd)
exit(0)
end
Buf.write_str(client.buf_rw.w, s)
end
end
acceptor = fn (server_socket, events, acceptor_ev, void) do
if List.has?(events, :read) do
puts("acceptor: read")
client = Socket.Buf.accept(%Socket{fd: server_socket})
puts("acceptor: got client")
client_ev = HTTP.Event.new(event_base, client.sockfd, [:read, :persist],
echo_client, client)
r = HTTP.Event.add(client_ev, timeout)
puts("HTTP.Event.add: #{r}")
end
end
puts("socket.fd: #{socket.fd}")
puts("event_base: #{event_base}")
acceptor_ev = HTTP.Event.new(event_base, socket.fd, [:read, :persist],
acceptor, void)
puts("acceptor_ev: #{acceptor_ev}")
r = HTTP.Event.add(acceptor_ev, timeout)
puts("HTTP.Event.add: #{r}")
r = HTTP.Event.dispatch(event_base)
if r do
e = errno()
puts("HTTP.Event.dispatch: #{r}: #{strerror(e)}")
end