2008-02-27 22:50:27 -07:00
|
|
|
let dbg msg a =
|
2008-02-24 21:41:20 -07:00
|
|
|
prerr_endline msg;
|
|
|
|
a
|
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
(** Establish a server on the given address.
|
2008-02-24 21:41:20 -07:00
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
[connection_handler] will be called with the file descriptor of
|
|
|
|
any new connections.
|
|
|
|
*)
|
|
|
|
let establish_server ues connection_handler addr =
|
|
|
|
let g = Unixqueue.new_group ues in
|
|
|
|
let handle_event ues esys e =
|
2008-02-24 21:41:20 -07:00
|
|
|
match e with
|
|
|
|
| Unixqueue.Input_arrived (g, fd) ->
|
2008-02-27 22:50:27 -07:00
|
|
|
let cli_fd, cli_addr = Unix.accept fd in
|
|
|
|
connection_handler cli_fd
|
|
|
|
| _ ->
|
|
|
|
raise Equeue.Reject
|
|
|
|
in
|
|
|
|
let srv = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
|
|
|
|
Unix.bind srv addr;
|
|
|
|
Unix.listen srv 50;
|
|
|
|
Unix.setsockopt srv Unix.SO_REUSEADDR true;
|
|
|
|
Unixqueue.add_handler ues g handle_event;
|
|
|
|
Unixqueue.add_resource ues g (Unixqueue.Wait_in srv, -.1.0)
|
2008-02-08 15:38:31 -07:00
|
|
|
|
|
|
|
let main () =
|
2008-02-24 21:41:20 -07:00
|
|
|
let ues = Unixqueue.create_unix_event_system () in
|
|
|
|
let g = Unixqueue.new_group ues in
|
2008-02-08 15:38:31 -07:00
|
|
|
let handle_connection fd =
|
2008-02-28 22:00:24 -07:00
|
|
|
ignore (Client.create ues g fd);
|
|
|
|
Unixqueue.add_resource ues g (Unixqueue.Wait_in fd, -.1.0)
|
2008-02-08 15:38:31 -07:00
|
|
|
in
|
2008-02-28 22:00:24 -07:00
|
|
|
Unixqueue.add_handler ues g Client.handle_event;
|
2008-02-27 22:50:27 -07:00
|
|
|
establish_server
|
2008-02-24 21:41:20 -07:00
|
|
|
ues
|
2008-02-08 15:38:31 -07:00
|
|
|
handle_connection
|
|
|
|
(Unix.ADDR_INET (Unix.inet_addr_any, 7777));
|
|
|
|
ues#run ()
|
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
let _ =
|
|
|
|
main ()
|