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

Pavel Grunt pgrunt at redhat.com
Wed Jan 14 08:44:42 PST 2015


Stop reading a 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 54e6963..d9dced9 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;
     }
 
@@ -317,6 +326,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