]> git.wh0rd.org - chrome-ext/music-player-client.git/blobdiff - js/mpc.js
mpc: handle ACK responses
[chrome-ext/music-player-client.git] / js / mpc.js
index 4860682ade5760e8554fe02a3ce7285a31018cd5..ae013332e618a7700ae0e5a75f4d913c3f94f671 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._recv_buffer = [];
+       this._recv_buffer_last = 0;
        this.state = {};
 }
 
@@ -32,6 +34,8 @@ Mpc.prototype.send = function(msg) {
 Mpc.prototype.recv_msg = function(lines) {
        curr = this._queue.shift();
        this.log(0x2, 'recv: [' + curr + ']:', lines.join('\n'));
+       if (lines[0].substr(0, 4) == 'ACK ')
+               this.err(curr, lines.join('\n'));
        curr = curr.split(' ');
 
        switch (curr[0]) {
@@ -74,16 +78,23 @@ 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 */
-       /* 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 ') {
+               if (lines[i] == 'OK' || lines[i].substr(0, 3) == 'OK ' ||
+                   lines[i].substr(0, 4) == 'ACK ') {
                        this.recv_msg(lines.splice(0, i + 1));
                        i = 0;
                } 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;
 }
 
 /*