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

cleanup

parent 21034f29
......@@ -5,32 +5,32 @@
// Created by Yoann Pigné on 2012-05-14.
// Copyright 2012 University of Le Havre. All rights reserved.
//
(function (global) {
(function(global) {
"use strict";
if (!global.netstream) {
global.netstream = {};
}
// Copyright (c) 2008 notmasteryet
global.netstream.TypedArrayReader = function (t_array) {
// Copyright (c) 2008 notmasteryet
global.netstream.TypedArrayReader = function(t_array) {
this.position = 0;
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) {
return null;
}
return this.array[this.position++];
};
}
// RFC 2279
global.netstream.Utf8Translator = function (reader) {
};
// RFC 2279
global.netstream.Utf8Translator = function(reader) {
this.reader = reader;
this.waitBom = true;
this.pendingChar = null;
this.readChar = function () {
this.readChar = function() {
var ch = null;
do {
if (this.pendingChar !== null) {
......@@ -48,7 +48,7 @@ global.netstream.Utf8Translator = function (reader) {
var validBits = 5;
do {
var mask = currentPrefix >> 1 | 0x80;
if ((b1 & mask) === currentPrefix){
if ((b1 & mask) === currentPrefix) {
break;
}
currentPrefix = currentPrefix >> 1 | 0x80;
......@@ -63,7 +63,7 @@ global.netstream.Utf8Translator = function (reader) {
code = (code << 6) | (bi & 0x3F);
}
if (code <= 0xFFFF) {
if (code == 0xFEFF && this.waitBom)
if (code === 0xFEFF && this.waitBom)
ch = null;
else
ch = String.fromCharCode(code);
......@@ -84,15 +84,15 @@ global.netstream.Utf8Translator = function (reader) {
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),
utf8 = new netstream.Utf8Translator(reader),
......@@ -107,9 +107,9 @@ global.netstream.typedArrayToString = function (input) {
}
s = result.join("");
return s;
}
};
global.netstream.base64 = {
global.netstream.base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
_keyCode: {
65: 0,
......@@ -204,7 +204,7 @@ global.netstream.base64 = {
for (i = 0; i < input.length; i++) {
var d = input[i];
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) {
......@@ -258,16 +258,16 @@ global.netstream.base64 = {
}
return base64;
}
};
};
global.netstream.LOGGER = function (msg) {
global.netstream.LOGGER = function(msg) {
//console.log("-");return;
if (typeof(console.log) == "function") {
console.log(msg);
}
else if (typeof(window.status) == "string") {
window.status = msg;
else if (typeof(global.status) == "string") {
global.status = msg;
}
}
};
}(this));
\ No newline at end of file
} (this));
\ No newline at end of file
......@@ -177,7 +177,7 @@
this.socket.onmessage = function (e) {
if (e.data != "Heartbeat") {
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);
}
......@@ -202,7 +202,7 @@
clearInterval(gs.refreshIntervalId);
}
else {
if (this.debug) {
if (gs.debug) {
netstream.LOGGER("> Sending Hearbeat");
}
gs.socket.send("Heartbeat");
......@@ -246,12 +246,12 @@
msgIndex = res.index;
if (this.debug) {
netstream.LOGGER("(GSTransport): Stream " + stream + " is addressed in this message");
var m=[]
,arr = new Uint8Array(msg.buffer);
for(var i =0; i< arr.length; i++){
m[i]=arr[i]
}
netstream.LOGGER(m.join(', '));
// var m=[]
// ,arr = new Uint8Array(msg.buffer);
// for(var i =0; i< arr.length; i++){
// m[i]=arr[i]
// }
// netstream.LOGGER(m.join(', '));
}
// get the command
var cmd = -1;
......@@ -319,6 +319,21 @@
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) {
var limit = 0;
// Index past the last byte this.read during the current
......@@ -326,17 +341,7 @@
var nbytes = 0;
// Number of bytes this.read.
nbytes = encodedMsg.length;
if(nbytes > this.buffer.byteLength){
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;
}
this.ensureBufferCapacity(nbytes);
var encodedArray = new Uint8Array(this.buffer, this.pos, nbytes);
for (var i = 0; i < nbytes; 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