diff -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/daemon/common/commonGlobals.ml ./src/daemon/common/commonGlobals.ml
--- ./src/daemon/common/commonGlobals.ml	2005-09-01 18:50:59.000000000 +0200
+++ ./src/daemon/common/commonGlobals.ml	2005-09-05 13:44:13.000000000 +0200
@@ -213,11 +213,18 @@
   "Download"
     (!!max_hard_download_rate * 2048) (* TODO: changed from 1024 to 2048 because of bug *)
 
+let payload_bandwidth = ref 0.
 
 let _ =
   option_hook max_hard_upload_rate (fun _ ->
       TcpBufferedSocket.change_rate upload_control
-        (!!max_hard_upload_rate * 1024));
+        (!!max_hard_upload_rate * 1024);
+      payload_bandwidth :=
+	float_of_int (if !!max_hard_upload_rate = 0 then 
+	   10000 * 1024
+         else 
+	   maxi (!!max_hard_upload_rate * 1024) 1024) *. 0.90;
+  );
   option_hook max_hard_download_rate (fun _ ->
       let rate = !!max_hard_download_rate in
       TcpBufferedSocket.change_rate download_control
diff -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/daemon/common/commonUploads.ml ./src/daemon/common/commonUploads.ml
--- ./src/daemon/common/commonUploads.ml	2005-08-26 23:21:20.000000000 +0200
+++ ./src/daemon/common/commonUploads.ml	2005-09-05 13:43:27.000000000 +0200
@@ -729,112 +729,72 @@
 let (pending_slots_map : client Intmap.t ref) = ref Intmap.empty
 (* let (pending_slots_fifo : int Fifo.t)  = Fifo.create () *)
 
-let remaining_bandwidth = ref 0
-let total_bandwidth = ref 0
-let complete_bandwidth = ref 0
-let counter = ref 1
-let sent_bytes = Array.create 10 0
+let packet_size = 10240
+
+let streaming_amount = packet_size * 2
+let streaming_left = ref streaming_amount
+let streaming_time = (ref None : float option ref)
+
 let has_upload = ref 0
 let upload_credit = ref 0
 
 
 let can_write_len sock len =
   let bool1 = can_write_len sock len in
-  let upload_rate =
-        (if !!max_hard_upload_rate = 0 then 10000 else !!max_hard_upload_rate)
-    * 1024 in
-  let bool2 =
-    (
-(*      lprintf "upload_rate %d -> %d\n" upload_rate
-        (upload_rate * (Fifo.length upload_clients)); *)
-(* changed 2.5.24
-Don't put in a socket more than 10 seconds of upload.
-  *)
-    not_buffer_more sock (upload_rate * 10  (* * (Fifo.length upload_clients) *) ))
-  in
+(* changed 2.5.24: Don't put in a socket more than 10 seconds of upload. *)
+(* ...since the socket shares available bandwidth with others, 
+   that test has no physical meaning. Should it simply be removed ? *)
+  let bool2 = not_buffer_more sock (int_of_float !CommonGlobals.payload_bandwidth * 10) in
 (*  lprintf "can_write_len %b %b\n" bool1 bool2; *)
   let b = bool1 && bool2 in
 (*  if not b then
-lprintf "bool1 %b len %d bool2 %b upload %d\n" bool1 len bool2 upload_rate;
-  *)
+      lprintf "bool1 %b len %d bool2 %b upload %d\n" 
+        bool1 len bool2 upload_rate; *)
   b
 
-let upload_to_one_client () =
-(*  lprintf "upload_to_one_client %d %d\n" (Fifo.length upload_clients)
-  !remaining_bandwidth;
-Fifo.iter (fun c -> lprintf "   client %d\n" (client_num c))
-upload_clients; *)
-  if !remaining_bandwidth < 10000 then begin
-      let c = Fifo.take upload_clients in
-      client_can_upload c  !remaining_bandwidth
-    end else
-  let per_client =
-    let len = Fifo.length upload_clients in
-    if len * 10000 < !remaining_bandwidth then
-(* Each client in the Fifo can receive 10000 bytes.
-Divide the bandwidth between the clients
-*)
-      (!remaining_bandwidth / 10000 / len) * 10000
-    else mini 10000 !remaining_bandwidth in
-  let c = Fifo.take upload_clients in
-  client_can_upload c per_client
+let next_uploads () =
 
-let rec fifo_uploads n =
-  if n>0 && !remaining_bandwidth > 0 then
-    begin
-      upload_to_one_client ();
-      fifo_uploads (n-1)
-    end
+  let rec next_uploads_aux () =
+    let rec next_uploads_round n =
+      let upload_to_one_client max_amount =
+        let c = Fifo.take upload_clients in
+        client_can_upload c max_amount
+(* it's up to client_can_upload to put the client back into the Fifo *)
+      in
 
-let rec next_uploads () =
-(*  lprintf "next_uploads %d\n" !remaining_bandwidth; *)
-  let old_remaining_bandwidth = !remaining_bandwidth in
-  let len = Fifo.length upload_clients in
-  fifo_uploads len;
-  if !remaining_bandwidth < old_remaining_bandwidth then
-    next_uploads ()
+(*  lprintf "next_uploads %d %d\n" 
+      (Fifo.length upload_clients) !streaming_left;
+    Fifo.iter (fun c -> 
+      lprintf "   client %d\n" (client_num c)
+    ) upload_clients; *)
+      if n>0 && 
+        not (Fifo.empty upload_clients) && 
+        !streaming_left > 0 then begin
+          upload_to_one_client packet_size;
+          next_uploads_round (n-1)
+      end in
+
+(* stop if no uploader could take anything during the last round *)
+    let old_streaming_left = !streaming_left in
+    next_uploads_round (Fifo.length upload_clients);
+    if !streaming_left < old_streaming_left then
+      next_uploads_aux () in
 
-let next_uploads () =
-  sent_bytes.(!counter-1) <- sent_bytes.(!counter-1) - !remaining_bandwidth;
   (*
   if !verbose_upload then begin
-      lprintf "Left %d\n" !remaining_bandwidth;
-    end; *)
-  complete_bandwidth := !complete_bandwidth + !remaining_bandwidth;
-  incr counter;
-  if !counter = 11 then begin
-      counter := 1;
-      total_bandwidth :=
-      (if !!max_hard_upload_rate = 0 then 10000 * 1024
-        else (maxi (!!max_hard_upload_rate - 1) 1) * 1024 );
-      complete_bandwidth := !total_bandwidth;
-(*      lprintf "Init to %d\n" !total_bandwidth;  *)
-      remaining_bandwidth := 0
-    end;
-
-  let last_sec = ref 0 in
-  for i = 0 to 9 do
-    last_sec := !last_sec + sent_bytes.(i)
-  done;
-
-(*  if !verbose_upload then begin
-      lprintf "last sec: %d/%d (left %d)\n" !last_sec !total_bandwidth
-        (!total_bandwidth - !last_sec);
- (*
-      for i = 0 to 9 do
-        lprintf "    last[%d] = %d\n" i  sent_bytes.(i)
-      done; *)
-
+      lprintf "streaming_left %d\n" !streaming_left;
     end; *)
-  remaining_bandwidth := mini (mini (mini
-        (maxi (!remaining_bandwidth + !total_bandwidth / 10) 10000)
-      !total_bandwidth) !complete_bandwidth)
-  (!total_bandwidth - !last_sec);
-  complete_bandwidth := !complete_bandwidth - !remaining_bandwidth;
-(*  lprintf "Remaining %d[%d]\n" !remaining_bandwidth !complete_bandwidth;  *)
-  sent_bytes.(!counter-1) <- !remaining_bandwidth;
-  if !remaining_bandwidth > 0 then
-    next_uploads ()
+  (* buffer empties with time... *)
+  let new_streaming_time = BasicSocket.current_time () in
+  let deltat = (match !streaming_time with
+    | None -> 0.
+    | Some t -> new_streaming_time -. t) in
+  streaming_left := !streaming_left + 
+    (int_of_float (!CommonGlobals.payload_bandwidth *. deltat));
+  if !streaming_left > streaming_amount then
+    streaming_left := streaming_amount;
+  streaming_time := Some new_streaming_time;
+  next_uploads_aux ()
 
 let reset_upload_timer () = ()
 
@@ -940,8 +900,12 @@
 
   let slot_bw = 3072 in
   let min_upload_slots = 3 in
-(*  let estimated_capacity = !!max_hard_upload_rate * 1024 in *)
   let estimated_capacity = detected_uplink_capacity () in
+  let estimated_capacity = if !!max_hard_upload_rate = 0 then
+    estimated_capacity
+  else 
+    (* max_hard_upload_rate lowered manually,... *)
+    mini estimated_capacity (!!max_hard_upload_rate * 1024) in
   if !verbose_upload then
     lprintf_nl "[cUp] usage: %d(%d) capacity: %d"
       (short_delay_upload_usage ())
@@ -999,9 +963,7 @@
     end
 
 let consume_bandwidth len =
-  remaining_bandwidth := !remaining_bandwidth - len
-
-let remaining_bandwidth () = !remaining_bandwidth
+  streaming_left := !streaming_left - len
 
 (**********************************************************************
 
diff -x CVS -x Root -x Repository -x Tag -x Entries -x Entries.Log -x .svn -x '*.rej' -x '*.orig' -x .cvsignore -N -r -u ./src/networks/donkey/donkeyFiles.ml ./src/networks/donkey/donkeyFiles.ml
--- ./src/networks/donkey/donkeyFiles.ml	2005-08-15 22:27:56.000000000 +0200
+++ ./src/networks/donkey/donkeyFiles.ml	2005-09-05 13:43:27.000000000 +0200
@@ -47,7 +47,7 @@
 
 let verbose_upload = false
       
-let msg_block_size_int = 10000
+let msg_block_size_int = 10240
 let msg_block_size = Int64.of_int msg_block_size_int
 let upload_buffer = String.create msg_block_size_int
 let max_msg_size = 15000
@@ -78,7 +78,6 @@
     let rec send_small_block c sock file begin_pos len_int = 
 (*      lprintf "send_small_block %d\n" len_int; *)
 (*      let len_int = Int32.to_int len in *)
-      CommonUploads.consume_bandwidth len_int;
       try
         if !verbose then begin
             lprintf "send_small_block(%s-%s) %Ld %d\n"
@@ -107,6 +106,7 @@
         Unix32.read (file_fd file) begin_pos upload_buffer slen len_int;
         let uploaded = Int64.of_int len_int in
         count_upload c file uploaded;
+	CommonUploads.consume_bandwidth len_int;
         network_must_update network;
         (match file.file_shared with None -> ()
           | Some impl ->
@@ -125,7 +125,7 @@
     
     let rec send_client_block c sock per_client =
 (*      lprintf "send_client_block\n"; *)
-      if per_client > 0 && CommonUploads.remaining_bandwidth () > 0 then
+      if per_client > 0 && CommonUploads.can_write_len sock max_msg_size then
         match c.client_upload with
         | Some ({ up_chunks = _ :: chunks } as up)  ->
             if up.up_file.file_shared = None then begin
@@ -163,8 +163,7 @@
                 up.up_pos <- Int64.add up.up_pos 
                   (Int64.of_int msg_block_size_int);
                 let per_client = per_client-msg_block_size_int in
-                if can_write_len sock max_msg_size then
-                  send_client_block c sock per_client
+                send_client_block c sock per_client
               end
         | _ -> ()
     
@@ -173,14 +172,9 @@
       do_if_connected  c.client_source.DonkeySources.source_sock (fun sock ->
 (*    lprintf "upload_to_client %d connected\n"  (maxi max_msg_size size); *)
           
-(* changed in 2.5.27: mini instead of maxi *)
           let size = mini max_msg_size size in
-          if CommonUploads.can_write_len sock size then 
-            begin
-(*              lprintf "can_write_len %d\n"  (maxi max_msg_size size); *)
-              send_client_block c sock size;
-            end;
-          (match c.client_upload with
+          send_client_block c sock size;
+           (match c.client_upload with
               None -> ()
             | Some up ->
                 if !CommonUploads.has_upload = 0 then
