]> git.wh0rd.org - chrome-ext/music-player-client.git/commitdiff
mpc: properly handle responses that spread multiple buffers
authorMike Frysinger <vapier@gentoo.org>
Sun, 1 Sep 2013 09:27:34 +0000 (05:27 -0400)
committerMike Frysinger <vapier@gentoo.org>
Sun, 1 Sep 2013 09:27:34 +0000 (05:27 -0400)
When using commands like playlistinfo, the result is much larger than a
single buffer can handle.  So handle merging multiple buffers.

js/mpc.js

index 4860682ade5760e8554fe02a3ce7285a31018cd5..dfcd5fd304278d8d520e637ad98f560dd06c25a1 100644 (file)
--- a/js/mpc.js
+++ b/js/mpc.js
@@ -5,6 +5,8 @@ function Mpc(socket, cb_update_state, debug_enabled) {
        this._cb_update_state = cb_update_state;
        this._debug_enabled = debug_enabled;
        this._queue = ['init'];
        this._cb_update_state = cb_update_state;
        this._debug_enabled = debug_enabled;
        this._queue = ['init'];
+       this._recv_buffer = [];
+       this._recv_buffer_last = 0;
        this.state = {};
 }
 
        this.state = {};
 }
 
@@ -74,8 +76,7 @@ Mpc.prototype.recv_msg = function(lines) {
 
 Mpc.prototype.recv = function(msg) {
        /* We can get back a bunch of responses in a row, so parse them out */
 
 Mpc.prototype.recv = function(msg) {
        /* We can get back a bunch of responses in a row, so parse them out */
-       /* XXX: Do we have to handle partial reads ?  like long playlists ... */
-       lines = msg.split('\n');
+       var lines = this._recv_buffer = this._recv_buffer.concat(msg.split('\n'));
        var i = 0;
        while (i < lines.length) {
                if (lines[i] == 'OK' || lines[i].substr(0, 3) == 'OK ') {
        var i = 0;
        while (i < lines.length) {
                if (lines[i] == 'OK' || lines[i].substr(0, 3) == 'OK ') {
@@ -84,6 +85,13 @@ Mpc.prototype.recv = function(msg) {
                } else
                        ++i;
        }
                } else
                        ++i;
        }
+
+       if (lines.length && this._recv_buffer_last != lines.length) {
+               // Keep sucking in data so long as more exists.
+               this._recv_buffer_last = lines.length;
+               this._socket.poll();
+       } else
+               this._recv_buffer_last = lines.length;
 }
 
 /*
 }
 
 /*