Permalink
Browse files

Merge branch 'complete' of git://github.com/djs55/ocaml-cstruct into …

…djs55-complete

Conflicts:
	CHANGES
  • Loading branch information...
2 parents f72f693 + 8dfe248 commit 716855f6c13af02e1e1dd3c471792a71c453071f @avsm committed Jul 5, 2014
Showing with 38 additions and 2 deletions.
  1. +1 −0 CHANGES
  2. +12 −0 lwt/lwt_cstruct.ml
  3. +22 −2 lwt/lwt_cstruct.mli
  4. +3 −0 unix/unix_cstruct.mli
View
@@ -2,6 +2,7 @@
* Add bounds checks for `Cstruct.BE/LE` functions that violate a view.
Previously, only bounds errors on the underlying buffers would raise.
Bug #25, reported by Mindy Preston in mirage/mirage-tcpip#56.
+* Add 'Lwt_cstruct.complete' convenience function
1.2.0 (2014-06-06):
View
@@ -20,6 +20,18 @@ let read fd t =
let write fd t =
Lwt_bytes.write fd t.Cstruct.buffer t.Cstruct.off t.Cstruct.len
+let complete op t =
+ let open Lwt in
+ let rec loop t =
+ op t >>= fun n ->
+ let t = Cstruct.shift t n in
+ if Cstruct.len t = 0
+ then return ()
+ else if n = 0
+ then fail End_of_file
+ else loop t in
+ loop t
+
let sendto fd t flags dst =
Lwt_bytes.sendto fd t.Cstruct.buffer t.Cstruct.off t.Cstruct.len flags dst
View
@@ -1,6 +1,6 @@
(*
- * Copyright (c) 2012 Anil Madhavapeddy <anil@recoil.org>
- * Copyright (c) 2012 Citrix Systems Inc
+ * Copyright (c) 2012-2014 Anil Madhavapeddy <anil@recoil.org>
+ * Copyright (c) 2012-2014 Citrix Systems Inc
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -15,10 +15,30 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
+(** Blocking Lwt functions to read and write from Cstruct buffers. *)
+
val read: Lwt_unix.file_descr -> Cstruct.t -> int Lwt.t
+(** [read fd t] reads data from the file descriptor [fd] into the
+ [t] cstruct.
+ @return the numbers of bytes actually read. *)
val write: Lwt_unix.file_descr -> Cstruct.t -> int Lwt.t
+(** [write fd t] writes data from the [t] cstruct to the file
+ descriptor [fd].
+ @return the numbers of bytes actually written. *)
+
+val complete: (Cstruct.t -> int Lwt.t) -> Cstruct.t -> unit Lwt.t
+(** [complete (read fd) t] fills [t] with data from [fd].
+
+ [complete (write fd) t] fully-writes [t] to [fd].
+
+ @raise End_of_file if the file descriptor operation fails.
+ *)
val sendto: Lwt_unix.file_descr -> Cstruct.t -> Unix.msg_flag list -> Unix.sockaddr -> int Lwt.t
+(** [sendto fd t flags sa] invokes {!Lwt_unix.sendto} on the [t] cstruct.
+ @return the number of bytes sent. *)
val recvfrom: Lwt_unix.file_descr -> Cstruct.t -> Unix.msg_flag list -> (int * Unix.sockaddr) Lwt.t
+(** [recvfrom fd t flags sa] invokes {!Lwt_unix.recvfrom} on the [t] cstruct.
+ @return the number of bytes read and the socket address of the remote side. *)
View
@@ -14,4 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
+(** Unix functions that operate on Cstruct buffers. *)
+
val of_fd : Unix.file_descr -> Cstruct.t
+(** [of_fd fd] memory maps the [fd] and returns a cstruct *)

0 comments on commit 716855f

Please sign in to comment.