forked from microsoft/vscode-mono-debug
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathRemoteController.cs
More file actions
472 lines (397 loc) · 16.4 KB
/
Copy pathRemoteController.cs
File metadata and controls
472 lines (397 loc) · 16.4 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
471
472
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Xml;
namespace GiderosPlayerRemote
{
public enum LogType
{
PlayerOutput,
Info,
Warning
}
public class RemoteController
{
Socket soc;
NetworkStream networkStream;
int nextSeqId = 1;
string projectFileName;
IRemoteControllerListener logger;
List<KeyValuePair<string, string>> fileList = new List<KeyValuePair<string, string>>();
Dictionary<string, KeyValuePair<DateTime, byte[]>> md5 = new Dictionary<string, KeyValuePair<DateTime, byte[]>>();
DependencyGraph dependencyGraph = new DependencyGraph();
MD5 md5calculator = MD5.Create();
ProjectProperties properties_ = new ProjectProperties();
public bool TryStart(
string addr,
int port,
string gprojPath,
IRemoteControllerListener logger)
{
this.projectFileName = gprojPath;
this.logger = logger;
if (soc != null) { soc.Dispose(); }
soc = new Socket(
AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
try
{
soc.Connect(new IPEndPoint(IPAddress.Parse(addr), 15000));
}
catch (SocketException e)
{
if (e.ErrorCode == 10061) { return false; }
else { throw; }
}
soc.ReceiveBufferSize = 1024 * 1024;
soc.SendBufferSize = 1024 * 1024;
networkStream = new NetworkStream(soc);
Play();
return true;
}
public void ReadLoop()
{
var reader = new GiderosMessageReader(soc);
while (true)
{
ReceivedGiderosMessage msg = reader.TryTakeMessageFromBuffer();
if (msg == null)
{
if (reader.ReceiveMore() == 0) { return; }
continue;
}
byte msgType = msg.ReadByte();
switch (msgType)
{
case GiderosMessageType.Output:
HandleOutput(msg);
break;
case GiderosMessageType.FileList:
HandleFileList(msg);
break;
default:
logger.X_Log(LogType.Warning, "unknown message:" + msgType);
break;
}
}
}
void HandleOutput(ReceivedGiderosMessage msg)
{
logger.X_Log(LogType.PlayerOutput, msg.ReadString());
}
void HandleFileList(ReceivedGiderosMessage msg)
{
// void Application::dataReceived(const QByteArray& d)
// in gdrdeamon/application.cpp
Queue<KeyValuePair<string, string>> fileQueue = new Queue<KeyValuePair<string, string>>();
Dictionary<string, string> localFileMap = new Dictionary<string, string>();
Dictionary<string, string> localFileMapReverse = new Dictionary<string, string>();
foreach (var e in fileList)
{
localFileMap[e.Key] = e.Value;
localFileMapReverse[e.Value] = e.Key;
}
Dictionary<string, KeyValuePair<int, byte[]>> remoteFileMap = new Dictionary<string, KeyValuePair<int, byte[]>>();
while (msg.IsEOB() == false)
{
var file = msg.ReadString();
if (file[0] == 'F')
{
int age = msg.ReadInt();
byte[] md5 = msg.ReadBytes(16);
remoteFileMap[file.Substring(1)] =
new KeyValuePair<int, byte[]>(age, md5);
}
else if (file[0] == 'D')
{
}
}
// delete unused files
foreach (var iter in remoteFileMap)
{
if (localFileMap.ContainsKey(iter.Key) == false)
{
//printf("deleting: %s\n", qPrintable(iter->first));
NewMessage(GiderosMessageType.DeleteFile)
.AppendString(iter.Key)
.Send();
logger.X_Log(LogType.Info, "delete " + iter.Key);
}
}
// upload files
string path = Path.GetDirectoryName(projectFileName);
foreach (var iter in localFileMap)
{
KeyValuePair<int, byte[]>? riter = remoteFileMap.Find(iter.Key);
string localfile = Path.Combine(path, iter.Value);
bool send = false;
if (riter.HasValue == false)
{
//printf("always upload: %s\n", qPrintable(iter->first));
send = true;
}
else
{
int localage = Util.FileAge(localfile);
int remoteage = riter.Value.Key;
byte[] localmd5 = md5[iter.Value].Value;
byte[] remotemd5 = riter.Value.Value;
if (localage < remoteage || localmd5.SequenceEqual(remotemd5) == false)
{
//printf("upload new file: %s\n", qPrintable(iter->first));
send = true;
}
}
if (send == true)
{
fileQueue.Enqueue(new KeyValuePair<string, string>(iter.Key, localfile));
}
else
{
//printf("don't upload: %s\n", qPrintable(iter->first));
}
}
List<KeyValuePair<string, bool>> topologicalSort =
dependencyGraph.TopologicalSort();
List<string> luaFilesToPlay = topologicalSort
.Where(x => x.Value == false)
.Select(x => localFileMapReverse[x.Key])
.ToList();
//---------------------------------------------------------
// 여기부터 void Application::timer()
var cfolderSent = new HashSet<string>();
while (fileQueue.Count > 0)
{
string s1 = fileQueue.Peek().Key;
string s2 = fileQueue.Peek().Value;
fileQueue.Dequeue();
// create remote directories
var dir = Path.GetDirectoryName(s1);
if (cfolderSent.Contains(dir))
{
// pass
}
else
{
cfolderSent.Add(dir);
NewMessage(GiderosMessageType.CreateFolder)
.AppendString(dir)
.Send();
//logger(LogType.Info, "cfolder " + dir);
}
string fileName = Path.Combine(path, s2);
try
{
byte[] bytes = File.ReadAllBytes(fileName);
NewMessage(GiderosMessageType.File)
.AppendString(s1)
.AppendByteArray(bytes)
.Send();
logger.X_Log(LogType.Info, "send " + s1);
}
catch (FileNotFoundException)
{
// md5 계산에서 이미 경고를 냈으므로
// 여기에선 무시한다
}
}
logger.X_Log(LogType.Info, "Uploading finished.");
SendProjectProperties();
//-----------------------------------------------
var playMsg = NewMessage(GiderosMessageType.Play);
foreach (string f in luaFilesToPlay)
{
//logger(LogType.Info, "play " + f);
playMsg.AppendString(f);
}
playMsg.Send();
}
void SendProjectProperties()
{
var m = NewMessage(GiderosMessageType.SendProjectProperties);
m.AppendInt(properties_.scaleMode);
m.AppendInt(properties_.logicalWidth);
m.AppendInt(properties_.logicalHeight);
m.AppendInt((int)properties_.imageScales.Count);
foreach (var s in properties_.imageScales)
{
m.AppendString(s.Key);
m.AppendFloat((float)s.Value);
}
m.AppendInt(properties_.orientation);
m.AppendInt(properties_.fps);
m.AppendInt(properties_.retinaDisplay);
m.AppendInt(properties_.autorotation);
m.AppendInt(properties_.mouseToTouch ? 1 : 0);
m.AppendInt(properties_.touchToMouse ? 1 : 0);
m.AppendInt(properties_.mouseTouchOrder);
m.AppendInt(properties_.windowWidth);
m.AppendInt(properties_.windowHeight);
m.Send();
}
void Play()
{
XmlDocument doc = new XmlDocument();
doc.Load(projectFileName);
// read properties
{
var root = doc.DocumentElement;
properties_.Clear();
var properties = (XmlElement)root.SelectSingleNode("properties");
// graphics options
if (properties.HasAttribute("scaleMode"))
properties_.scaleMode = int.Parse(properties.GetAttribute("scaleMode"));
if (properties.HasAttribute("logicalWidth"))
properties_.logicalWidth = int.Parse(properties.GetAttribute("logicalWidth"));
if (properties.HasAttribute("logicalHeight"))
properties_.logicalHeight = int.Parse(properties.GetAttribute("logicalHeight"));
var imageScales = properties.SelectSingleNode("imageScales");
foreach (var n in imageScales.ChildNodes)
{
var scale = (XmlElement)n;
properties_.imageScales.Add(
new KeyValuePair<string, double>(
scale.GetAttribute("suffix"),
double.Parse(scale.GetAttribute("scale"))));
}
if (properties.HasAttribute("orientation"))
properties_.orientation = int.Parse(properties.GetAttribute("orientation"));
if (properties.HasAttribute("fps"))
properties_.fps = int.Parse(properties.GetAttribute("fps"));
// iOS options
if (properties.HasAttribute("retinaDisplay"))
properties_.retinaDisplay = int.Parse(properties.GetAttribute("retinaDisplay"));
if (properties.HasAttribute("autorotation"))
properties_.autorotation = int.Parse(properties.GetAttribute("autorotation"));
// export options
if (properties.HasAttribute("architecture"))
properties_.architecture = int.Parse(properties.GetAttribute("architecture"));
if (properties.HasAttribute("exportMode"))
properties_.exportMode = int.Parse(properties.GetAttribute("exportMode"));
if (properties.HasAttribute("iosDevice"))
properties_.iosDevice = int.Parse(properties.GetAttribute("iosDevice"));
if (properties.HasAttribute("packageName"))
properties_.packageName = properties.GetAttribute("packageName");
}
// populate file list and dependency graph
{
fileList.Clear();
dependencyGraph.Clear();
var dependencies = new List<KeyValuePair<string, string>>();
var stack = new Stack<XmlElement>();
stack.Push(doc.DocumentElement);
var dir = new List<string>();
while (stack.Count > 0)
{
XmlElement e = stack.Pop();
if (e == null)
{
dir.RemoveAt(dir.Count - 1);
continue;
}
string type = e.Name;
if (type == "file")
{
string fileName = e.HasAttribute("source")
? e.GetAttribute("source")
: e.GetAttribute("file");
string name = Path.GetFileName(fileName);
string n = "";
foreach (string d in dir)
n += d + "/";
n += name;
fileList.Add(new KeyValuePair<string, string>(n, fileName));
if (fileName.ToLower().EndsWith(".lua"))
{
bool excludeFromExecution =
(e.HasAttribute("excludeFromExecution")) &&
(int.Parse(e.GetAttribute("excludeFromExecution")) != 0);
dependencyGraph.AddCode(fileName, excludeFromExecution);
}
continue;
}
if (type == "folder")
{
string name = e.GetAttribute("name");
dir.Add(name);
string n = "";
foreach (string d in dir)
n += d + "/";
stack.Push(null);
}
if (type == "dependency")
{
string from = e.GetAttribute("from");
string to = e.GetAttribute("to");
dependencies.Add(new KeyValuePair<string, string>(from, to));
}
var childNodes = e.ChildNodes;
foreach (var c in childNodes)
stack.Push((XmlElement)c);
}
foreach (var d in dependencies)
dependencyGraph.AddDependency(d.Key, d.Value);
}
UpdateMD5();
SendStop();
NewMessage(GiderosMessageType.SetProjectName)
.AppendString(Path.GetFileNameWithoutExtension(projectFileName))
.Send();
NewMessage(GiderosMessageType.SendFileList)
.Send();
}
public void SendStop()
{
NewMessage(GiderosMessageType.Stop)
.Send();
}
void UpdateMD5()
{
//var begin = DateTime.Now;
// .tmp/~.md5 파일 포맷이 QDataStream에 의존하기 때문에
// 역공학하기 귀찮아서 .md5 캐싱 구현 스킵함.
// 요즘 PC에서 md5 전체 재계산하는거 얼마나 느린지 모르겠는데
// 일단 캐시 없이 구현해서 듀얼 정도 스케일에서 계산 돌려보고 느리면 캐싱하는 걸로..
string path = Path.GetDirectoryName(projectFileName);
foreach (var f in fileList)
{
var filename = f.Value;
var absfilename = Path.Combine(path, filename);
DateTime mtime = File.GetLastWriteTime(absfilename);
//if (iter == md5_.end() || mtime != iter.value().first)
{
md5[filename] = new KeyValuePair<DateTime, byte[]>(
mtime,
MD5FromFile(absfilename));
}
}
//logger("md5 time: " + (DateTime.Now - begin).TotalSeconds.ToString());
}
byte[] MD5FromFile(string path)
{
try
{
using (var stream = File.OpenRead(path))
{
return md5calculator.ComputeHash(stream);
}
}
catch (FileNotFoundException)
{
logger.X_Log(LogType.Warning, "Fild not found: " + path);
return new byte[16];
}
}
GiderosMessageToSend NewMessage(byte ty)
{
var rv = new GiderosMessageToSend(nextSeqId++, networkStream);
rv.AppendByte(ty);
return rv;
}
}
}