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.
|
|
|
|
*)
|
2008-03-18 15:27:03 -06:00
|
|
|
let establish_server d connection_handler addr =
|
|
|
|
let rec handle_event fd events =
|
|
|
|
match events with
|
|
|
|
| [] ->
|
|
|
|
()
|
|
|
|
| Dispatch.Input :: tl ->
|
2008-02-27 22:50:27 -07:00
|
|
|
let cli_fd, cli_addr = Unix.accept fd in
|
2008-03-18 15:27:03 -06:00
|
|
|
connection_handler cli_fd cli_addr;
|
|
|
|
handle_event fd tl
|
|
|
|
| Dispatch.Hangup :: tl ->
|
|
|
|
Dispatch.delete d fd;
|
|
|
|
handle_event fd tl
|
|
|
|
| _ :: tl ->
|
|
|
|
handle_event fd tl
|
2008-02-27 22:50:27 -07:00
|
|
|
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;
|
2008-03-18 15:27:03 -06:00
|
|
|
Dispatch.add d fd handle_event [Dispatch.Input];
|
2008-02-08 15:38:31 -07:00
|
|
|
|
|
|
|
let main () =
|
2008-03-18 15:27:03 -06:00
|
|
|
let d = Dispatch.create 50 in
|
2008-02-27 22:50:27 -07:00
|
|
|
establish_server
|
2008-02-24 21:41:20 -07:00
|
|
|
ues
|
2008-03-18 15:27:03 -06:00
|
|
|
(Client.handle_connection d)
|
|
|
|
(Unix.ADDR_INET (Unix.inet_addr_any, 6667));
|
|
|
|
Dispatch.run d
|
2008-02-08 15:38:31 -07:00
|
|
|
|
2008-02-27 22:50:27 -07:00
|
|
|
let _ =
|
|
|
|
main ()
|