P2P: refactor for less indenting

This commit is contained in:
Vincent Bernardoff 2016-12-01 16:10:17 +01:00
parent 517893f707
commit 6a1265255f

View File

@ -218,11 +218,9 @@ module Make (P: PARAMS) = struct
(* send a message over a TCP socket *) (* send a message over a TCP socket *)
let send_msg ?crypt fd buf msg = let send_msg ?crypt fd buf msg =
Lwt.catch begin fun () -> Lwt.catch begin fun () ->
match Data_encoding.Binary.write msg_encoding msg buf hdrlen with match crypt, Data_encoding.Binary.write msg_encoding msg buf hdrlen with
| None -> Error_monad.fail Encoding_error | _, None -> Error_monad.fail Encoding_error
| Some len -> | None, Some len ->
match crypt with
| None ->
if len > maxlen then Error_monad.fail Message_too_big if len > maxlen then Error_monad.fail Message_too_big
else begin else begin
EndianBigstring.BigEndian.set_int16 buf 0 (len - hdrlen) ; EndianBigstring.BigEndian.set_int16 buf 0 (len - hdrlen) ;
@ -230,7 +228,7 @@ module Make (P: PARAMS) = struct
Lwt_utils.write_mbytes ~len fd buf >>= fun () -> Lwt_utils.write_mbytes ~len fd buf >>= fun () ->
Error_monad.return len Error_monad.return len
end end
| Some crypt -> | Some crypt, Some len ->
let encbuf = crypt (MBytes.sub buf hdrlen (len - hdrlen)) in let encbuf = crypt (MBytes.sub buf hdrlen (len - hdrlen)) in
let len = MBytes.length encbuf in let len = MBytes.length encbuf in
if len > maxlen then Error_monad.fail Message_too_big if len > maxlen then Error_monad.fail Message_too_big
@ -244,7 +242,6 @@ module Make (P: PARAMS) = struct
end end
(fun exn -> Lwt.return @@ Error_monad.error_exn exn) (fun exn -> Lwt.return @@ Error_monad.error_exn exn)
(* The (internal) type of network events, those dispatched from peer (* The (internal) type of network events, those dispatched from peer
workers to the net and others internal to net workers. *) workers to the net and others internal to net workers. *)
type event = type event =