-
Notifications
You must be signed in to change notification settings - Fork 2
/
MGSocket.as
343 lines (292 loc) · 9.26 KB
/
MGSocket.as
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package {
import flash.display.MovieClip;
//import some stuff from the valve lib
import ValveLib.Globals;
import flash.net.Socket;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.net.LocalConnection;
import com.adobe.serialization.json.JSONEncoder;
import com.adobe.serialization.json.JSONDecoder;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import com.adobe.crypto.SHA256;
public class MGSocket extends EventDispatcher{
public static const PING:uint = 0;
public static const PING_NOCLOSE:uint = 1;
public static const SYSTEM_JSON:uint = 2;
public static const SYSTEM_BINARY:uint = 3;
public static const GAME_JSON:uint = 4;
public static const GAME_BINARY:uint = 5;
public static const BIG_JSON:uint = 0xFE00;
public static const BIG_BINARY:uint = 0xFF00;
public static const ROLE_USER:int = 0;
public static const ROLE_MODERATOR:int = 2;
public static const ROLE_OWNER:int = 3;
public static const ROLE_ADMIN:int = 4;
public var socket:Socket;
public var socketReady:Boolean = false;
public var socketInit:Boolean = false;
private var pingTimer:Timer;
private var id:int = -1;
private var userName:String = "";
private var authToken:String = null;
private var version:Number = 0;
public var curObj:Object = null;
private var sendQueue:Array = new Array();
private var curBytes:ByteArray = new ByteArray();
private var curType:uint = 0;
private var curLen:uint = 0;
private var newPacket:Boolean = true;
private var remaining:int = 4;
private var curPing:Number = -1;
private var pingSendTime:Number = 0;
private var nonceStr:String = null;
private var lastPacket:Number;
public function MGSocket() : void {
trace("[MGSocket] MGSocket Constructed!");
socket = new Socket();
}
public function connect(ip:String, port:int, id:int, userName:String, version:Number, authToken:String = null){
trace("[MGSocket] Connecting to " + ip + ":" + port);
this.id = id;
this.userName = userName;
this.version = version;
this.authToken = authToken;
socket.addEventListener(Event.CONNECT, socketConnect);
socket.addEventListener(ProgressEvent.SOCKET_DATA, dataRead);
socket.connect(ip, port);
lastPacket = new Date().time;
}
public function close(){
socket.removeEventListener(Event.CONNECT, socketConnect);
socket.removeEventListener(ProgressEvent.SOCKET_DATA, dataRead);
try{
socket.close();
}catch(e:Error){
trace(e);
}
socketReady = false;
if (pingTimer != null){
pingTimer.stop();
pingTimer.removeEventListener(TimerEvent.TIMER, timerPing);
pingTimer = null;
}
}
private function socketConnect(e:Event){
trace("[MGSocket] CONNECTED");
socketReady = true;
socket.removeEventListener(Event.CONNECT, socketConnect);
writeJSON({type:"connect", userID:id, userName:userName, version:version}, SYSTEM_JSON);
write(new ByteArray(), PING_NOCLOSE);
pingTimer = new Timer(20000,0);
pingTimer.addEventListener(TimerEvent.TIMER, timerPing);
pingTimer.start();
dispatchEvent(e);
}
private function timerPing(e:TimerEvent){
if (socketReady){
var now = new Date().time;
if (now - lastPacket <= 40000)
write(new ByteArray(), PING_NOCLOSE);
else{
dispatchEvent(new MGEvent(MGEvent.CLOSED, {type:"close", error:"Connection not responding. The server may be down or unreachable currently."}, SYSTEM_JSON));
pingTimer.stop();
pingTimer.removeEventListener(TimerEvent.TIMER, timerPing);
pingTimer = null;
}
}
else if (pingTimer != null){
pingTimer.stop();
pingTimer.removeEventListener(TimerEvent.TIMER, timerPing);
pingTimer = null;
}
}
public function getPing() : Number{
return curPing;
}
private function dataRead(e:Event) {
var pos:uint = 0;
var data:ByteArray = new ByteArray();
socket.readBytes(data, 0, data.bytesAvailable);
var left:uint = data.length;
//trace("Data Read: " + left);
while (pos != data.length){
//trace("pos: " + pos + " -- " + data.length);
//trace("remaining: " + remaining + " -- " + left);
if (newPacket){
//trace("header is not done");
// header is not done
if (left < remaining){
// not enough for header
curBytes.writeBytes(data, pos, data.length - pos);
remaining -= data.length;
return;
}
curBytes.writeBytes(data, pos, remaining);
pos += remaining;
left -= remaining;
curBytes.position = 0;
var header:uint = curBytes.readUnsignedInt();
//trace("header: 0x" + header.toString(16));
if (header >= 0xFE000000){
curType = (header & 0xFF000000) >>> 16;
curLen = header & 0x00FFFFFF;
}
else{
curType = (header & 0xFFFF0000) >>> 16;
curLen = header & 0x0000FFFF;
}
curBytes = new ByteArray();
remaining = curLen;
newPacket = false;
if (remaining == 0){
// packet is done
remaining = 4;
// emit
messageReceived(curType, curLen, curBytes);
curLen = 0;
curType = 0;
curBytes = new ByteArray();
newPacket = true;
}
}
else{
if (left >= remaining){
// packet is done
curBytes.writeBytes(data, pos, remaining);
pos += remaining;
left -= remaining;
remaining = 4;
// emit
curBytes.position = 0;
messageReceived(curType, curLen, curBytes);
newPacket = true;
curLen = 0;
curType = 0;
curBytes = new ByteArray();
}
else{
curBytes.writeBytes(data, pos, left);
remaining -= left;
pos += left;
}
}
}
}
private function messageReceived(type:uint, len:uint, data:ByteArray){
var json:String;
var obj:Object;
lastPacket = new Date().time;
switch(type){
case PING:
curPing = new Date().time - pingSendTime;
trace("[MGSocket] PING: " + curPing);
return;
break;
case PING_NOCLOSE:
curPing = new Date().time - pingSendTime;
trace("[MGSocket] PING_NOCLOSE: " + curPing);
return;
break;
case SYSTEM_JSON:
trace("[MGSocket] SYSTEM_JSON");
json = data.readUTFBytes(data.length);
trace("[MGSocket] " + json);
obj = new JSONDecoder(json, true).getValue();
if (obj.hasOwnProperty("type")){
switch(obj.type){
case "close":
close();
dispatchEvent(new MGEvent(MGEvent.CLOSED, obj, type));
break;
case "error":
dispatchEvent(new MGEvent(MGEvent.ERROR, obj, type));
break;
case "disconnected":
dispatchEvent(new MGEvent(MGEvent.USER_DISCONNECTED, obj, type));
break;
case "joinChannel":
dispatchEvent(new MGEvent(MGEvent.USER_JOINCHANNEL, obj, type));
break;
case "leaveChannel":
dispatchEvent(new MGEvent(MGEvent.USER_LEAVECHANNEL, obj, type));
break;
case "authSuccess":
dispatchEvent(new MGEvent(MGEvent.AUTH_SUCCESS, obj, type));
break;
case "roleChange":
dispatchEvent(new MGEvent(MGEvent.ROLE_CHANGE, obj, type));
break;
case "nonce":
nonceStr = json;
if (authToken != null){
var mac:String = SHA256.hash(authToken + nonceStr + authToken);
writeJSON({type:"auth", auth:mac}, SYSTEM_JSON);
}
break;
}
}
break;
case GAME_JSON:
trace("[MGSocket] GAME_JSON");
json = data.readUTFBytes(data.length);
trace("[MGSocket] " + json);
obj = new JSONDecoder(json, true).getValue();
dispatchEvent(new MGEvent(MGEvent.JSON, obj, type));
break;
case BIG_JSON:
trace("[MGSocket] BIG_JSON");
json = data.readUTFBytes(data.length);
trace("[MGSocket] " + json);
obj = new JSONDecoder(json, true).getValue();
dispatchEvent(new MGEvent(MGEvent.JSON, obj, type));
break;
case SYSTEM_BINARY:
trace("[MGSocket] SYSTEM_BINARY");
dispatchEvent(new MGEvent(MGEvent.BINARY, data, type));
break;
case GAME_BINARY:
trace("[MGSocket] GAME_BINARY");
dispatchEvent(new MGEvent(MGEvent.BINARY, data, type));
break;
case BIG_BINARY:
trace("[MGSocket] BIG_BINARY");
break;
default:
trace("messageReceived: 0x" + type.toString(16) + " -- " + len + " -- " + data.length);
break;
}
}
public function writeJSON(obj:Object, type:uint = GAME_JSON){
var json:String = new JSONEncoder( obj ).getString();
trace("[MGSocket] writeJSON -- " + json);
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(json);
write(ba, type);
}
public function write(ba:ByteArray, type:uint = GAME_JSON){
var header:uint = type << 16;
header += ba.length;
if (!socketReady){
trace("Socket Not Ready. Not executing the command.");
return;
}
var tba:ByteArray = new ByteArray();
tba.writeUnsignedInt(header);
tba.writeBytes(ba, 0, ba.length);
if (type == PING_NOCLOSE || type == PING){
pingSendTime = new Date().time;;
}
try{
socket.writeBytes(tba, 0, tba.length);
socket.flush();
}catch(err:Error){
trace(err);
}
}
}
}