-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetString.js
More file actions
62 lines (58 loc) · 1.74 KB
/
NetString.js
File metadata and controls
62 lines (58 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************
License: MIT
Authors: Lukas Michalski, Benjamin Zeller, Thorsten Schwalb
************************************************************************ */
qx.Class.define("pms.NetString",{
statics:
{
/******************************************************************************
* FUNCTION: readNetstring
******************************************************************************/
readNetstring : function (buffer)
{
var buflen = buffer.length;
var delim = -1;
for(var i = 0; i < buflen; i++){
var cc = buffer.charCodeAt(i);
if(cc == 0x3a){
if(i == 0){
return this.handleInvalidMessage();
}
delim = i;
break;
}
if (cc < 0x30 || cc > 0x39){
return this.handleInvalidMessage();
}
}
if(delim > 0){
var msgLenTxt = buffer.substr(0,delim);
var msgLen = parseInt(msgLenTxt);
if(msgLen === NaN){
return this.handleInvalidMessage();
}
//if the buffer is shorter than the data we need stop
if(msgLen+msgLenTxt.length+1+1 > buffer.length)
return;
if(buffer.charCodeAt(msgLenTxt.length+1+msgLen) != 0x2c){
return handleInvalidMessage();
}
var message = buffer.substr(delim+1,msgLen);
buffer = buffer.substr(msgLenTxt.length+1+msgLen+1);
return message;
}
return undefined;
},
/******************************************************************************
* FUNCTION: toNetstring
******************************************************************************/
toNetstring : function(str)
{
return str.length+":"+str+",";
},
handleInvalidMessage : function()
{
return undefined;
}
}
});