Commit fe89f3b0 authored by Yoann Pigné's avatar Yoann Pigné

cleanup

parent 21034f29
// //
// netstream_commons.js // netstream_commons.js
// netstream project // netstream project
// //
// Created by Yoann Pigné on 2012-05-14. // Created by Yoann Pigné on 2012-05-14.
// Copyright 2012 University of Le Havre. All rights reserved. // Copyright 2012 University of Le Havre. All rights reserved.
// //
(function(global) {
"use strict";
(function (global) { if (!global.netstream) {
"use strict"; global.netstream = {};
}
// Copyright (c) 2008 notmasteryet
global.netstream.TypedArrayReader = function(t_array) {
if (!global.netstream) {
global.netstream = {};
}
// Copyright (c) 2008 notmasteryet
global.netstream.TypedArrayReader = function (t_array) {
this.position = 0; this.position = 0;
this.array = new Uint8Array(t_array.buffer, t_array.byteOffset, t_array.byteLength); this.array = new Uint8Array(t_array.buffer, t_array.byteOffset, t_array.byteLength);
this.readByte = function () { this.readByte = function() {
if (this.position >= this.array.length) { if (this.position >= this.array.length) {
return null; return null;
} }
return this.array[this.position++]; return this.array[this.position++];
}; };
} };
// RFC 2279 // RFC 2279
global.netstream.Utf8Translator = function (reader) { global.netstream.Utf8Translator = function(reader) {
this.reader = reader; this.reader = reader;
this.waitBom = true; this.waitBom = true;
this.pendingChar = null; this.pendingChar = null;
this.readChar = function () { this.readChar = function() {
var ch = null; var ch = null;
do { do {
if (this.pendingChar !== null) { if (this.pendingChar !== null) {
ch = this.pendingChar; ch = this.pendingChar;
this.pendingChar = null; this.pendingChar = null;
} else {
var b1 = this.reader.readByte();
if (b1 < 0) {
return null;
}
if ((b1 & 0x80) === 0) {
ch = String.fromCharCode(b1);
} else {
var currentPrefix = 0xC0;
var validBits = 5;
do {
var mask = currentPrefix >> 1 | 0x80;
if ((b1 & mask) === currentPrefix) {
break;
}
currentPrefix = currentPrefix >> 1 | 0x80;
--validBits;
}
while (validBits >= 0);
if (validBits > 0) {
var code = (b1 & ((1 << validBits) - 1));
for (var i = 5; i >= validBits; --i) {
var bi = this.reader.readByte();
if ((bi & 0xC0) != 0x80) throw "Invalid sequence character";
code = (code << 6) | (bi & 0x3F);
}
if (code <= 0xFFFF) {
if (code === 0xFEFF && this.waitBom)
ch = null;
else
ch = String.fromCharCode(code);
} else {
var v = code - 0x10000;
var w1 = 0xD800 | ((v >> 10) & 0x3FF);
var w2 = 0xDC00 | (v & 0x3FF);
this.pendingChar = String.fromCharCode(w2);
ch = String.fromCharCode(w1);
}
} else { } else {
var b1 = this.reader.readByte(); throw "Invalid character";
if (b1 < 0) {
return null;
}
if ((b1 & 0x80) === 0) {
ch = String.fromCharCode(b1);
} else {
var currentPrefix = 0xC0;
var validBits = 5;
do {
var mask = currentPrefix >> 1 | 0x80;
if ((b1 & mask) === currentPrefix){
break;
}
currentPrefix = currentPrefix >> 1 | 0x80;
--validBits;
}
while (validBits >= 0);
if (validBits > 0) {
var code = (b1 & ((1 << validBits) - 1));
for (var i = 5; i >= validBits; --i) {
var bi = this.reader.readByte();
if ((bi & 0xC0) != 0x80) throw "Invalid sequence character";
code = (code << 6) | (bi & 0x3F);
}
if (code <= 0xFFFF) {
if (code == 0xFEFF && this.waitBom)
ch = null;
else
ch = String.fromCharCode(code);
} else {
var v = code - 0x10000;
var w1 = 0xD800 | ((v >> 10) & 0x3FF);
var w2 = 0xDC00 | (v & 0x3FF);
this.pendingChar = String.fromCharCode(w2);
ch = String.fromCharCode(w1);
}
} else {
throw "Invalid character";
}
}
} }
this.waitBom = false; }
} }
while (ch === null); this.waitBom = false;
return ch; }
while (ch === null);
return ch;
}; };
} };
//
// Takes a typed Array received from the websocket and supposly encoding an utf8 string.
// Converts it to a unicode string.
//
// BEING USED
//
global.netstream.typedArrayToString = function(input) {
//
// Takes a typed Array received from the websocket and supposly encoding an utf8 string.
// Converts it to a unicode string.
//
// BEING USED
//
global.netstream.typedArrayToString = function (input) {
var reader = new netstream.TypedArrayReader(input), var reader = new netstream.TypedArrayReader(input),
utf8 = new netstream.Utf8Translator(reader), utf8 = new netstream.Utf8Translator(reader),
result = [], result = [],
...@@ -102,172 +102,172 @@ global.netstream.typedArrayToString = function (input) { ...@@ -102,172 +102,172 @@ global.netstream.typedArrayToString = function (input) {
s = ""; s = "";
ch = utf8.readChar(); ch = utf8.readChar();
while (ch.charCodeAt(0) !== 0) { while (ch.charCodeAt(0) !== 0) {
result[i++] = ch; result[i++] = ch;
ch = utf8.readChar(); ch = utf8.readChar();
} }
s = result.join(""); s = result.join("");
return s; return s;
} };
global.netstream.base64 = { global.netstream.base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
_keyCode: { _keyCode: {
65: 0, 65: 0,
66: 1, 66: 1,
67: 2, 67: 2,
68: 3, 68: 3,
69: 4, 69: 4,
70: 5, 70: 5,
71: 6, 71: 6,
72: 7, 72: 7,
73: 8, 73: 8,
74: 9, 74: 9,
75: 10, 75: 10,
76: 11, 76: 11,
77: 12, 77: 12,
78: 13, 78: 13,
79: 14, 79: 14,
80: 15, 80: 15,
81: 16, 81: 16,
82: 17, 82: 17,
83: 18, 83: 18,
84: 19, 84: 19,
85: 20, 85: 20,
86: 21, 86: 21,
87: 22, 87: 22,
88: 23, 88: 23,
89: 24, 89: 24,
90: 25, 90: 25,
97: 26, 97: 26,
98: 27, 98: 27,
99: 28, 99: 28,
100: 29, 100: 29,
101: 30, 101: 30,
102: 31, 102: 31,
103: 32, 103: 32,
104: 33, 104: 33,
105: 34, 105: 34,
106: 35, 106: 35,
107: 36, 107: 36,
108: 37, 108: 37,
109: 38, 109: 38,
110: 39, 110: 39,
111: 40, 111: 40,
112: 41, 112: 41,
113: 42, 113: 42,
114: 43, 114: 43,
115: 44, 115: 44,
116: 45, 116: 45,
117: 46, 117: 46,
118: 47, 118: 47,
119: 48, 119: 48,
120: 49, 120: 49,
121: 50, 121: 50,
122: 51, 122: 51,
48: 52, 48: 52,
49: 53, 49: 53,
50: 54, 50: 54,
51: 55, 51: 55,
52: 56, 52: 56,
53: 57, 53: 57,
54: 58, 54: 58,
55: 59, 55: 59,
56: 60, 56: 60,
57: 61, 57: 61,
43: 62, 43: 62,
47: 63, 47: 63,
61: 64 61: 64
}, },
decode: function(input) { decode: function(input) {
var lkey1 = this._keyStr.indexOf(input[input.length - 1]); var lkey1 = this._keyStr.indexOf(input[input.length - 1]);
var lkey2 = this._keyStr.indexOf(input[input.length - 2]); var lkey2 = this._keyStr.indexOf(input[input.length - 2]);
var bytes = Math.ceil((3 * input.length) / 4.0); var bytes = Math.ceil((3 * input.length) / 4.0);
if (lkey1 == 64) bytes--; if (lkey1 == 64) bytes--;
//padding chars, so skip //padding chars, so skip
if (lkey2 == 64) bytes--; if (lkey2 == 64) bytes--;
//padding chars, so skip //padding chars, so skip
var arrayBuffer = new ArrayBuffer(bytes); var arrayBuffer = new ArrayBuffer(bytes);
var uarray; var uarray;
var chr1, var chr1,
chr2, chr2,
chr3; chr3;
var enc1, var enc1,
enc2, enc2,
enc3, enc3,
enc4; enc4;
var i = 0; var i = 0;
var j = 0; var j = 0;
if (arrayBuffer) uarray = new Uint8Array(arrayBuffer); if (arrayBuffer) uarray = new Uint8Array(arrayBuffer);
else uarray = new Uint8Array(bytes); else uarray = new Uint8Array(bytes);
//input = input.replace(/[^A-Za-z0-9\+\/\=]/g, ""); //input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
for (i = 0; i < input.length; i++) { for (i = 0; i < input.length; i++) {
var d = input[i]; var d = input[i];
if ((d < 65 || d > 90) && (d < 97 || d > 122) & (d < 47 || d > 57) & d != 61 & d != 43) { if ((d < 65 || d > 90) && (d < 97 || d > 122) & (d < 47 || d > 57) & d != 61 & d != 43) {
throw "illegal character in base64 string "+d; throw "illegal character in base64 string " + d;
}
}
for (i = 0; i < bytes; i += 3) {
enc1 = this._keyCode[input[j++]];
//.indexOf(input.charAt(j++));
enc2 = this._keyCode[input[j++]];
//this._keyStr.indexOf(input.charAt(j++));
enc3 = this._keyCode[input[j++]];
//this._keyStr.indexOf(input.charAt(j++));
enc4 = this._keyCode[input[j++]];
//this._keyStr.indexOf(input.charAt(j++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
uarray[i] = chr1;
if (enc3 != 64) uarray[i + 1] = chr2;
if (enc4 != 64) uarray[i + 2] = chr3;
} }
return uarray; }
for (i = 0; i < bytes; i += 3) {
enc1 = this._keyCode[input[j++]];
//.indexOf(input.charAt(j++));
enc2 = this._keyCode[input[j++]];
//this._keyStr.indexOf(input.charAt(j++));
enc3 = this._keyCode[input[j++]];
//this._keyStr.indexOf(input.charAt(j++));
enc4 = this._keyCode[input[j++]];
//this._keyStr.indexOf(input.charAt(j++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
uarray[i] = chr1;
if (enc3 != 64) uarray[i + 1] = chr2;
if (enc4 != 64) uarray[i + 2] = chr3;
}
return uarray;
}, },
encode: function(arrayBuffer) { encode: function(arrayBuffer) {
var base64 = ''; var base64 = '';
var bytes = new Uint8Array(arrayBuffer); var bytes = new Uint8Array(arrayBuffer);
var byteLength = bytes.byteLength; var byteLength = bytes.byteLength;
var byteRemainder = byteLength % 3; var byteRemainder = byteLength % 3;
var mainLength = byteLength - byteRemainder; var mainLength = byteLength - byteRemainder;
var a, var a,
b, b,
c, c,
d; d;
var chunk; var chunk;
for (var i = 0; i < mainLength; i = i + 3) { for (var i = 0; i < mainLength; i = i + 3) {
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]; chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
a = (chunk & 16515072) >> 18; a = (chunk & 16515072) >> 18;
b = (chunk & 258048) >> 12; b = (chunk & 258048) >> 12;
c = (chunk & 4032) >> 6; c = (chunk & 4032) >> 6;
d = chunk & 63; d = chunk & 63;
base64 += this._keyStr[a] + this._keyStr[b] + this._keyStr[c] + this._keyStr[d]; base64 += this._keyStr[a] + this._keyStr[b] + this._keyStr[c] + this._keyStr[d];
} }
if (byteRemainder == 1) { if (byteRemainder == 1) {
chunk = bytes[mainLength]; chunk = bytes[mainLength];
a = (chunk & 252) >> 2; a = (chunk & 252) >> 2;
b = (chunk & 3) << 4; b = (chunk & 3) << 4;
base64 += this._keyStr[a] + this._keyStr[b] + '=='; base64 += this._keyStr[a] + this._keyStr[b] + '==';
} else if (byteRemainder == 2) { } else if (byteRemainder == 2) {
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]; chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1];
a = (chunk & 64512) >> 10; a = (chunk & 64512) >> 10;
b = (chunk & 1008) >> 4; b = (chunk & 1008) >> 4;
c = (chunk & 15) << 2; c = (chunk & 15) << 2;
base64 += this._keyStr[a] + this._keyStr[b] + this._keyStr[c] + '='; base64 += this._keyStr[a] + this._keyStr[b] + this._keyStr[c] + '=';
} }
return base64; return base64;
} }
}; };
global.netstream.LOGGER = function (msg) { global.netstream.LOGGER = function(msg) {
//console.log("-");return; //console.log("-");return;
if (typeof(console.log) == "function") { if (typeof(console.log) == "function") {
console.log(msg); console.log(msg);
} }
else if (typeof(window.status) == "string") { else if (typeof(global.status) == "string") {
window.status = msg; global.status = msg;
} }
} };
}(this)); } (this));
\ No newline at end of file \ No newline at end of file
...@@ -177,7 +177,7 @@ ...@@ -177,7 +177,7 @@
this.socket.onmessage = function (e) { this.socket.onmessage = function (e) {
if (e.data != "Heartbeat") { if (e.data != "Heartbeat") {
if (gs.debug === true) { if (gs.debug === true) {
netstream.LOGGER("(gs_transport): < Received a " + e.data.length + " bytes long data chunk: "+e.data); netstream.LOGGER("(gs_transport): < Received a " + e.data.length + " bytes long data chunk.");
} }
gs.handleMsg(e.data); gs.handleMsg(e.data);
} }
...@@ -202,7 +202,7 @@ ...@@ -202,7 +202,7 @@
clearInterval(gs.refreshIntervalId); clearInterval(gs.refreshIntervalId);
} }
else { else {
if (this.debug) { if (gs.debug) {
netstream.LOGGER("> Sending Hearbeat"); netstream.LOGGER("> Sending Hearbeat");
} }
gs.socket.send("Heartbeat"); gs.socket.send("Heartbeat");
...@@ -246,12 +246,12 @@ ...@@ -246,12 +246,12 @@
msgIndex = res.index; msgIndex = res.index;
if (this.debug) { if (this.debug) {
netstream.LOGGER("(GSTransport): Stream " + stream + " is addressed in this message"); netstream.LOGGER("(GSTransport): Stream " + stream + " is addressed in this message");
var m=[] // var m=[]
,arr = new Uint8Array(msg.buffer); // ,arr = new Uint8Array(msg.buffer);
for(var i =0; i< arr.length; i++){ // for(var i =0; i< arr.length; i++){
m[i]=arr[i] // m[i]=arr[i]
} // }
netstream.LOGGER(m.join(', ')); // netstream.LOGGER(m.join(', '));
} }
// get the command // get the command
var cmd = -1; var cmd = -1;
...@@ -319,6 +319,21 @@ ...@@ -319,6 +319,21 @@
return 0; return 0;
}, },
ensureBufferCapacity: function (size) {
if (size > this.buffer.byteLength) {
if (this.debug) {
console.log("Actual buffer size is " + this.buffer.byteLength + " resizing to " + (2 * size + this.pos) + " bytes", size, this.pos);
}
var newb = new ArrayBuffer(2 * size + this.pos)
, i=0;
for (; i < this.pos; i++) {
newb[i] = this.buffer[i];
}
delete this.buffer;
this.buffer = newb;
}
},
handleMsg: function (encodedMsg) { handleMsg: function (encodedMsg) {
var limit = 0; var limit = 0;
// Index past the last byte this.read during the current // Index past the last byte this.read during the current
...@@ -326,17 +341,7 @@ ...@@ -326,17 +341,7 @@
var nbytes = 0; var nbytes = 0;
// Number of bytes this.read. // Number of bytes this.read.
nbytes = encodedMsg.length; nbytes = encodedMsg.length;
if(nbytes > this.buffer.byteLength){ this.ensureBufferCapacity(nbytes);
if(this.debug){
console.log("resizing buffer size to "+(2*nbytes+this.pos)+" bytes");
}
var newb = new ArrayBuffer(2*nbytes+this.pos);
for(var i=0; i< this.pos; i++){
newb[i]=this.buffer[i];
}
delete this.buffer;
this.buffer=newb;
}
var encodedArray = new Uint8Array(this.buffer, this.pos, nbytes); var encodedArray = new Uint8Array(this.buffer, this.pos, nbytes);
for (var i = 0; i < nbytes; i++) { for (var i = 0; i < nbytes; i++) {
encodedArray[i] = encodedMsg.charCodeAt(i); encodedArray[i] = encodedMsg.charCodeAt(i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment