Skip to content

Commit

Permalink
Deprecate current send_msg, recv_msg; test new
Browse files Browse the repository at this point in the history
  • Loading branch information
aantron committed Aug 13, 2019
1 parent 6e0013b commit 3e5923b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/unix/lwt_bytes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ type io_vector = {
val io_vector : buffer : t -> offset : int -> length : int -> io_vector

val recv_msg : socket : Lwt_unix.file_descr -> io_vectors : io_vector list -> (int * Unix.file_descr list) Lwt.t
(** Not implemented on Windows. *)
[@@ocaml.deprecated " Use Lwt_unix.Versioned.recv_msg_2."]
(** Not implemented on Windows.
@deprecated Use {!Lwt_unix.Versioned.recv_msg_2}. *)

val send_msg : socket : Lwt_unix.file_descr -> io_vectors : io_vector list -> fds : Unix.file_descr list -> int Lwt.t
(** Not implemented on Windows. *)
[@@ocaml.deprecated " Use Lwt_unix.Versioned.send_msg_2."]
(** Not implemented on Windows.
@deprecated Use {!Lwt_unix.Versioned.send_msg_2}. *)

(** {2 Memory mapped files} *)

Expand Down
16 changes: 14 additions & 2 deletions src/unix/lwt_unix.cppo.mli
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,9 @@ val io_vector : buffer : string -> offset : int -> length : int -> io_vector
(** Creates an io-vector *)

val recv_msg : socket : file_descr -> io_vectors : io_vector list -> (int * Unix.file_descr list) Lwt.t
[@@ocaml.deprecated
" Will be replaced by Lwt_unix.Versioned.recv_msg_2 in Lwt >= 5.0.0. See
https://github.com/ocsigen/lwt/issues/594"]
(** [recv_msg ~socket ~io_vectors] receives data into a list of
io-vectors, plus any file-descriptors that may accompany the
messages. It returns a tuple whose first field is the number of
Expand All @@ -936,17 +939,26 @@ val recv_msg : socket : file_descr -> io_vectors : io_vector list -> (int * Unix
provided [io_vectors] list. Data is written directly into the
[iov_buffer] buffers.
Not implemented on Windows. *)
Not implemented on Windows.
@deprecated Will be replaced by {!Lwt_unix.Versioned.recv_msg_2} in Lwt
5.0.0. *)

val send_msg : socket : file_descr -> io_vectors : io_vector list -> fds : Unix.file_descr list -> int Lwt.t
[@@ocaml.deprecated
" Will be replaced by Lwt_unix.Versioned.send_msg_2 in Lwt >= 5.0.0. See
https://github.com/ocsigen/lwt/issues/594"]
(** [send_msg ~socket ~io_vectors ~fds] sends data from a list of
io-vectors, accompanied with a list of file-descriptors. It
returns the number of bytes sent. If fd-passing is not possible on
the current system and [fds] is not empty, it raises
[Lwt_sys.Not_available "fd_passing"]. Data is written directly from
the [iov_buffer] buffers.
Not implemented on Windows. *)
Not implemented on Windows.
@deprecated Will be replaced by {!Lwt_unix.Versioned.send_msg_2} in Lwt
5.0.0. *)

type credentials = {
cred_pid : int;
Expand Down
6 changes: 3 additions & 3 deletions test/unix/test_lwt_bytes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ let suite = suite "lwt_bytes" [
let offset = 0 in
let io_vectors = [Lwt_bytes.io_vector ~buffer ~offset ~length:6] in
let server_logic socket =
Lwt_bytes.recv_msg ~socket ~io_vectors
(Lwt_bytes.recv_msg [@ocaml.warning "-3"]) ~socket ~io_vectors
in
let client_logic socket sockaddr =
let message = Lwt_bytes.of_string "abcdefghij" in
Expand All @@ -702,14 +702,14 @@ let suite = suite "lwt_bytes" [
let offset = 0 in
let server_logic socket =
let io_vectors = [Lwt_bytes.io_vector ~buffer ~offset ~length:6] in
Lwt_bytes.recv_msg ~socket ~io_vectors
(Lwt_bytes.recv_msg [@ocaml.warning "-3"]) ~socket ~io_vectors
in
let client_logic socket sockaddr =
Lwt_unix.connect socket sockaddr
>>= fun () ->
let message = Lwt_bytes.of_string "abcdefghij" in
let io_vectors = [Lwt_bytes.io_vector ~buffer:message ~offset ~length:9] in
Lwt_bytes.send_msg ~socket ~io_vectors ~fds:[]
(Lwt_bytes.send_msg [@ocaml.warning "-3"]) ~socket ~io_vectors ~fds:[]
in
udp_server_client_exchange server_logic client_logic
>>= fun () ->
Expand Down
68 changes: 63 additions & 5 deletions test/unix/test_lwt_unix.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,64 @@ let send_recv_msg_tests = [
let socket_1, socket_2 = Lwt_unix.(socketpair PF_UNIX SOCK_STREAM 0) in
let pipe_read, pipe_write = Lwt_unix.pipe () in

let source_buffer = Bytes.of_string "_foo_bar_" in
let source_iovecs = Lwt_unix.IO_vectors.create () in
Lwt_unix.IO_vectors.append_bytes source_iovecs source_buffer 1 3;
Lwt_unix.IO_vectors.append_bytes source_iovecs source_buffer 5 3;

Lwt_unix.Versioned.send_msg_2
~socket:socket_1
~io_vectors:source_iovecs
~fds:[Lwt_unix.unix_file_descr pipe_write] >>= fun n ->
if n <> 6 then
Lwt.return false

else
let destination_buffer = Bytes.of_string "_________" in
let destination_iovecs = Lwt_unix.IO_vectors.create () in
Lwt_unix.IO_vectors.append_bytes
destination_iovecs destination_buffer 5 3;
Lwt_unix.IO_vectors.append_bytes
destination_iovecs destination_buffer 1 3;

Lwt_unix.Versioned.recv_msg_2
~socket:socket_2 ~io_vectors:destination_iovecs >>= fun (n, fds) ->
let succeeded =
match n, fds, Bytes.to_string destination_buffer with
| 6, [fd], "_bar_foo_" -> Some fd
| _ -> None
in
match succeeded with
| None ->
Lwt.return false
| Some fd ->

let n = Unix.write fd (Bytes.of_string "baz") 0 3 in
if n <> 3 then
Lwt.return false

else
let buffer = Bytes.create 3 in
Lwt_unix.read pipe_read buffer 0 3 >>= fun n ->
match n, Bytes.to_string buffer with
| 3, "baz" ->
Lwt_unix.close socket_1 >>= fun () ->
Lwt_unix.close socket_2 >>= fun () ->
Lwt_unix.close pipe_read >>= fun () ->
Lwt_unix.close pipe_write >>= fun () ->
Unix.close fd;
Lwt.return true

| _ ->
Lwt.return false
end;

test "send_msg, recv_msg (old)" ~only_if:(fun () -> not Sys.win32)
begin fun () ->

let socket_1, socket_2 = Lwt_unix.(socketpair PF_UNIX SOCK_STREAM 0) in
let pipe_read, pipe_write = Lwt_unix.pipe () in

let source_buffer = "_foo_bar_" in
let source_iovecs = Lwt_unix.[
{
Expand All @@ -610,7 +668,7 @@ let send_recv_msg_tests = [
]
in

Lwt_unix.send_msg
(Lwt_unix.send_msg [@ocaml.warning "-3"])
~socket:socket_1
~io_vectors:source_iovecs
~fds:[Lwt_unix.unix_file_descr pipe_write] >>= fun n ->
Expand All @@ -633,7 +691,7 @@ let send_recv_msg_tests = [
]
in

Lwt_unix.recv_msg
(Lwt_unix.recv_msg [@ocaml.warning "-3"])
~socket:socket_2 ~io_vectors:destination_iovecs >>= fun (n, fds) ->
let succeeded =
match n, fds, destination_buffer with
Expand Down Expand Up @@ -665,7 +723,7 @@ let send_recv_msg_tests = [
Lwt.return false
end;

test "send_msg, recv_msg (Lwt_bytes)" ~only_if:(fun () -> not Sys.win32)
test "send_msg, recv_msg (Lwt_bytes, old)" ~only_if:(fun () -> not Sys.win32)
begin fun () ->

let socket_1, socket_2 = Lwt_unix.(socketpair PF_UNIX SOCK_STREAM 0) in
Expand All @@ -686,7 +744,7 @@ let send_recv_msg_tests = [
]
in

Lwt_bytes.send_msg
(Lwt_bytes.send_msg [@ocaml.warning "-3"])
~socket:socket_1
~io_vectors:source_iovecs
~fds:[Lwt_unix.unix_file_descr pipe_write] >>= fun n ->
Expand All @@ -709,7 +767,7 @@ let send_recv_msg_tests = [
]
in

Lwt_bytes.recv_msg
(Lwt_bytes.recv_msg [@ocaml.warning "-3"])
~socket:socket_2 ~io_vectors:destination_iovecs >>= fun (n, fds) ->
let succeeded =
match n, fds, Lwt_bytes.to_string destination_buffer with
Expand Down

0 comments on commit 3e5923b

Please sign in to comment.