-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathNetWorkServer.cs
More file actions
470 lines (428 loc) · 14.8 KB
/
NetWorkServer.cs
File metadata and controls
470 lines (428 loc) · 14.8 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
#########
############
#############
## ###########
### ###### #####
### ####### ####
### ########## ####
#### ########### ####
#### ########### #####
##### ### ######## #####
##### ### ######## ######
###### ### ########### ######
###### #### ############## ######
####### ##################### ######
####### ###################### ######
####### ###### ################# ######
####### ###### ###### ######### ######
####### ## ###### ###### ######
####### ###### ##### #####
###### ##### ##### ####
##### #### ##### ###
##### ### ### #
### ### ###
## ### ###
__________#_______####_______####______________
我们的未来没有BUG
* ==============================================================================
* Filename: NetWorkServer
* Created: 2018/7/13 14:29:22
* Author: エル・プサイ・コングリィ
* Purpose:
* ==============================================================================
*/
namespace MikuLuaProfiler
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEditor;
[InitializeOnLoad]
public static class StartUp
{
static bool isPlaying = false;
static StartUp()
{
#if UNITY_2017_1_OR_NEWER
EditorApplication.playModeStateChanged += (state) =>
{
if (isPlaying == true && EditorApplication.isPlaying == false)
{
NetWorkServer.Close();
}
isPlaying = EditorApplication.isPlaying;
};
#else
EditorApplication.playmodeStateChanged += () =>
{
if (isPlaying == true && EditorApplication.isPlaying == false)
{
NetWorkServer.RealClose();
}
isPlaying = EditorApplication.isPlaying;
};
#endif
}
}
public static class NetWorkServer
{
private static TcpListener tcpLister;
private static TcpClient tcpClient = null;
private static Thread receiveThread;
private static Thread sendThread;
public static Thread acceptThread;
private static NetworkStream ns;
private static BinaryReader br;
private static BinaryWriter bw;
private const int PACK_HEAD = 0x23333333;
private static Action<Sample> m_onReceiveSample;
private static Action<LuaRefInfo> m_onReceiveRef;
private static Action<LuaDiffInfo> m_onReceiveDiff;
private static Queue<int> m_cmdQueue = new Queue<int>(32);
public static bool CheckIsReceiving()
{
return tcpClient != null;
}
public static void RegisterOnReceiveSample(Action<Sample> onReceive)
{
m_onReceiveSample = onReceive;
}
public static void RegisterOnReceiveRefInfo(Action<LuaRefInfo> onReceive)
{
m_onReceiveRef = onReceive;
}
public static void RegisterOnReceiveDiffInfo(Action<LuaDiffInfo> onReceive)
{
m_onReceiveDiff = onReceive;
}
public static void BeginListen(string ip, int port)
{
if (tcpLister != null) return;
m_strCacheDict.Clear();
IPAddress myIP = IPAddress.Parse(ip);
tcpLister = new TcpListener(myIP, port);
tcpLister.Start();
acceptThread = new Thread(AcceptThread);
acceptThread.Start();
}
private static void AcceptThread()
{
UnityEngine.Debug.Log("<color=#00ff00>begin listerner</color>");
tcpClient = null;
while (true)
{
AcceptAClient();
Thread.Sleep(100);
}
}
private static void AcceptAClient()
{
if (tcpClient != null) return;
try
{
if (tcpClient == null)
{
tcpClient = tcpLister.AcceptTcpClient();
}
}
catch
{
UnityEngine.Debug.Log("<color=#ff0000>start fail</color>");
Close();
return;
}
LuaProfilerWindow.ClearTreeView();
UnityEngine.Debug.Log("<color=#00ff00>link start</color>");
tcpClient.ReceiveTimeout = 1000000;
ns = tcpClient.GetStream();
br = new BinaryReader(ns);
bw = new BinaryWriter(ns);
ns.ReadTimeout = 600000;
// 启动一个线程来接受请求
receiveThread = new Thread(DoReceiveMessage);
receiveThread.Start();
SendCmd(0);
// 启动一个线程来发送请求
sendThread = new Thread(DoSendMessage);
sendThread.Start();
}
// 0获取ref表,1 记录下当前全局表状态,2 diff 当前状态与历史记录, 3 执行完lua的gc在diff
public static void SendCmd(int cmd)
{
lock (m_cmdQueue)
{
m_cmdQueue.Enqueue(cmd);
}
}
// 接受请求
private static void DoReceiveMessage()
{
UnityEngine.Debug.Log("<color=#00ff00>begin to listener</color>");
//sign为true 循环接受数据
while (true)
{
try
{
if (tcpClient == null)
{
Close();
return;
}
if (ns.CanRead && ns.DataAvailable)
{
try
{
int head = br.ReadInt32();
//处理粘包
while (head == PACK_HEAD)
{
int messageId = br.ReadInt32();
switch (messageId)
{
case 0:
{
Sample s = Deserialize(br);
if (m_onReceiveSample != null)
{
m_onReceiveSample(s);
}
}
break;
case 1:
{
var r = DeserializeRef(br);
if (m_onReceiveRef != null)
{
m_onReceiveRef(r);
}
}
break;
case 2:
{
var r = DeserializeDiff(br);
if (m_onReceiveDiff != null)
{
m_onReceiveDiff(r);
}
}
break;
}
}
}
#pragma warning disable 0168
catch (EndOfStreamException ex)
{
Close();
return;
}
#pragma warning restore 0168
}
}
#pragma warning disable 0168
catch (ThreadAbortException e) { }
catch (Exception e)
{
Close();
}
#pragma warning restore 0168
Thread.Sleep(10);
}
}
private static void DoSendMessage()
{
while (true)
{
try
{
if (ns.CanWrite)
{
lock (m_cmdQueue)
{
while (m_cmdQueue.Count > 0)
{
int msgId = -1;
msgId = m_cmdQueue.Dequeue();
bw.Write(PACK_HEAD);
bw.Write(msgId);
}
}
}
}
#pragma warning disable 0168
catch (ThreadAbortException e) { }
catch (Exception e)
{
UnityEngine.Debug.Log(e);
Close();
}
#pragma warning restore 0168
Thread.Sleep(10);
}
}
public static void RealClose()
{
try
{
if (tcpLister != null)
{
tcpLister.Stop();
tcpLister = null;
}
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
}
UnityEngine.Debug.Log("<color=#ff0000>disconnect</color>");
if (acceptThread != null)
{
try
{
acceptThread.Abort();
}
catch { }
acceptThread = null;
}
Close();
}
public static void Close()
{
tcpClient = null;
if (receiveThread != null)
{
try
{
receiveThread.Abort();
}
catch { }
receiveThread = null;
}
if (sendThread != null)
{
try
{
sendThread.Abort();
}
catch { }
sendThread = null;
}
}
private static Dictionary<int, string> m_strCacheDict = new Dictionary<int, string>(4096);
public static Sample Deserialize(BinaryReader br)
{
Sample s = Sample.Create();
s.calls = br.ReadInt32();
s.frameCount = br.ReadInt32();
s.fps = br.ReadSingle();
s.pss = br.ReadInt64();
s.power = br.ReadSingle();
s.costLuaGC = br.ReadInt32();
s.costMonoGC = br.ReadInt32();
s.name = ReadString(br);
s.costTime = br.ReadInt32();
s.currentLuaMemory = br.ReadInt32();
s.currentMonoMemory = br.ReadInt32();
int count = br.ReadUInt16();
for (int i = 0, imax = count; i < imax; i++)
{
Deserialize(br).fahter = s;
}
int lua_gc = 0;
foreach (var item in s.childs)
{
lua_gc += item.costLuaGC;
}
s.costLuaGC = Math.Max(lua_gc, s.costLuaGC);
int mono_gc = 0;
foreach (var item in s.childs)
{
mono_gc += item.costMonoGC;
}
s.costMonoGC = Math.Max(mono_gc, s.costMonoGC);
return s;
}
public static LuaRefInfo DeserializeRef(BinaryReader br)
{
LuaRefInfo refInfo = LuaRefInfo.Create();
refInfo.cmd = br.ReadByte();
refInfo.frameCount = br.ReadInt32();
refInfo.name = ReadString(br);
refInfo.addr = ReadString(br);
refInfo.type = br.ReadByte();
return refInfo;
}
public static LuaDiffInfo DeserializeDiff(BinaryReader br)
{
LuaDiffInfo diffInfo = LuaDiffInfo.Create();
int addCount = br.ReadInt32();
for (int i = 0; i < addCount; i++)
{
diffInfo.PushAddRef(ReadString(br), br.ReadInt32());
}
int addDetailCount = br.ReadInt32();
for (int i = 0; i < addDetailCount; i++)
{
string key = ReadString(br);
int count = br.ReadInt32();
for (int ii = 0; ii < count; ii++)
{
diffInfo.PushAddDetail(key, ReadString(br));
}
}
int rmCount = br.ReadInt32();
for (int i = 0; i < rmCount; i++)
{
diffInfo.PushRmRef(ReadString(br), br.ReadInt32());
}
int rmDetailCount = br.ReadInt32();
for (int i = 0; i < rmDetailCount; i++)
{
string key = ReadString(br);
int count = br.ReadInt32();
for (int ii = 0; ii < count; ii++)
{
diffInfo.PushRmDetail(key, ReadString(br));
}
}
int nullCount = br.ReadInt32();
for (int i = 0; i < nullCount; i++)
{
diffInfo.PushNullRef(ReadString(br), br.ReadInt32());
}
int nullDetailCount = br.ReadInt32();
for (int i = 0; i < nullDetailCount; i++)
{
string key = ReadString(br);
int count = br.ReadInt32();
for (int ii = 0; ii < count; ii++)
{
diffInfo.PushNullDetail(key, ReadString(br));
}
}
return diffInfo;
}
private static string ReadString(BinaryReader br)
{
string result = null;
bool isRef = br.ReadBoolean();
int index = br.ReadInt32();
if (!isRef)
{
int len = br.ReadInt32();
byte[] datas = br.ReadBytes(len);
result = string.Intern(Encoding.UTF8.GetString(datas));
m_strCacheDict[index] = result;
}
else
{
result = m_strCacheDict[index];
}
return result;
}
}
}