Skip to content
Snippets Groups Projects
Commit 07750492 authored by David Bühler's avatar David Bühler
Browse files

[server] Improves the stopping condition of functions [send_bytes].

parent 357fb3b1
No related branches found
No related tags found
No related merge requests found
...@@ -67,43 +67,41 @@ type channel = { ...@@ -67,43 +67,41 @@ type channel = {
} }
let read_bytes { sock ; rcv ; brcv } = let read_bytes { sock ; rcv ; brcv } =
begin (* rcv buffer is only used locally *)
(* rcv buffer is only used locally *) let s = Bytes.length rcv in
let s = Bytes.length rcv in let rec scan p =
let rec scan p = (* try to fill RCV buffer *)
(* try to fill RCV buffer *) let n =
if p < s then try Unix.read sock rcv p (s-p)
let n = with Unix.Unix_error((EAGAIN|EWOULDBLOCK),_,_) -> 0
try Unix.read sock rcv p (s-p)
with Unix.Unix_error((EAGAIN|EWOULDBLOCK),_,_) -> 0
in if n > 0 then scan (p+n) else p
else p
in in
let n = scan 0 in let p = p + n in
if n > 0 then Buffer.add_subbytes brcv rcv 0 n if n > 0 && p < s then scan p else p
end in
let n = scan 0 in
if n > 0 then Buffer.add_subbytes brcv rcv 0 n
let send_bytes { sock ; snd ; bsnd } = let send_bytes { sock ; snd ; bsnd } =
begin (* snd buffer is only used locally *)
(* snd buffer is only used locally *) let n = Buffer.length bsnd in
let n = Buffer.length bsnd in if n > 0 then
if n > 0 then let s = Bytes.length snd in
let s = Bytes.length snd in let rec send p =
let rec send p = (* try to flush BSND buffer *)
(* try to flush BSND buffer *) let w = min (n-p) s in
let w = min (n-p) s in Buffer.blit bsnd p snd 0 w ;
Buffer.blit bsnd p snd 0 w ; let r =
let r = try Unix.single_write sock snd 0 w
try Unix.single_write sock snd 0 w with Unix.Unix_error((EAGAIN|EWOULDBLOCK),_,_) -> 0
with Unix.Unix_error((EAGAIN|EWOULDBLOCK),_,_) -> 0
in if r > 0 then send (p+r) else p
in in
let p = send 0 in let p = p + r in
if p > 0 then if r > 0 && p < n then send p else p
let tail = Buffer.sub bsnd p (n-p) in in
Buffer.reset bsnd ; let p = send 0 in
Buffer.add_string bsnd tail if p > 0 then
end let tail = Buffer.sub bsnd p (n-p) in
Buffer.reset bsnd ;
Buffer.add_string bsnd tail
(* -------------------------------------------------------------------------- *) (* -------------------------------------------------------------------------- *)
(* --- Data Chunks Encoding --- *) (* --- Data Chunks Encoding --- *)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment