P2P: properly handle End_of_file.

This commit is contained in:
Grégoire Henry 2016-11-26 11:47:50 +01:00
parent c9fdaf13ec
commit 2a43eeaa7f
2 changed files with 8 additions and 6 deletions

View File

@ -203,7 +203,7 @@ module Make (P: PARAMS) = struct
| Some msg -> | Some msg ->
Lwt.return msg) Lwt.return msg)
(function (function
| Unix.Unix_error _ -> return Disconnect | Unix.Unix_error _ | End_of_file -> return Disconnect
| e -> fail e) | e -> fail e)
(* send a message over a TCP socket *) (* send a message over a TCP socket *)
@ -236,7 +236,7 @@ module Make (P: PARAMS) = struct
return true return true
end) end)
(function (function
| Unix.Unix_error _ -> return_false | Unix.Unix_error _ | End_of_file -> return_false
| e -> fail e) | e -> fail e)
(* A peer handle, as a record-encoded object, abstract from the (* A peer handle, as a record-encoded object, abstract from the

View File

@ -231,8 +231,9 @@ let rec read_bytes ?(pos = 0) ?len fd buf =
if len = 0 then if len = 0 then
Lwt.return_unit Lwt.return_unit
else else
Lwt_unix.read fd buf pos len >>= fun nb_read -> Lwt_unix.read fd buf pos len >>= function
inner (pos + nb_read) (len - nb_read) | 0 -> Lwt.fail End_of_file (* other endpoint cleanly closed its connection *)
| nb_read -> inner (pos + nb_read) (len - nb_read)
in in
inner pos len inner pos len
@ -254,7 +255,8 @@ let write_mbytes ?(pos=0) ?len descr buf =
if len = 0 then if len = 0 then
Lwt.return_unit Lwt.return_unit
else else
Lwt_bytes.write descr buf pos len >>= fun nb_written -> Lwt_bytes.write descr buf pos len >>= function
inner (pos + nb_written) (len - nb_written) in | 0 -> Lwt.fail End_of_file (* other endpoint cleanly closed its connection *)
| nb_written -> inner (pos + nb_written) (len - nb_written) in
inner pos len inner pos len