Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/lwt_unix_conduit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@ ELSE
| `TCP -> LUN.Tcp_client.connect sa
END

let serve ~mode ~sockaddr ?timeout callback =
let serve ~mode ~sockaddr ?stop ?timeout callback =
IFDEF HAVE_LWT_SSL THEN
match mode with
| `TCP -> LUN.Tcp_server.init ~sockaddr ?timeout callback
| `TCP -> LUN.Tcp_server.init ~sockaddr ?stop ?timeout callback
| `SSL (`Crt_file_path certfile, `Key_file_path keyfile) ->
Lwt_unix_net_ssl.Server.init ~certfile ~keyfile ?timeout sockaddr callback
Lwt_unix_net_ssl.Server.init ~certfile ~keyfile
?stop ?timeout sockaddr callback
ELSE
match mode with
| `TCP -> LUN.Tcp_server.init ~sockaddr ?timeout callback
| `TCP -> LUN.Tcp_server.init ~sockaddr ?stop ?timeout callback
| `SSL (`Crt_file_path certfile, `Key_file_path keyfile) ->
fail (Failure "No SSL support compiled into Conduit")
END
Expand Down
1 change: 1 addition & 0 deletions lib/lwt_unix_conduit.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ val connect :
val serve :
mode:server_mode ->
sockaddr:Lwt_unix.sockaddr ->
?stop:(unit Lwt.t) ->
?timeout:int -> (ic -> oc -> unit io) -> unit io

val close_in : 'a Lwt_io.channel -> unit
Expand Down
11 changes: 7 additions & 4 deletions lib/lwt_unix_net.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@ module Tcp_server = struct
Lwt_unix.setsockopt client Lwt_unix.TCP_NODELAY true;
let ic = Lwt_io.of_fd ~mode:Lwt_io.input client in
let oc = Lwt_io.of_fd ~mode:Lwt_io.output client in

let c = callback ic oc in
let events = match timeout with
|None -> [c]
|Some t -> [c; (Lwt_unix.sleep (float_of_int t)) ] in
let _ = Lwt.pick events >>= fun () -> close (ic,oc) in
return ()

let init ~sockaddr ?timeout callback =

let init ~sockaddr ?(stop = fst (Lwt.wait ())) ?timeout callback =
let cont = ref true in
let s = init_socket sockaddr in
while_lwt true do

Lwt.async (fun () -> stop >>= fun () -> cont := false; Lwt.return ());
while_lwt !cont do
Lwt_unix.accept s >>=
process_accept ?timeout callback
done
Expand Down
1 change: 1 addition & 0 deletions lib/lwt_unix_net.mli
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Tcp_server : sig

val init :
sockaddr:Lwt_unix.sockaddr ->
?stop:(unit Lwt.t) ->
?timeout:int ->
(input channel -> output channel -> unit Lwt.t) ->
unit Lwt.t
Expand Down
7 changes: 5 additions & 2 deletions lib/lwt_unix_net_ssl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ module Server = struct
let _ = Lwt.pick events >>= fun () -> close (ic,oc) in
return ()

let init ?(nconn=20) ?password ~certfile ~keyfile ?timeout sa callback =
let init ?(nconn=20) ?password ~certfile ~keyfile
?(stop = fst (Lwt.wait ())) ?timeout sa callback =
let s = listen ~nconn ?password ~certfile ~keyfile sa in
let cont = ref true in

Lwt.async (fun () -> stop >>= fun () -> cont := false; Lwt.return ());
while_lwt !cont do
try_lwt begin
accept s >>= process_accept ~timeout callback
end with
end with
| Lwt.Canceled -> cont := false; return ()
| _ -> return ()
done
Expand Down
1 change: 1 addition & 0 deletions lib/lwt_unix_net_ssl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Server : sig
?password:(bool -> string) ->
certfile:string ->
keyfile:string ->
?stop:(unit Lwt.t) ->
?timeout:int ->
Lwt_unix.sockaddr ->
(Lwt_io.input_channel -> Lwt_io.output_channel -> unit Lwt.t) ->
Expand Down