Skip to content

Commit

Permalink
fixed endian bug
Browse files Browse the repository at this point in the history
  • Loading branch information
masuidrive committed Nov 16, 2011
1 parent 01d5595 commit 0f30370
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
7 changes: 5 additions & 2 deletions lib/websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ WebSocket.prototype._create_frame = function(opcode, d, last_frame) {
source: length,
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_SHORT
type: Ti.Codec.TYPE_SHORT,
byteOrder: Ti.Codec.BIG_ENDIAN
});
outIndex += 2;
}
Expand All @@ -243,7 +244,8 @@ WebSocket.prototype._create_frame = function(opcode, d, last_frame) {
source: length,
dest: out,
position: outIndex,
type: Ti.Codec.TYPE_LONG
type: Ti.Codec.TYPE_LONG,
byteOrder: Ti.Codec.BIG_ENDIAN
});
outIndex += 8;
}
Expand Down Expand Up @@ -456,6 +458,7 @@ WebSocket.prototype.close = function(code, message) {
dest: buffer,
position: 0,
type: Ti.Codec.TYPE_SHORT,
byteOrder: Ti.Codec.BIG_ENDIAN
});

if(message) {
Expand Down
17 changes: 8 additions & 9 deletions ti-websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,8 @@ WebSocket.prototype._create_frame = function(opcode, d, last_frame) {
source: length,
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_SHORT
type: Ti.Codec.TYPE_SHORT,
byteOrder: Ti.Codec.BIG_ENDIAN
});
outIndex += 2;
}
Expand All @@ -870,7 +871,8 @@ WebSocket.prototype._create_frame = function(opcode, d, last_frame) {
source: length,
dest: out,
position: outIndex,
type: Ti.Codec.TYPE_LONG
type: Ti.Codec.TYPE_LONG,
byteOrder: Ti.Codec.BIG_ENDIAN
});
outIndex += 8;
}
Expand Down Expand Up @@ -929,7 +931,7 @@ WebSocket.prototype._mask_payload = function(out, outIndex, payload) {
}
};

var parse_frame = function(buffer, size) {
var parse_frame = function(buffer, size, socket) {
if(size < 3) return undefined;

var byte1 = Utils.read_byte(buffer, 0);
Expand All @@ -943,21 +945,17 @@ var parse_frame = function(buffer, size) {
var offset = 2;
switch(len) {
case 126:
var bytesRead = socket.read(buffer, 0, 2);
len = Utils.read_2byte(buffer, offset);
offset += 2;
break;

case 127:
// too large
var bytesRead = socket.read(buffer, 0, 8);
// too large I felt
len = Utils.read_8byte(buffer, offset);
offset += 8;
break;
}

if(size < len + offset) return undefined;

var string = Ti.Codec.decodeString({
source: buffer,
position: offset,
Expand Down Expand Up @@ -1013,7 +1011,7 @@ WebSocket.prototype._readCallback = function(e) {
return;
}

var frame = parse_frame(e.buffer, e.bytesProcessed);
var frame = parse_frame(e.buffer, e.bytesProcessed, this.socket);
e.buffer.clear();
// TODO: check frame

Expand Down Expand Up @@ -1087,6 +1085,7 @@ WebSocket.prototype.close = function(code, message) {
dest: buffer,
position: 0,
type: Ti.Codec.TYPE_SHORT,
byteOrder: Ti.Codec.BIG_ENDIAN
});

if(message) {
Expand Down

0 comments on commit 0f30370

Please sign in to comment.