Skip to content

Commit 4c5cd89

Browse files
committed
Add stop parameters in main-loop of server
1 parent 0ae41d4 commit 4c5cd89

File tree

6 files changed

+16
-11
lines changed

6 files changed

+16
-11
lines changed

lib/lwt_unix_conduit.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ ELSE
4141
| `TCP -> LUN.Tcp_client.connect sa
4242
END
4343

44-
let serve ~mode ~sockaddr ?timeout callback =
44+
let serve ~mode ~sockaddr ?stop ?timeout callback =
4545
IFDEF HAVE_LWT_SSL THEN
4646
match mode with
47-
| `TCP -> LUN.Tcp_server.init ~sockaddr ?timeout callback
47+
| `TCP -> LUN.Tcp_server.init ~sockaddr ?stop ?timeout callback
4848
| `SSL (`Crt_file_path certfile, `Key_file_path keyfile) ->
49-
Lwt_unix_net_ssl.Server.init ~certfile ~keyfile ?timeout sockaddr callback
49+
Lwt_unix_net_ssl.Server.init ~certfile ~keyfile
50+
?stop ?timeout sockaddr callback
5051
ELSE
5152
match mode with
52-
| `TCP -> LUN.Tcp_server.init ~sockaddr ?timeout callback
53+
| `TCP -> LUN.Tcp_server.init ~sockaddr ?stop ?timeout callback
5354
| `SSL (`Crt_file_path certfile, `Key_file_path keyfile) ->
5455
fail (Failure "No SSL support compiled into Conduit")
5556
END

lib/lwt_unix_conduit.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ val connect :
3131
val serve :
3232
mode:server_mode ->
3333
sockaddr:Lwt_unix.sockaddr ->
34+
?stop:(unit -> bool) ->
3435
?timeout:int -> (ic -> oc -> unit io) -> unit io
3536

3637
val close_in : 'a Lwt_io.channel -> unit

lib/lwt_unix_net.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ module Tcp_server = struct
5858
Lwt_unix.setsockopt client Lwt_unix.TCP_NODELAY true;
5959
let ic = Lwt_io.of_fd ~mode:Lwt_io.input client in
6060
let oc = Lwt_io.of_fd ~mode:Lwt_io.output client in
61-
61+
6262
let c = callback ic oc in
6363
let events = match timeout with
6464
|None -> [c]
6565
|Some t -> [c; (Lwt_unix.sleep (float_of_int t)) ] in
6666
let _ = Lwt.pick events >>= fun () -> close (ic,oc) in
6767
return ()
68-
69-
let init ~sockaddr ?timeout callback =
68+
69+
let init ~sockaddr ?(stop = (fun () -> true)) ?timeout callback =
7070
let s = init_socket sockaddr in
71-
while_lwt true do
71+
while_lwt (stop ()) do
7272
Lwt_unix.accept s >>=
7373
process_accept ?timeout callback
7474
done

lib/lwt_unix_net.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Tcp_server : sig
3232

3333
val init :
3434
sockaddr:Lwt_unix.sockaddr ->
35+
?stop:(unit -> bool) ->
3536
?timeout:int ->
3637
(input channel -> output channel -> unit Lwt.t) ->
3738
unit Lwt.t

lib/lwt_unix_net_ssl.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ module Server = struct
6767
let _ = Lwt.pick events >>= fun () -> close (ic,oc) in
6868
return ()
6969

70-
let init ?(nconn=20) ?password ~certfile ~keyfile ?timeout sa callback =
70+
let init ?(nconn=20) ?password ~certfile ~keyfile
71+
?(stop = (fun () -> true)) ?timeout sa callback =
7172
let s = listen ~nconn ?password ~certfile ~keyfile sa in
7273
let cont = ref true in
73-
while_lwt !cont do
74+
while_lwt !cont && (stop ()) do
7475
try_lwt begin
7576
accept s >>= process_accept ~timeout callback
76-
end with
77+
end with
7778
| Lwt.Canceled -> cont := false; return ()
7879
| _ -> return ()
7980
done

lib/lwt_unix_net_ssl.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Server : sig
3838
?password:(bool -> string) ->
3939
certfile:string ->
4040
keyfile:string ->
41+
?stop:(unit -> bool) ->
4142
?timeout:int ->
4243
Lwt_unix.sockaddr ->
4344
(Lwt_io.input_channel -> Lwt_io.output_channel -> unit Lwt.t) ->

0 commit comments

Comments
 (0)