Skip to content

Instantly share code, notes, and snippets.

@umegaya
Created February 13, 2012 16:55
Show Gist options
  • Save umegaya/1818228 to your computer and use it in GitHub Desktop.
Save umegaya/1818228 to your computer and use it in GitHub Desktop.
@uupaa msgpack.js: modification to unpack buffer which has multiple message pack records
[iyatomi@localhost msgpack]$ diff _msgpack.js msgpack.js
63c63
< // @return Mix/undefined/null:
---
> // @return Mix/undefined:
65d64
< // null means buffer exhausted
69,75c68,69
< if (data != null) {
< _buf = typeof data === "string" ? toByteArray(data) : data;
< _idx = -1;
< }
< else if (_buf.length <= _idx) {
< return null;
< }
---
> _buf = typeof data === "string" ? toByteArray(data) : data;
> _idx = -1;
usage:
like C function strtok
Dispatcher.prototype.recv = function(conn, e) {
var self = this;
var m = _G.msgpack.unpack(new Uint8Array(e.data));
if (typeof(m) == undefined) {
return;
}
while (m != null) {
if (m[0] == RESPONSE) {
var cb = self.dispatch_table[m[1]];
self.dispatch_table[m[1]] = null;
cb(conn, m[3], m[2]);
}
else if (m[0] == REQUEST) {
/* TODO: resolve function with _G and m[1] and call it */
resp = [RESPONSE, m[2]].concat(self.fetch(_G, m));
conn.send(_G.msgpack.pack(resp))
}
m = _G.msgpack.unpack(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment