[Spice-devel] [PATCH spice-html5 5/5] Read file only when have agent tokens

Pavel Grunt pgrunt at redhat.com
Thu Jan 8 06:19:47 PST 2015


Stop reading file when there are no tokens,
continue reading it when tokens arrive.
---
 main.js | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/main.js b/main.js
index 5c4a470..7c36342 100644
--- a/main.js
+++ b/main.js
@@ -58,6 +58,7 @@ function SpiceMainConn()
     this.agent_msg_queue = [];
     this.file_xfer_tasks = {};
     this.file_xfer_task_id = 0;
+    this.file_xfer_read_queue = [];
 }
 
 SpiceMainConn.prototype = Object.create(SpiceConn.prototype);
@@ -162,9 +163,17 @@ SpiceMainConn.prototype.process_channel_message = function(msg)
 
     if (msg.type == SPICE_MSG_MAIN_AGENT_TOKEN)
     {
-        var tokens = new SpiceMsgMainAgentTokens(msg.data);
+        var remaining_tokens, tokens = new SpiceMsgMainAgentTokens(msg.data);
         this.agent_tokens += tokens.num_tokens;
         this.send_agent_message_queue();
+
+        remaining_tokens = this.agent_tokens;
+        while (remaining_tokens > 0 && this.file_xfer_read_queue.length > 0)
+        {
+            var xfer_task = this.file_xfer_read_queue.shift();
+            this.file_xfer_read(xfer_task, xfer_task.read_bytes);
+            remaining_tokens--;
+        }
         return true;
     }
 
@@ -306,6 +315,13 @@ SpiceMainConn.prototype.file_xfer_read = function(file_xfer_task, start_byte)
     sb = start_byte || 0,
     eb = Math.min(sb + FILE_XFER_CHUNK_SIZE, file_xfer_task.file.size);
 
+    if (!this.agent_tokens)
+    {
+        file_xfer_task.read_bytes = sb;
+        this.file_xfer_read_queue.push(file_xfer_task);
+        return;
+    }
+
     reader = new FileReader();
     reader.onload = function(e)
     {
-- 
1.9.3



More information about the Spice-devel mailing list