X-Git-Url: https://git.wh0rd.org/?p=chrome-ext%2Fmusic-player-client.git;a=blobdiff_plain;f=js%2Fmpc.js;h=ae013332e618a7700ae0e5a75f4d913c3f94f671;hp=4860682ade5760e8554fe02a3ce7285a31018cd5;hb=3903477400d2618c07543eba122ab794dac8fb5a;hpb=60d8b85a1a317d5226a3537d8a78e91138d6395d diff --git a/js/mpc.js b/js/mpc.js index 4860682..ae01333 100644 --- 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; } /*