P2P: refactor for less indenting
This commit is contained in:
parent
517893f707
commit
6a1265255f
@ -218,33 +218,30 @@ 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
|
if len > maxlen then Error_monad.fail Message_too_big
|
||||||
| None ->
|
else begin
|
||||||
if len > maxlen then Error_monad.fail Message_too_big
|
EndianBigstring.BigEndian.set_int16 buf 0 (len - hdrlen) ;
|
||||||
else begin
|
(* TODO timeout write ??? *)
|
||||||
EndianBigstring.BigEndian.set_int16 buf 0 (len - hdrlen) ;
|
Lwt_utils.write_mbytes ~len fd buf >>= fun () ->
|
||||||
(* TODO timeout write ??? *)
|
Error_monad.return len
|
||||||
Lwt_utils.write_mbytes ~len fd buf >>= fun () ->
|
end
|
||||||
Error_monad.return len
|
| Some crypt, Some len ->
|
||||||
end
|
let encbuf = crypt (MBytes.sub buf hdrlen (len - hdrlen)) in
|
||||||
| Some crypt ->
|
let len = MBytes.length encbuf in
|
||||||
let encbuf = crypt (MBytes.sub buf hdrlen (len - hdrlen)) in
|
if len > maxlen then Error_monad.fail Message_too_big
|
||||||
let len = MBytes.length encbuf in
|
else begin
|
||||||
if len > maxlen then Error_monad.fail Message_too_big
|
let lenbuf = MBytes.create 2 in
|
||||||
else begin
|
EndianBigstring.BigEndian.set_int16 lenbuf 0 len ;
|
||||||
let lenbuf = MBytes.create 2 in
|
Lwt_utils.write_mbytes fd lenbuf >>= fun () ->
|
||||||
EndianBigstring.BigEndian.set_int16 lenbuf 0 len ;
|
Lwt_utils.write_mbytes fd encbuf >>= fun () ->
|
||||||
Lwt_utils.write_mbytes fd lenbuf >>= fun () ->
|
Error_monad.return len
|
||||||
Lwt_utils.write_mbytes fd encbuf >>= fun () ->
|
end
|
||||||
Error_monad.return len
|
|
||||||
end
|
|
||||||
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 =
|
||||||
|
Loading…
Reference in New Issue
Block a user