Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Trap.Message=function(b){this._data=[];this._authData=null;this._format=Trap.Message.Format.SEVEN_BIT_SAFE;this._op=Trap.Message.Operation.OK;this._compressed=false;this._channel=0;this._messageId=0;if(typeof(b)!="undefined"){this.deserialize(b,0,b.length)}};Trap._compat.__defineGetterSetter(Trap.Message.prototype,"messageId");Trap._compat.__defineGetter(Trap.Message.prototype,"channel",function(){return this.format==Trap.Message.Format.REGULAR?this._channel:0});Trap._compat.__defineSetter(Trap.Message.prototype,"channel");Trap._compat.__defineGetterSetter(Trap.Message.prototype,"compressed");Trap._compat.__defineGetter(Trap.Message.prototype,"data",function(){return this._data});Trap._compat.__defineGetter(Trap.Message.prototype,"dataAsString",function(){return String.fromUTF8ByteArray(this._data)});Trap._compat.__defineGetter(Trap.Message.prototype,"string",function(){return String.fromUTF8ByteArray(this._data)});Trap._compat.__defineGetter(Trap.Message.prototype,"authData",function(){return this._authData});Trap._compat.__defineGetter(Trap.Message.prototype,"format",function(){return this._format});Trap._compat.__defineGetter(Trap.Message.prototype,"op",function(){return this._op});Trap._compat.__defineGetter(Trap.Message.prototype,"channelID",function(){return this._channel});Trap._compat.__defineSetter(Trap.Message.prototype,"data",function(b){if(typeof(b)=="string"){this._data=b.toUTF8ByteArray()}else{if(typeof(b.length)=="number"||typeof(b.byteLength)=="number"){this._data=b}else{if(typeof(b)=="number"){this._data=[b]}else{if(typeof(b)=="object"){this._data=JSON.stringify(b).toUTF8ByteArray()}else{throw"Invalid data supplied; not an array, not a string, not a number"}}}}return this});Trap._compat.__defineSetter(Trap.Message.prototype,"authData",function(b){if(!!b&&b.length>65535){throw"authData cannot be more than 65535 bytes"}if(!!b&&b.length!=b.toUTF8ByteArray().length){throw"authData was not a US-ASCII string"}this._authData=b;return this});Trap._compat.__defineSetter(Trap.Message.prototype,"format",function(b){this._format=b;return this});Trap._compat.__defineSetter(Trap.Message.prototype,"op",function(b){this._op=b;return this});Trap._compat.__defineSetter(Trap.Message.prototype,"channelID",function(b){this._channel=b;return this});Trap.Message.Operation={OPEN:1,OPENED:2,CLOSE:3,END:4,CHALLENGE:5,ERROR:6,MESSAGE:8,ACK:9,FRAGMENT_START:10,FRAGMENT_END:11,OK:16,PING:17,PONG:18,TRANSPORT:19,name:function(b){switch(b){case 1:return"OPEN";case 2:return"OPENED";case 3:return"CLOSE";case 4:return"END";case 5:return"CHALLENGE";case 6:return"ERROR";case 8:return"MESSAGE";case 9:return"ACK";case 10:return"FRAGMENT_START";case 11:return"FRAGMENT_END";case 16:return"OK";case 17:return"PING";case 18:return"PONG";case 19:return"TRANSPORT";default:return"Unknown op type: "+b}},getType:function(b){return b}};Trap.Message.Format={REGULAR:"Trap.Message.Format.Regular",SEVEN_BIT_SAFE:"Trap.Message.Format.7bit",DEFAULT:"Trap.Message.Format.Regular"};Trap.Constants.MESSAGE_FORMAT_DEFAULT=Trap.Message.Format.DEFAULT;Trap.Message.prototype.getBits=function(i,j,f){var g=(Math.pow(2,f-j+1)-1);g=g<<(32-f);var h=(i&g)>>(32-f);return h};Trap.Message.prototype.writeInt7=function(c,d){d.write(this.getBits(c,5,11));d.write(this.getBits(c,12,18));d.write(this.getBits(c,19,25));d.write(this.getBits(c,26,32))};Trap.Message.prototype.writeInt8=function(c,d){d.write(this.getBits(c,1,8));d.write(this.getBits(c,9,16));d.write(this.getBits(c,17,24));d.write(this.getBits(c,25,32))};Trap.Message.prototype.serialize=function(c){var d=(c?new Trap.ByteArrayOutputStream():new Trap.ByteStringOutputStream());if(this.format==Trap.Message.Format.SEVEN_BIT_SAFE){this.serialize7bit(d,c)}else{this.serialize8bit(d,c)}if(c){return d.toArray()}else{return d.toString()}};Trap.Message.prototype.getCompressedData=function(){if(!this.compressed){return this.data}if(!this._compressedData){this._compressedData=new Zlib.Deflate(this.data).compress()}return this._compressedData};Trap.Message.prototype.serialize8bit=function(i,h){if(this.data.length>=Math.pow(2,32)){throw"Asked to serialize more than 2^32 bytes data into a 8-bit Trap message"}var j=0;j|=this.op|128;i.write(j);var b=(this.authData!=null?this.authData.length:0);var g=this.getCompressedData();j=0;if(this.compressed&&h){j|=128}i.write(j);i.write(this.getBits(b,17,24));i.write(this.getBits(b,25,32));this.writeInt8(this.getMessageId(),i,true);i.write(0);i.write(this.channelID);i.write(0);i.write(0);this.writeInt8((g.byteLength?g.byteLength:g.length),i,false);if(b>0){i.write(this.authData)}i.write(h?g:String.fromUTF8ByteArray(g))};Trap.Message.prototype.serialize7bit=function(g,f){if(this.data.length>=Math.pow(2,28)){throw"Asked to serialize more than 2^28 bytes data into a 7-bit Trap message"}var h=0;h|=this.op;g.write(h);var b=(this.authData!=null?this.authData.length:0);g.write(this.getBits(b,17,18));g.write(this.getBits(b,19,25));g.write(this.getBits(b,26,32));this.writeInt7(this.getMessageId(),g,true);this.writeInt7(0,g,true);this.writeInt7((this.data.byteLength?this.data.byteLength:this.data.length),g,false);if(b>0){g.write(this.authData)}g.write(f?this.data:String.fromUTF8ByteArray(this.data))};Trap.Message.prototype.deserialize=function(j,k,m){if((k+m)>j.length){throw"Offset and length specified exceed the buffer"}if(m<16){return -1}var i;var n;if((j[k+0]&128)!=0){this.format=Trap.Message.Format.REGULAR;this.op=Trap.Message.Operation.getType(j[k+0]&63);this.compressed=(j[k+1]&128)!=0;this.channel=j[k+9]&255;i=j[k+2]<<8|j[k+3];this.messageId=j[k+4]<<24|j[k+5]<<16|j[k+6]<<8|j[k+7];n=j[k+12]<<24|j[k+13]<<16|j[k+14]<<8|j[k+15]}else{this.format=Trap.Message.Format.SEVEN_BIT_SAFE;this.op=Trap.Message.Operation.getType(j[k+0]&63);i=((j[k+1]&3)<<14)|((j[k+2]&127)<<7)|((j[k+3]&127)<<0);this.messageId=((j[k+4]&127)<<21)|((j[k+5]&127)<<14)|((j[k+6]&127)<<7)|((j[k+7]&127)<<0);n=((j[k+12]&127)<<21)|((j[k+13]&127)<<14)|((j[k+14]&127)<<7)|((j[k+15]&127)<<0);this.compressed=false;this.channel=0}var h=16+i+n;if(m<h){return -1}var l=k+16;if(i>0){this.authData=Trap.subarray(j,l,l+i);this.authData=String.utf8Decode(this.authData);l+=i}else{this.authData=null}this.data=Trap.subarray(j,l,l+n);if(this.compressed){this.data=new Zlib.Inflate(this.data).decompress()}return h};Trap.Message.prototype.length=function(){var b=16;if(this.authData!=null){b+=this.authData.toUTF8ByteArray().length}if(this.getCompressedData()!=null){b+=this.getCompressedData().byteLength||this.getCompressedData().length}return b};
New to GrepCode? Check out our FAQ X