let logger s = if !verbose then Printf.printf "%s\n" s in
Printf.ksprintf logger fmt
-type error = Unix.error * string * string
-
-type handle = Unix.file_descr
-
-type conn_callbacks =
-{
- accept_callback : handle -> Unix.file_descr -> Unix.sockaddr -> unit;
- connect_callback : handle -> unit;
- recv_callback : handle -> string -> (* offset *) int -> (* length *) int -> unit;
- send_done_callback : handle -> unit;
- closed_callback : handle -> unit;
- error_callback : handle -> error -> unit;
-}
-
-type conn_status =
- | Connecting
- | Listening
- | Connected
-
-type conn_state =
-{
- callbacks : conn_callbacks;
- mutable status : conn_status;
- mutable send_done_enabled : bool;
- mutable recv_enabled : bool;
-
- send_buf : Buffer.t;
-}
-
module ConnMap = Map.Make (struct type t = Unix.file_descr let compare = compare end)
(* A module that supports finding a timer by handle as well as by expiry time. *)
expiry_callback : unit -> unit
}
-type t =
+type error = Unix.error * string * string
+
+type handle = Unix.file_descr
+
+type conn_status =
+ | Connecting
+ | Listening
+ | Connected
+
+type conn_callbacks =
+{
+ accept_callback : t -> handle -> Unix.file_descr -> Unix.sockaddr -> unit;
+ connect_callback : t -> handle -> unit;
+ recv_callback : t -> handle -> string -> (* offset *) int -> (* length *) int -> unit;
+ send_done_callback : t -> handle -> unit;
+ closed_callback : t -> handle -> unit;
+ error_callback : t -> handle -> error -> unit;
+}
+
+and conn_state =
+{
+ callbacks : conn_callbacks;
+ mutable status : conn_status;
+ mutable send_done_enabled : bool;
+ mutable recv_enabled : bool;
+
+ send_buf : Buffer.t;
+}
+
+and t =
{
mutable conns: conn_state ConnMap.t;
mutable timers: timer_callbacks Timers.t;
try
Unix.connect handle addr;
conn_state.status <- Connected;
- conn_state.callbacks.connect_callback handle
+ conn_state.callbacks.connect_callback t handle
with
| Unix.Unix_error (Unix.EINPROGRESS, _, _) ->
Unixext.Fdset.set t.readers handle;
Unixext.Fdset.set t.writers handle
| Unix.Unix_error (ec, f, s) ->
- conn_state.callbacks.error_callback handle (ec, f, s)
+ conn_state.callbacks.error_callback t handle (ec, f, s)
let listen t handle =
let conn_state = ConnMap.find handle t.conns in
cs.status <- Connected;
if not cs.recv_enabled then
Unixext.Fdset.clear t.readers fd;
- cs.callbacks.connect_callback fd
+ cs.callbacks.connect_callback t fd
| Some err ->
- cs.callbacks.error_callback fd (err, "connect", "")
+ cs.callbacks.error_callback t fd (err, "connect", "")
)
| Listening ->
(try
let afd, aaddr = Unix.accept fd in
- cs.callbacks.accept_callback fd afd aaddr
+ cs.callbacks.accept_callback t fd afd aaddr
with
| Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
| Unix.Unix_error (Unix.ECONNABORTED, _, _)
try
let read_bytes = Unix.read fd buf 0 buflen in
if read_bytes = 0 then
- cs.callbacks.closed_callback fd
+ cs.callbacks.closed_callback t fd
else
- cs.callbacks.recv_callback fd buf 0 read_bytes
+ cs.callbacks.recv_callback t fd buf 0 read_bytes
with
| Unix.Unix_error (Unix.EWOULDBLOCK, _, _)
| Unix.Unix_error (Unix.EAGAIN, _, _)
Unixext.Fdset.set t.readers fd
else
Unixext.Fdset.clear t.readers fd;
- cs.callbacks.connect_callback fd
+ cs.callbacks.connect_callback t fd
| Some err ->
- cs.callbacks.error_callback fd (err, "connect", "")
+ cs.callbacks.error_callback t fd (err, "connect", "")
)
| Listening ->
(* This should never happen, since listening sockets
do_send fd cs;
if Buffer.length cs.send_buf = 0 then begin
if cs.send_done_enabled then
- cs.callbacks.send_done_callback fd;
+ cs.callbacks.send_done_callback t fd;
Unixext.Fdset.clear t.writers fd
end