forked from Trevor3000/RemoteControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmMain.cs
More file actions
2078 lines (1890 loc) · 80.6 KB
/
Copy pathFrmMain.cs
File metadata and controls
2078 lines (1890 loc) · 80.6 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using RemoteControl.Protocals;
using System.IO;
using System.Threading;
using System.Diagnostics;
using log4net;
using RemoteControl.Protocals.Plugin;
using RemoteControl.Protocals.Request;
using RemoteControl.Protocals.Response;
using RemoteControl.Audio;
using RemoteControl.Audio.Codecs;
using RemoteControl.Protocals.Utilities;
using RemoteControl.Server.Utils;
namespace RemoteControl.Server
{
public partial class FrmMain : FrmBase
{
public const string APP_TITLE = "远程控制服务端";
private static readonly ILog Logger = LogManager.GetLogger(typeof(FrmMain));
private int clientCount = 0;
private TreeNode InternetTreeNode { get { return this.treeView1.Nodes[1]; } }
private SocketSession currentSession = null;
private Dictionary<string, Action<ResponseStartGetScreen>> sessionScreenHandlers = new Dictionary<string, Action<ResponseStartGetScreen>>();
private Dictionary<string, Action<ResponseStartCaptureVideo>> sessionVideoHandlers = new Dictionary<string, Action<ResponseStartCaptureVideo>>();
private SendCommandHotKey sendCommandHotKey = SendCommandHotKey.Enter;
private WaveOut _waveOut = null;
public FrmMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = APP_TITLE;
Control.CheckForIllegalCrossThreadCalls = false;
initSkinMenus();
initIcons();
initServerEvents();
UIUtil.BindTextBoxCtrlA(this.textBoxCommandRequest);
UIUtil.BindTextBoxCtrlA(this.textBoxCommandResponse);
actChangeSkin(Settings.CurrentSettings.SkinPath);
if (WaveOut.Devices.Length > 0)
{
_waveOut = new WaveOut(WaveOut.Devices[0], 8000, 16, 1);
}
}
private void initSkinMenus()
{
RSCApplication.lstSkins = RSCApplication.GetAllSkinFiles();
if (RSCApplication.lstSkins.Count > 0)
{
int iSkinCount = RSCApplication.lstSkins.Count;
for (int i = 0; i < iSkinCount; i++)
{
string sSkinFile = RSCApplication.lstSkins[i];
string sSkinName = System.IO.Path.GetFileName(sSkinFile);
ToolStripMenuItem menuSkin = new ToolStripMenuItem(sSkinName, null, (o, e) =>
{
ToolStripMenuItem m = o as ToolStripMenuItem;
string sFile = m.Tag as string;
actChangeSkin(sFile);
});
menuSkin.Tag = sSkinFile;
this.ToolStripMenuItemSkins.DropDownItems.Add(menuSkin);
}
}
var tools = RSCApplication.GetAllTools();
if (tools.Count > 0)
{
for (int i = 0; i < tools.Count; i++)
{
string tool = tools[i];
string menuText = System.IO.Path.GetFileNameWithoutExtension(tool);
Bitmap bmp = System.Drawing.Icon.ExtractAssociatedIcon(tool).ToBitmap();
ToolStripMenuItem menuItem = new ToolStripMenuItem(menuText, bmp, (o, e) =>
{
ToolStripMenuItem m = o as ToolStripMenuItem;
string sFile = m.Tag as string;
ProcessUtil.Run(sFile, "", false);
});
menuItem.Tag = tool;
this.ToolStripMenuItemTools.DropDownItems.Add(menuItem);
}
}
Dictionary<ePathType, string> paths = new Dictionary<ePathType,string>();
paths.Add(ePathType.APP_DIR, "根目录");
paths.Add(ePathType.AVATAR_DIR,"头像目录");
paths.Add(ePathType.SKINS_DIR,"皮肤目录");
paths.Add(ePathType.TOOL_DIR,"工具目录");
foreach (var pair in paths)
{
string path = RSCApplication.GetPath(pair.Key);
string menuText = pair.Value;
ToolStripMenuItem menuItem = new ToolStripMenuItem(menuText, null, (o, e) =>
{
ToolStripMenuItem m = o as ToolStripMenuItem;
string sFile = m.Tag as string;
ProcessUtil.RunByCmdStart("explorer.exe", sFile, true);
//ProcessUtil.Run("explorer.exe", sFile, false);
});
menuItem.Tag = path;
this.ToolStripMenuItemUsualFolders.DropDownItems.Add(menuItem);
}
}
private void initIcons()
{
string sFileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\shell32.dll";
int iIconCount = Win32API.ExtractIconEx(sFileName, -1, null, null, 0);
IntPtr[] pLargeIcons = new IntPtr[iIconCount];
IntPtr[] pSmallIcons = new IntPtr[iIconCount];
Win32API.ExtractIconEx(sFileName, 0, pLargeIcons, pSmallIcons, iIconCount);
for (int i = 0; i < iIconCount; i++)
{
this.imageList1.Images.Add(Icon.FromHandle(pLargeIcons[i]));
}
Dictionary<string,View> viewDic = new Dictionary<string,View>();
viewDic.Add("大图标", View.LargeIcon);
viewDic.Add("详情", View.Details);
viewDic.Add("小图标", View.SmallIcon);
viewDic.Add("列表", View.List);
viewDic.Add("平铺", View.Tile);
this.toolStripSplitButton1.Click += (o, args) => this.toolStripSplitButton1.ShowDropDown();
foreach (var viewItem in viewDic)
{
ToolStripItem tsi = this.toolStripSplitButton1.DropDownItems.Add(viewItem.Key, null, (o, args) =>
{
ToolStripItem i = o as ToolStripItem;
View v = (View)i.Tag;
this.listView1.View = v;
});
tsi.Tag = viewItem.Value;
}
var avatars = RSCApplication.GetAllAvatarFiles();
for (int i = 0; i < avatars.Count; i++)
{
string avatarPath = avatars[i];
string avatarFileName = System.IO.Path.GetFileName(avatarPath);
this.imageList2.Images.Add(avatarFileName, Image.FromFile(avatarPath));
}
}
private void initServerEvents()
{
RSCApplication.oRemoteControlServer = new RemoteControlServer();
RSCApplication.oRemoteControlServer.ClientConnected += oRemoteControlServer_ClientConnected;
RSCApplication.oRemoteControlServer.ClientDisconnected += oRemoteControlServer_ClientDisconnected;
RSCApplication.oRemoteControlServer.PacketReceived += oRemoteControlServer_PacketReceived;
}
void oRemoteControlServer_PacketReceived(object sender, PacketReceivedEventArgs e)
{
//Console.WriteLine(e.PacketType.ToString());
ResponseBase rb = e.Obj as ResponseBase;
if (rb != null && rb.Result == false)
{
Logger.Debug(e.Session.SocketId + " Error:" + rb.Message + "\r\n" + rb.Detail);
doOutput(rb.Message);
return;
}
if (e.PacketType == ePacketType.PACKET_CLIENT_CLOSE_RESPONSE)
{
e.Session.Close();
}
else if (e.PacketType == ePacketType.PACKET_GET_HOST_NAME_RESPONSE)
{
var resp = e.Obj as ResponseGetHostName;
string hostName = resp.HostName;
e.Session.SetHostName(hostName);
e.Session.SetAppPath(resp.AppPath);
e.Session.SetOnlineAvatar(resp.OnlineAvatar);
if (this.currentSession != null &&
this.currentSession.SocketId == e.Session.SocketId)
{
// 更新主机名
this.Invoke(new Action(() =>
{
this.toolStripTextBox2.Text = hostName;
}));
}
this.Invoke(new Action(() =>
{
// 修改节点图标
TreeNode node = FindClientNode(e.Session);
if (node != null)
{
node.Text = string.Format("{0}({1})", e.Session.GetSocketIPById(), e.Session.HostName);
if (this.treeView1.ImageList.Images.ContainsKey(e.Session.OnlineAvatar))
{
node.ImageKey = e.Session.OnlineAvatar;
node.SelectedImageKey = e.Session.OnlineAvatar;
}
}
}));
}
// 过滤非当前会话
if (this.currentSession == null || e.Session.SocketId != this.currentSession.SocketId)
return;
if (e.PacketType == ePacketType.PACKET_GET_DRIVES_RESPONSE)
{
ResponseGetDrives resp = e.Obj as ResponseGetDrives;
this.UpdateUI(() =>
{
this.listView1.Items.Clear();
for (int i = 0; i < resp.drives.Count; i++)
{
string drive = resp.drives[i];
ListViewItem item = new ListViewItem(string.Concat(new object[] { drive, "", "" }), 7);
ListViewItemFileOrDirTag tag = new ListViewItemFileOrDirTag();
tag.IsFile = false;
tag.Path = drive;
item.Tag = tag;
this.listView1.Items.Add(item);
}
});
}
else if (e.PacketType == ePacketType.PACKET_GET_SUBFILES_OR_DIRS_RESPONSE)
{
ResponseGetSubFilesOrDirs resp = e.Obj as ResponseGetSubFilesOrDirs;
this.UpdateUI(() =>
{
this.listView1.Items.Clear();
for (int i = 0; i < resp.dirs.Count; i++)
{
var dirObj = resp.dirs[i];
string path = dirObj.DirPath;
string itemText = System.IO.Path.GetFileName(path);
ListViewItem item = new ListViewItem(new string[] { itemText, "", dirObj.LastWriteTime.ToString("yyyy/MM/dd HH:mm:ss"),"<文件夹>" }, 3);
ListViewItemFileOrDirTag tag = new ListViewItemFileOrDirTag();
tag.IsFile = false;
tag.Path = path;
item.Tag = tag;
this.listView1.Items.Add(item);
}
for (int i = 0; i < resp.files.Count; i++)
{
var fileObj = resp.files[i];
string path = fileObj.FilePath;
string itemText = System.IO.Path.GetFileName(path);
string extension = System.IO.Path.GetExtension(path).ToLower();
if (!this.imageList1.Images.ContainsKey(extension))
{
this.imageList1.Images.Add(extension, CommonUtil.GetIcon(extension, true));
}
ListViewItem item = new ListViewItem(new string[] { itemText, GetFileSizeDesc(fileObj.Size), fileObj.LastWriteTime.ToString("yyyy/MM/dd HH:mm:ss"), "<文件>" }, extension);
ListViewItemFileOrDirTag tag = new ListViewItemFileOrDirTag();
tag.IsFile = true;
tag.Path = path;
item.Tag = tag;
this.listView1.Items.Add(item);
}
});
}
else if (e.PacketType == ePacketType.PACKET_START_CAPTURE_SCREEN_RESPONSE)
{
if (sessionScreenHandlers.ContainsKey(e.Session.SocketId))
{
var screenHandle = sessionScreenHandlers[e.Session.SocketId];
screenHandle(e.Obj as ResponseStartGetScreen);
}
}
else if (e.PacketType == ePacketType.PACKET_START_CAPTURE_VIDEO_RESPONSE)
{
if (sessionVideoHandlers.ContainsKey(e.Session.SocketId))
{
var videoHandle = sessionVideoHandlers[e.Session.SocketId];
videoHandle(e.Obj as ResponseStartCaptureVideo);
}
}
else if (e.PacketType == ePacketType.PACKET_CREATE_FILE_OR_DIR_RESPONSE)
{
ResponseCreateFileOrDir resp = e.Obj as ResponseCreateFileOrDir;
if (resp.Result == false)
{
doOutput(resp.Path + "创建失败," + resp.Path);
}
string path = resp.Path;
string itemText = System.IO.Path.GetFileName(path);
ListViewItem item = new ListViewItem(string.Concat(new object[] { itemText, "", "" }), resp.PathType == Protocals.ePathType.File ? 152 : 3);
ListViewItemFileOrDirTag tag = new ListViewItemFileOrDirTag();
tag.IsFile = resp.PathType == Protocals.ePathType.File;
tag.Path = path;
item.Tag = tag;
this.listView1.Items.Add(item);
}
else if (e.PacketType == ePacketType.PACKET_DELETE_FILE_OR_DIR_RESPONSE)
{
ResponseDeleteFileOrDir resp = e.Obj as ResponseDeleteFileOrDir;
if (resp.Result == false)
{
doOutput(resp.Path + "删除失败," + resp.Path);
}
for (int i = this.listView1.Items.Count - 1; i >= 0; i--)
{
var tag = this.listView1.Items[i].Tag as ListViewItemFileOrDirTag;
if (resp.Path == tag.Path)
{
this.listView1.Items.RemoveAt(i);
}
}
}
else if (e.PacketType == ePacketType.PACKET_START_DOWNLOAD_HEADER_RESPONSE)
{
ResponseStartDownloadHeader downloadHeader = e.Obj as ResponseStartDownloadHeader;
string fileName = System.IO.Path.GetFileName(downloadHeader.Path);
this.DownloadHeader = downloadHeader;
// 处理资源释放
if (this.downloadFileStream != null)
{
this.downloadFileStream.Close();
this.downloadFileStream = null;
}
this.recvSize = 0;
this.UpdateDownloadProgressAction = null;
new Thread(() =>
{
var frm = new FrmDownload(() =>
{
// 处理资源释放
if (this.downloadFileStream != null)
{
this.downloadFileStream.Close();
this.downloadFileStream = null;
}
this.recvSize = 0;
this.UpdateDownloadProgressAction = null;
// 发送终止下载请求
this.currentSession.Send(ePacketType.PACKET_STOP_DOWNLOAD_REQUEST, null);
}, downloadHeader.Path, downloadHeader.SavePath, downloadHeader.FileSize);
this.DownloadWindow = frm;
this.UpdateDownloadProgressAction = frm.UpdateProgress;
frm.ShowDialog();
}) { IsBackground = true }.Start();
}
else if (e.PacketType == ePacketType.PACKET_START_DOWNLOAD_RESPONSE)
{
ResponseStartDownload resp = e.Obj as ResponseStartDownload;
try
{
string localFull = this.DownloadHeader.SavePath;
if (!System.IO.File.Exists(localFull))
{
System.IO.File.Create(localFull).Close();
}
byte[] data = resp.Data;
if (downloadFileStream == null)
{
downloadFileStream = new FileStream(localFull, FileMode.Open, FileAccess.Write);
}
downloadFileStream.Write(data, 0, data.Length);
this.recvSize += data.Length;
// 显示进度
if (this.DownloadWindow!=null)
{
this.DownloadWindow.UpdateProgress(this.recvSize);
}
// 下载完成
if (this.recvSize == this.DownloadHeader.FileSize)
{
this.DownloadWindow.Close();
// 处理资源释放
if (this.downloadFileStream != null)
{
this.downloadFileStream.Close();
this.downloadFileStream = null;
}
this.recvSize = 0;
this.UpdateDownloadProgressAction = null;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
else if (e.PacketType == ePacketType.PACKET_COMMAND_RESPONSE)
{
ResponseCommand resp = e.Obj as ResponseCommand;
if(resp.Result ==false)
return;
this.textBoxCommandResponse.AppendText(resp.CommandResponse + "\r\n");
}
else if (e.PacketType == ePacketType.PACKET_GET_PROCESSES_RESPONSE)
{
ResponseGetProcesses resp = e.Obj as ResponseGetProcesses;
new Thread(() =>
{
UpdateProcessListView(resp);
}) { IsBackground=true }.Start();
}
else if (e.PacketType == ePacketType.PACKET_COPY_FILE_OR_DIR_RESPONSE)
{
var resp = e.Obj as ResponseCopyFile;
doOutput("复制" + resp.SourceFile + "成功!");
}
else if (e.PacketType == ePacketType.PACKET_MOVE_FILE_OR_DIR_RESPONSE)
{
var resp = e.Obj as ResponseMoveFile;
doOutput("移动" + resp.SourceFile + "成功!");
}
else if (e.PacketType == ePacketType.PACKET_VIEW_REGISTRY_KEY_RESPONSE)
{
// 查看注册表项
var resp = e.Obj as ResponseViewRegistryKey;
this.UpdateUI(() =>
{
try
{
// 清除右侧value值列表
this.listView2.Items.Clear();
if (resp.KeyNames != null)
{
TreeView tv = this.treeView2;
// 查找根节点
TreeNode rootNode = null;
for (int j = 0; j < tv.Nodes[0].Nodes.Count; j++)
{
TreeNode node = tv.Nodes[0].Nodes[j];
string str = node.Tag.ToString();
eRegistryHive erh = (eRegistryHive)Enum.Parse(typeof(eRegistryHive), str);
if (erh == resp.KeyRoot)
{
rootNode = node;
break;
}
}
if (rootNode == null)
{
doOutput("未找到Registry根节点");
return;
}
TreeNode curNode = rootNode;
if (resp.KeyPath != null)
{
// 查找目标的节点
string[] keyNames = resp.KeyPath.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < keyNames.Length; i++)
{
var keyName = keyNames[i];
var found = false;
for (int k = 0; k < curNode.Nodes.Count; k++)
{
var node = curNode.Nodes[k];
if (node.Text == keyName)
{
found = true;
curNode = node;
break;
}
}
if (!found)
{
TreeNode node = new TreeNode(keyName, 0, 0);
List<string> curKeys = new List<string>();
for (int ii = 0; ii <= i; ii++)
{
curKeys.Add(keyNames[ii]);
}
node.Tag = new RequestViewRegistryKey() {
KeyRoot = resp.KeyRoot,
KeyPath = string.Join("\\",curKeys)
};
curNode.Nodes.Add(node);
curNode = node;
}
}
}
// 清除目标节点的子节点
curNode.Nodes.Clear();
// 重新添加目标节点的子节点
for (int i = 0; i < resp.KeyNames.Length; i++)
{
string keyName = resp.KeyNames[i];
TreeNode node = new TreeNode(keyName, 0, 0);
string newKeyPath = resp.KeyPath + @"\" + keyName;
newKeyPath = newKeyPath.TrimStart('\\');
node.Tag = new RequestViewRegistryKey(){
KeyRoot = resp.KeyRoot,
KeyPath = newKeyPath
};
curNode.Nodes.Add(node);
}
curNode.Expand();
tv.SelectedNode = curNode;
this.listView2.Tag = curNode.Tag;
this.textBoxRegistryPath.Text = "计算机\\" + resp.KeyRoot + "\\" + resp.KeyPath;
}
if (resp.ValueNames != null)
{
// 添加右侧value值列表
int valueNameLen = resp.ValueNames.Length;
for (int i = 0; i < valueNameLen; i++)
{
ListViewItem item = new ListViewItem(new string[]{
resp.ValueNames[i],
resp.ValueKinds[i].ToString(),
resp.Values[i].ToString()
},resp.ValueKinds[i].ToString());
this.listView2.Items.Add(item);
}
}
}
catch (Exception ex)
{
Logger.Error("", ex);
}
});
}
else if (e.PacketType == ePacketType.PACKET_START_CAPTURE_AUDIO_RESPONSE)
{
var resp = e.Obj as ResponseStartCaptureAudio;
if (_waveOut != null)
{
byte[] decodedData = G711.Decode_aLaw(resp.AudioData, 0, resp.AudioData.Length);
_waveOut.Play(decodedData, 0, decodedData.Length);
}
}
}
private System.IO.FileStream downloadFileStream;
private long recvSize = 0;
private ResponseStartDownloadHeader DownloadHeader;
private FrmDownload DownloadWindow;
private Action<long> UpdateDownloadProgressAction;
void oRemoteControlServer_ClientDisconnected(object sender, ClientConnectedEventArgs e)
{
RemoveClient(e.Client);
}
void oRemoteControlServer_ClientConnected(object sender, ClientConnectedEventArgs e)
{
AddClient(e.Client);
}
private void UpdateProcessListView(ResponseGetProcesses resp)
{
if (resp.Result == false)
return;
if (this.InvokeRequired)
{
this.Invoke(new Action<ResponseGetProcesses>(UpdateProcessListView), resp);
return;
}
this.listView3.Items.Clear();
for (int i = 0; i < resp.Processes.Count; i++)
{
var property = resp.Processes[i];
ListViewItem item = new ListViewItem(property.ProcessName);
item.SubItems.Add(property.PID.ToString());
item.SubItems.Add(property.User);
item.SubItems.Add(property.CPURate.ToString());
item.SubItems.Add(GetFileSizeDesc((long)(property.PrivateMemory)));
item.SubItems.Add(property.ThreadCount.ToString());
item.SubItems.Add(property.ExecutablePath);
item.SubItems.Add(property.FileDescription);
item.SubItems.Add(property.CommandLine);
this.listView3.Items.Add(item);
}
}
private void AddClient(SocketSession oClient)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<SocketSession>(AddClient), oClient);
return;
}
TreeNode treeNode = new TreeNode(oClient.GetSocketIPById());
treeNode.Tag = oClient;
treeNode.ImageKey = "qq";
treeNode.SelectedImageKey = "qq";
this.InternetTreeNode.Nodes.Add(treeNode);
this.clientCount++;
refreshClientCountShow();
doOutput(oClient.SocketId.ToString() + " 上线了!");
}
private TreeNode FindClientNode(SocketSession oClient)
{
for (int i = this.InternetTreeNode.Nodes.Count - 1; i >= 0; i--)
{
TreeNode node = this.InternetTreeNode.Nodes[i];
SocketSession session = node.Tag as SocketSession;
if (session != null && session.SocketId == oClient.SocketId)
{
return node;
}
}
return null;
}
private void RemoveClient(SocketSession oClient)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<SocketSession>(RemoveClient), oClient);
return;
}
for (int i = this.InternetTreeNode.Nodes.Count - 1; i >= 0; i--)
{
TreeNode node = this.InternetTreeNode.Nodes[i];
SocketSession session = node.Tag as SocketSession;
if (session != null && session.SocketId == oClient.SocketId)
{
this.InternetTreeNode.Nodes.RemoveAt(i);
}
}
this.currentSession = null;
this.toolStripTextBox1.Clear();
this.toolStripTextBox2.Clear();
this.clientCount--;
refreshClientCountShow();
doOutput(oClient.SocketId.ToString() + " 下线了!");
}
private void actChangeSkin(string sSkinFile)
{
//this.skiActive = false;
this.skinEngine1.SkinFile = sSkinFile;
Settings.CurrentSettings.SkinPath = sSkinFile;
if (this.ToolStripMenuItemSkins != null)
{
for (int j = 0; j < this.ToolStripMenuItemSkins.DropDownItems.Count; j++)
{
var item = this.ToolStripMenuItemSkins.DropDownItems[j] as ToolStripMenuItem;
if (this.ToolStripMenuItemSkins.DropDownItems[j].Tag.ToString() == Settings.CurrentSettings.SkinPath)
{
item.Checked = true;
}
else
{
item.Checked = false;
}
}
}
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
ToolStripButton tsButton = sender as ToolStripButton;
tsButton.Checked = !tsButton.Checked;
if (tsButton.Checked)
{
List<string> ips = RSCApplication.GetLocalIPV4s();
ips.Add("127.0.0.1"); // 支持127.0.0.1作为服务器ip
int iServerPort = Settings.CurrentSettings.ServerPort;
RSCApplication.oRemoteControlServer.Start(ips, iServerPort);
this.Text = APP_TITLE + " " + string.Join(",", ips.ToArray());
doOutput("已开启自动上线服务,端口:" + iServerPort);
}
else
{
RSCApplication.oRemoteControlServer.Stop();
this.Text = APP_TITLE;
doOutput("已停止自动上线服务!");
this.clientCount = 0;
refreshClientCountShow();
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
SocketSession session = e.Node.Tag as SocketSession;
if (session != null)
{
var mousePos = Control.MousePosition;
var tv = sender as TreeView;
var loc = tv.PointToClient(mousePos);
loc.Offset(10, 0);
this.toolTip1.Show(session.HostName, tv, loc, 2000);
}
}
private void treeView1_MouseHover(object sender, EventArgs e)
{
//var mousePos = Control.MousePosition;
//var tv = sender as TreeView;
//var loc = tv.PointToClient(mousePos);
//TreeViewHitTestInfo hitTestInfo = tv.HitTest(loc);
//if (hitTestInfo != null && hitTestInfo.Node != null)
//{
// SocketSession session = hitTestInfo.Node.Tag as SocketSession;
// if (session != null)
// {
// loc.Offset(5, 0);
// this.toolTip1.Show(string.Format("{0},{1}", session.SocketId, session.HostName), tv, loc, 2000);
// }
//}
}
private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
TreeViewHitTestInfo hitTestInfo = this.treeView1.HitTest(e.Location);
if (hitTestInfo != null && hitTestInfo.Node != null)
{
SocketSession session = hitTestInfo.Node.Tag as SocketSession;
if (session != null)
{
if (session != this.currentSession) // 与当前会话不同
{
if (this.currentSession != null) // 当前会话非空,判断是否切换
{
if (MsgBox.Question("是否要切换当前连接?", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
{
return;
}
}
this.currentSession = session;
this.toolStripTextBox1.Text = session.SocketId;
this.toolStripTextBox2.Text = session.HostName;
}
session.Send(ePacketType.PACKET_GET_DRIVES_REQUEST, null);
}
else
{
this.toolStripTextBox1.Text = string.Empty;
this.toolStripTextBox2.Text = string.Empty;
}
}
}
private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hitTestInfo = this.listView1.HitTest(e.Location);
if (hitTestInfo != null && hitTestInfo.Item != null)
{
ListViewItemFileOrDirTag tag = hitTestInfo.Item.Tag as ListViewItemFileOrDirTag;
if (!tag.IsFile)
{
if (this.currentSession != null)
{
this.listView1.Tag = tag.Path;
RequestGetSubFilesOrDirs req = new RequestGetSubFilesOrDirs();
req.parentDir = tag.Path;
this.currentSession.Send(ePacketType.PACKET_GET_SUBFILES_OR_DIRS_REQUEST, req);
}
}
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (this.listView1.Tag != null)
{
string dir = this.listView1.Tag as string;
if (this.currentSession != null)
{
DirectoryInfo parentDirInfo = System.IO.Directory.GetParent(dir);
if (parentDirInfo != null)
{
string parent = parentDirInfo.FullName;
RequestGetSubFilesOrDirs req = new RequestGetSubFilesOrDirs();
req.parentDir = parent;
this.currentSession.Send(ePacketType.PACKET_GET_SUBFILES_OR_DIRS_REQUEST, req);
this.listView1.Tag = parent;
}
else
{
this.currentSession.Send(ePacketType.PACKET_GET_DRIVES_REQUEST, null);
}
}
}
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
if (this.currentSession == null)
{
MsgBox.Info("请先选择客户端!");
return;
}
var frm = new FrmCaptureScreen(this.currentSession);
string sessionId = this.currentSession.SocketId;
if (!this.sessionScreenHandlers.ContainsKey(sessionId))
{
this.sessionScreenHandlers.Add(sessionId, frm.HandleScreen);
}
else
{
this.sessionScreenHandlers[sessionId] = frm.HandleScreen;
}
frm.Show();
}
private void UpdateUI(Action action)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<Action>(UpdateUI), action);
return;
}
action();
}
private void doOutput(string sMsg)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<string>(doOutput), sMsg);
return;
}
this.richTextBox1.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + sMsg + "\r\n" + this.richTextBox1.Text;
}
private void refreshClientCountShow()
{
this.toolStripStatusLabel1.Text = "自动上线:" + this.clientCount + "台";
}
/// <summary>
/// 新建文本文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton10_Click(object sender, EventArgs e)
{
if (this.listView1.Tag == null)
{
MessageBox.Show("无法在该目录下创建文件!");
return;
}
var frm = new FrmInputFileOrDir();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
return;
if (this.currentSession != null)
{
RequestCreateFileOrDir req = new RequestCreateFileOrDir();
req.PathType = Protocals.ePathType.File;
req.Path = System.IO.Path.Combine(this.listView1.Tag.ToString(), frm.InputText);
this.currentSession.Send(ePacketType.PACKET_CREATE_FILE_OR_DIR_REQUEST, req);
}
}
/// <summary>
/// 新建文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton11_Click(object sender, EventArgs e)
{
if (this.listView1.Tag == null)
{
MessageBox.Show("无法在该目录下创建文件!");
return;
}
var frm = new FrmInputFileOrDir();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
return;
if (this.currentSession != null)
{
RequestCreateFileOrDir req = new RequestCreateFileOrDir();
req.PathType = Protocals.ePathType.Directory;
req.Path = System.IO.Path.Combine(this.listView1.Tag.ToString(), frm.InputText);
this.currentSession.Send(ePacketType.PACKET_CREATE_FILE_OR_DIR_REQUEST, req);
}
}
/// <summary>
/// 删除文件或文件件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton9_Click(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count < 1)
return;
if (MessageBox.Show("确定要删除选择项?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Cancel)
return;
if (this.currentSession != null)
{
ListViewItem selectedItem = this.listView1.SelectedItems[0];
ListViewItemFileOrDirTag tag = selectedItem.Tag as ListViewItemFileOrDirTag;
RequestDeleteFileOrDir req = new RequestDeleteFileOrDir();
req.PathType = tag.IsFile ? Protocals.ePathType.File : Protocals.ePathType.Directory;
req.Path = tag.Path;
this.currentSession.Send(ePacketType.PACKET_DELETE_FILE_OR_DIR_REQUEST, req);
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton13_Click(object sender, EventArgs e)
{
if (this.currentSession == null)
return;
string remoteFileDir = this.listView1.Tag as string;
if (remoteFileDir == null)
{
MsgBox.Info("当前目录无法上传文件!");
return;
}
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
string localFilePath = ofd.FileName;
if (remoteFileDir.EndsWith("\\"))
remoteFileDir = remoteFileDir.TrimEnd('\\');
string remoteFilePath = remoteFileDir + "\\" + System.IO.Path.GetFileName(localFilePath);
RequestStartUploadHeader req = new RequestStartUploadHeader();
req.From = localFilePath;
req.To = remoteFilePath;
string fileId = Guid.NewGuid().ToString();
req.Id = fileId;
this.currentSession.Send(ePacketType.PACKET_START_UPLOAD_HEADER_REQUEST, req);
FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
uploadDic.Add(fileId, fs);
long fileSize = fs.Length;
var frm = new FrmDownload(() =>
{
RequestStopUpload reqStop = new RequestStopUpload();
reqStop.Id = fileId;
this.currentSession.Send(ePacketType.PACKET_STOP_UPLOAD_REQUEST, reqStop);
}, localFilePath, remoteFilePath, fileSize);
uploadFrmDic.Add(fileId, frm);
frm.Text = "上传文件";
new Thread(() =>
{
frm.ShowDialog();
}) { IsBackground = true }.Start();
new Thread(() => { DoUploadFileInternal(fileId); }) { IsBackground = true }.Start();
}
private Dictionary<string, FileStream> uploadDic = new Dictionary<string, FileStream>();
private Dictionary<string, FrmDownload> uploadFrmDic = new Dictionary<string, FrmDownload>();
private void DoUploadFileInternal(string fileId)
{
if (uploadDic.ContainsKey(fileId))
{
FileStream fs = uploadDic[fileId];
FrmDownload frm = uploadFrmDic[fileId];
if (fs != null)
{
byte[] buffer = new byte[2048];
int totalSize = 0;
while (true)
{
int size = fs.Read(buffer, 0, buffer.Length);
if (size < 1)
break;
if (!uploadDic.ContainsKey(fileId))
{
break;
}
byte[] data = new byte[size];
for (int i = 0; i < size; i++)
{
data[i] = buffer[i];
}
ResponseStartUpload resp = new ResponseStartUpload();
resp.Id = fileId;
resp.Data = data;