-
Notifications
You must be signed in to change notification settings - Fork 2
/
protocol.ts
168 lines (142 loc) · 4.89 KB
/
protocol.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Learn TypeScript:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
import Message from "./message";
const { ccclass, property } = cc._decorator;
@ccclass
export default class Protocol extends cc.Component {
static PKG_HEAD_BYTES = 4;
static MSG_FLAG_BYTES = 1;
static MSG_ROUTE_CODE_BYTES = 2;
static MSG_ID_MAX_BYTES = 5;
static MSG_ROUTE_LEN_BYTES = 1;
static MSG_ROUTE_CODE_MAX = 0xffff;
static MSG_COMPRESS_ROUTE_MASK = 0x1;
static MSG_TYPE_MASK = 0x7;
/**
* pomele client encode
* id message id;
* route message route
* msg message body
* socketio current support string
*/
static strencode(str) {
var byteArray = new Uint8Array(str.length * 3);
var offset = 0;
for (var i = 0; i < str.length; i++) {
var charCode = str.charCodeAt(i);
var codes = null;
if (charCode <= 0x7f) {
codes = [charCode];
} else if (charCode <= 0x7ff) {
codes = [0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f)];
} else {
codes = [0xe0 | (charCode >> 12), 0x80 | ((charCode & 0xfc0) >> 6), 0x80 | (charCode & 0x3f)];
}
for (var j = 0; j < codes.length; j++) {
byteArray[offset] = codes[j];
++offset;
}
}
var _buffer = new Uint8Array(offset);
Protocol.copyArray(_buffer, 0, byteArray, 0, offset);
return _buffer;
}
/**
* client decode
* msg String data
* return Message Object
*/
static strdecode(buffer) {
var bytes = new Uint8Array(buffer);
var array = [];
var offset = 0;
var charCode = 0;
var end = bytes.byteLength;
while (offset < end) {
if (bytes[offset] < 128) {
charCode = bytes[offset];
offset += 1;
} else if (bytes[offset] < 224) {
charCode = ((bytes[offset] & 0x3f) << 6) + (bytes[offset + 1] & 0x3f);
offset += 2;
} else {
charCode = ((bytes[offset] & 0x0f) << 12) + ((bytes[offset + 1] & 0x3f) << 6) + (bytes[offset + 2] & 0x3f);
offset += 3;
}
array.push(charCode);
}
return String.fromCharCode.apply(null, array);
}
static copyArray(dest, doffset, src, soffset, length) {
if ('function' === typeof src.copy) {
// Buffer
src.copy(dest, doffset, soffset, soffset + length);
} else {
// Uint8Array
for (var index = 0; index < length; index++) {
dest[doffset++] = src[soffset++];
}
}
}
static msgHasId(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_RESPONSE;
}
static msgHasRoute(type) {
return type === Message.TYPE_REQUEST || type === Message.TYPE_NOTIFY ||
type === Message.TYPE_PUSH;
}
static caculateMsgIdBytes(id) {
var len = 0;
do {
len += 1;
id >>= 7;
} while (id > 0);
return len;
}
static encodeMsgFlag(type, compressRoute, buffer, offset) {
if (type !== Message.TYPE_REQUEST && type !== Message.TYPE_NOTIFY &&
type !== Message.TYPE_RESPONSE && type !== Message.TYPE_PUSH) {
throw new Error('unkonw message type: ' + type);
}
buffer[offset] = (type << 1) | (compressRoute ? 1 : 0);
return offset + Protocol.MSG_FLAG_BYTES;
}
static encodeMsgId(id, buffer, offset) {
do {
var tmp = id % 128;
var next = Math.floor(id / 128);
if (next !== 0) {
tmp = tmp + 128;
}
buffer[offset++] = tmp;
id = next;
} while (id !== 0);
return offset;
}
static encodeMsgRoute(compressRoute, route, buffer, offset) {
if (compressRoute) {
if (route > Protocol.MSG_ROUTE_CODE_MAX) {
throw new Error('route number is overflow');
}
buffer[offset++] = (route >> 8) & 0xff;
buffer[offset++] = route & 0xff;
} else {
if (route) {
buffer[offset++] = route.length & 0xff;
Protocol.copyArray(buffer, offset, route, 0, route.length);
offset += route.length;
} else {
buffer[offset++] = 0;
}
}
return offset;
}
static encodeMsgBody(msg, buffer, offset) {
Protocol.copyArray(buffer, offset, msg, 0, msg.length);
return offset + msg.length;
}
}