irc-bot/ircd.ml

41 lines
986 B
OCaml
Raw Normal View History

let dbg msg a =
2008-02-24 21:41:20 -07:00
prerr_endline msg;
a
(** Establish a server on the given address.
2008-02-24 21:41:20 -07:00
[connection_handler] will be called with the file descriptor of
any new connections.
*)
let establish_server d connection_handler addr =
let rec handle_event fd events =
match events with
| [] ->
()
| Dispatch.Input :: tl ->
let cli_fd, cli_addr = Unix.accept fd in
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
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-05-09 08:01:11 -06:00
Dispatch.add d fd handle_event [Dispatch.Input]
let main () =
let d = Dispatch.create 50 in
establish_server
2008-02-24 21:41:20 -07:00
ues
(Client.handle_connection d)
(Unix.ADDR_INET (Unix.inet_addr_any, 6667));
Dispatch.run d
let _ =
main ()