forked from mingkly/UdpShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.xaml.cs
646 lines (624 loc) · 20.8 KB
/
App.xaml.cs
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
using MKFilePicker;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.Json;
using UdpQuickShare.Clients;
using UdpQuickShare.FileActions;
using UdpQuickShare.Pages;
using UdpQuickShare.Protocols;
using UdpQuickShare.Services;
using UdpQuickShare.ViewModels;
namespace UdpQuickShare;
public partial class App : Application
{
public static App Instance { get; private set; }
public ShareClient Client { get; private set; }
public static ObservableCollection<string> Messages { get; private set; }
public ObservableCollection<FileItem> Files { get; private set; }
public ObservableCollection<FileItem> SendingFiles { get; private set; }
public ObservableCollection<ClientMission> Missions { get; private set; }
public static IDataStore DataStore { get; private set; }
public DeviceManager DeviceManager { get; }
bool sendingOrRecieving;
public bool SendingOrRecieving
{
get => sendingOrRecieving;
set
{
if (sendingOrRecieving != value)
{
sendingOrRecieving = value;
SendingOrRecievingChanged?.Invoke(this, sendingOrRecieving);
}
}
}
public event EventHandler<bool> SendingOrRecievingChanged;
public static readonly int BufferSize = 1024;
public static readonly int UdpPort = 4321;
public static readonly int TcpPort = 4322;
public static string DeviceName => DeviceInfo.Name;
public bool AutoOpen { get; set; } =false;
public bool opened;
public App()
{
InitializeComponent();
MainPage = new AppShell();
Messages = new ObservableCollection<string>();
DataStore = new FileDataStore();
FileSaveManager.DataStore = DataStore;
var encoder = new DefaultEncoder();
var decoder = new DefaultDecoder();
LoadFileFromClient();
Client = new ShareClient(encoder, decoder, DataStore, UdpPort, TcpPort, BufferSize,Missions);
DeviceManager=new DeviceManager(DataStore, Client,this);
LoadUseDirectPath(DataStore);
DeviceManager.LoadDevice();
Client.OnMissionHandled += Client_OnMissionHandled;
Instance = this;
Client.SendingError += Client_SendingError;
}
private void Client_SendingError(object sender, ClientMission e)
{
DisplayAlert("错误", $"{e.FileName}-{e.FilePlatformPath}-{e.FilePath}");
}
protected override void OnResume()
{
base.OnResume();
opened = false;
}
void LoadUseDirectPath(IDataStore dataStore)
{
var useDirectPath = dataStore.Get<bool>("UseDirectPath");
FileManager.UseDirectPath = useDirectPath;
}
#region Messages
static void MessagesAdd(string message)
{
try
{
MainThread.BeginInvokeOnMainThread(() => Messages.Insert(0,message));
}
catch
{
//Messages.Add(message);
//Log(ex);
//Log(message);
}
}
public static void Log(string message)
{
#if DEBUG
MessagesAdd(message);
Debug.WriteLine(message);
#endif
}
public static void Log(object value)
{
#if DEBUG
DisplayAlert("", value.ToString());
MessagesAdd(value.ToString());
Debug.WriteLine(value);
#endif
}
#endregion
#region Devices
public void AddOrUpdateDevice(string deviceName, IPEndPoint ip, bool sendThis)=> DeviceManager.AddOrUpdateDevice(deviceName, ip, sendThis);
public bool DeleteDevice(IPEndPoint ip)=> DeviceManager.DeleteDevice(ip);
internal void ChooseDevice(IPEndPoint ip)=>DeviceManager.ChooseDevice(ip);
#endregion
#region MainPageAction
static FilePickerFileType[] FilePickerFileTypes = new FilePickerFileType[]
{
new FilePickerFileType(new Dictionary<DevicePlatform,IEnumerable<string>>
{
{DevicePlatform.Android,new string[]{"image/*"} },
{DevicePlatform.WinUI,new string[]{"*.png", "*.jpg", "*.jpeg", "*.webp" } }
}),
new FilePickerFileType(new Dictionary<DevicePlatform,IEnumerable<string>>
{
{DevicePlatform.Android,new string[]{"audio/*"} },
{DevicePlatform.WinUI,new string[]{ "*.mp3", "*.wav", "*.flac", "*.m4a", } }
}),
new FilePickerFileType(new Dictionary<DevicePlatform,IEnumerable<string>>
{
{DevicePlatform.Android,new string[]{"video/*"} },
{DevicePlatform.WinUI,new string[]{ "*.mp4", "*.rmvb", "*.mkv", "*.3gp", "*.wmv", "*.mov"} }
}),
new FilePickerFileType(new Dictionary<DevicePlatform,IEnumerable<string>>
{
{DevicePlatform.Android,new string[]{"*/*"} },
{DevicePlatform.WinUI,new string[]{ "" } }
}),
new FilePickerFileType(new Dictionary<DevicePlatform,IEnumerable<string>>
{
{DevicePlatform.Android,new string[]{"text/*","application/*"} },
{DevicePlatform.WinUI,new string[]{ "*.txt","*.csv", "*.lrc", "*.srt", "*.ass", } }
}),
};
static FilePickOptions MapFilePickOptions(FileType fileType)
{
var option = new FilePickOptions();
option.FileTypes = FilePickerFileTypes[(int)fileType];
return option;
}
public async Task SendFile(FileType fileType)
{
Log($"start pick file of {fileType}");
var res = await MKFilePicker.MKFilePicker.PickFileAsync(MapFilePickOptions(fileType));
if(res == null)
{
Log($"pick file cancelled");
return;
}
Log($"file [{res.FileName}] in [{res.FullPath}] picked");
var mission = new ClientMission(MissionType.WaitSending,
(uint)res.GetHashCode(),
res.FileName,
res.FullPath,
res.PlatformPath,
GetFileTypeByFileName(res.FileName),
0,
DeviceManager.SendingDeviceIP);
_ = Client.HandleMission(mission);
}
public async Task SendMultiFile()
{
Log($"start pick files");
var ress = await MKFilePicker.MKFilePicker.PickFilesAsync(MapFilePickOptions(FileType.Any));
if (ress == null)
{
Log($"pick files cancelled");
return;
}
Log($"{ress.Count()} files picked");
_ = Task.Run(async () =>
{
foreach(var res in ress)
{
var mission = new ClientMission(MissionType.WaitSending,
(uint)res.GetHashCode(),
res.FileName,
res.FullPath,
res.PlatformPath,
GetFileTypeByFileName(res.FileName),
0,
DeviceManager.SendingDeviceIP);
await Client.HandleMission(mission);
await Task.Delay(1000);
}
});
}
public Task SendText(string text)
{
Log($"start sending text :\"{text}\"");
var mission = new ClientMission(text, DeviceManager.SendingDeviceIP);
_ = Client.HandleMission(mission);
return Task.CompletedTask;
}
public Task ChooseDevice()
{
var diction = new Dictionary<string, object>
{
{ nameof(Devices), DeviceManager.Devices },
{"Client",Client }
};
return Shell.Current.GoToAsync("ChooseDevice", true, diction);
}
#endregion
#region RecievingFiles
public void RemoveRecievingFile(uint fileId)
{
var target = Missions.FirstOrDefault(m => m.FileId == fileId);
if (target!=null)
{
Client.StopRecieving(fileId);
Missions.Remove(target);
SaveFiles();
}
var target2 = Files.FirstOrDefault(s => s.FileId == fileId);
Files.Remove(target2);
Log($"recieving file {fileId}({target2.Name}) deleted");
}
public void ClearRecievingFile()
{
var targets = Missions.Where(m => !IsSendingFile(m)).ToArray();
foreach (var target in targets)
{
Missions.Remove(target);
}
SaveFiles();
Files.Clear();
Log($"recieving file cleared");
}
public async Task OpenRecievedFile(FileItem fileItem)
{
try
{
if (fileItem != null)
{
if (!string.IsNullOrEmpty(fileItem.Description))
{
if (IsUri(fileItem.Description, out var uri))
{
opened = true;
await Browser.OpenAsync(uri, BrowserLaunchMode.External);
}
opened= await FileOpener.TryOpenAsync(fileItem);
}
else
{
opened = await FileOpener.TryOpenAsync(fileItem);
}
}
}
catch
{
DisplayAlert("不支持", "");
}
}
public static Task InfoReciveFile(FileItem fileItem)
{
Log(fileItem.Path);
return Task.CompletedTask;
}
public static async Task CopyText(FileItem fileItem)
{
if(fileItem.Description!= null)
{
await Clipboard.Default.SetTextAsync(fileItem.Description);
DisplayAlert("复制成功", "");
}
}
public void StopOrResumeRecievingFile(FileItem fileItem)
{
if (fileItem.Working)
{
Client.StopRecieving(fileItem.FileId);
}
else if(!SendingOrRecieving)
{
Client.ResumeRecieving(fileItem.FileId);
}
}
public static bool IsUri(string value, out Uri uri)
{
return Uri.TryCreate(value, new UriCreationOptions()
{
DangerousDisablePathAndQueryCanonicalization = true,
}, out uri);
}
#endregion
#region SendingFiles
public void RemoveSendingFile(uint fileId)
{
var target = Missions.FirstOrDefault(m => m.FileId == fileId);
if (target != null)
{
Client.StopRecieving(fileId);
Missions.Remove(target);
SaveFiles();
}
var target2 = SendingFiles.FirstOrDefault(s => s.FileId == fileId);
SendingFiles.Remove(target2);
Log($"sending file {fileId}({target2.Name}) deleted");
}
public void ClearSendingFile()
{
var targets = Missions.Where(m => IsSendingFile(m)).ToArray();
foreach (var target in targets)
{
Missions.Remove(target);
}
SaveFiles();
SendingFiles.Clear();
Log($"sending file cleared");
}
public void StopOrResumeSendingFile(FileItem fileItem)
{
if (fileItem.Working)
{
Client.StopSending(fileItem.FileId);
}
else if (!SendingOrRecieving)
{
Client.ResumeSending(fileItem.FileId);
}
}
#endregion
void LoadFileFromClient()
{
try
{
Missions = DataStore.Get<ObservableCollection<ClientMission>>(nameof(Missions))??new ObservableCollection<ClientMission>();
}
catch
{
Missions = new ObservableCollection<ClientMission>();
}
Files = new ObservableCollection<FileItem>();
SendingFiles = new ObservableCollection<FileItem>();
if (Missions.Any())
{
foreach (var file in Missions.OrderByDescending(m=>m.CreateTime))
{
var item = ConvertToFileItem(file);
Debug.WriteLine($"{file.Position}-{item.Percent}");
if (item.IsSendingFile)
{
SendingFiles.Add(item);
}
else
{
Files.Add(item);
}
}
}
}
void SaveFiles()
{
//Log($"missions:{string.Join(",", Missions.Select(s => s.FileName))}");
DataStore.Save(nameof(Missions), Missions);
}
void TryAddMission(ClientMission mission)
{
if (Missions.FirstOrDefault(a => a.FileId == mission.FileId) == null)
{
Missions.Add(mission);
SaveFiles();
}
}
static bool IsSendingFile(ClientMission file)
{
return file.Type == MissionType.Sending ||
file.Type == MissionType.WaitSending ||
file.Type == MissionType.WaitResumeSending ||
file.Type == MissionType.SendingCompleted;
}
FileItem ConvertToFileItem(ClientMission file)
{
var isSending= IsSendingFile(file);
var item = new FileItem(this, isSending)
{
FileId = file.FileId,
Name = file.FileName,
Length = file.FileLength,
Path = file.FilePlatformPath,
CreateTime = file.CreateTime,
Percent = (double)file.Position / file.FileLength,
};
if (item.Percent == 0)
{
item.State = FileItemState.Waiting;
}
else if (item.Percent >= 1)
{
item.State = FileItemState.Completed;
}
else
{
item.State = FileItemState.Stopped;
}
return item;
}
DateTime lastUpdate;
private void Client_OnMissionHandled(object sender, ClientMission e)
{
if (e.Type == MissionType.WaitSending)
{
SendingOrRecieving = true;
var target = Files.FirstOrDefault(f => f.FileId == e.FileId);
if (target == null)
{
TryAddMission(e);
target = ConvertToFileItem(e);
lastUpdate = DateTime.Now;
MainThread.BeginInvokeOnMainThread(() =>
{
SendingFiles.Insert(0, target);
Log($"add {e.FileId} to recieving files");
});
}
}
else if(e.Type== MissionType.Sending)
{
SendingOrRecieving = true;
var target = SendingFiles.FirstOrDefault(f => f.FileId == e.FileId);
if (target != null && target.State != FileItemState.Completed)
{
target.Working = true;
target.State = FileItemState.Working;
var delta = DateTime.Now - lastUpdate;
lastUpdate = DateTime.Now;
target.Speed =
(long)((e.Position - (target.Percent * target.Length))
/
(delta.TotalSeconds));
target.Percent = (double)e.Position / e.FileLength;
Log($"update sending file prograss :percent {target.Percent},position {e.Position}");
}
}
else if (e.Type == MissionType.WaitResumeSending)
{
SendingOrRecieving = false;
var target = SendingFiles.FirstOrDefault(f => f.FileId == e.FileId);
if (target != null)
{
target.State = FileItemState.Stopped;
target.Working = false;
SaveFiles();
DisplayAlert("传输停止", $"{e.FileName}");
Log($"file {e.FileId}({e.FileName}) send stopped");
}
}
else if (e.Type == MissionType.SendingCompleted)
{
SendingOrRecieving = false;
var target = SendingFiles.FirstOrDefault(f => f.FileId == e.FileId);
if (target != null)
{
target.State = FileItemState.Completed;
if (e.Text != null)
{
target.Description = e.Text;
}
target.Path = e.FilePlatformPath;
target.Percent = 1;
DisplayAlert("发送完毕", e.FileName);
Log($"file {e.FileId}({e.FileName}) sended success");
}
else
{
TryAddMission(e);
target = ConvertToFileItem(e);
if (e.Text != null)
{
target.Description = e.Text;
}
MainThread.BeginInvokeOnMainThread(() =>
{
SendingFiles.Insert(0, target);
Log($"add {e.FileId} to sending files");
});
}
e.Position = e.FileLength;
SaveFiles();
}
else if (e.Type == MissionType.WaitRecieving)
{
SendingOrRecieving = true;
var target = Files.FirstOrDefault(f => f.FileId == e.FileId);
if (target == null)
{
TryAddMission(e);
target = ConvertToFileItem(e);
lastUpdate = DateTime.Now;
MainThread.BeginInvokeOnMainThread(() =>
{
Files.Insert(0, target);
Log($"add {e.FileId} to recieving files");
});
}
}
else if (e.Type == MissionType.Recieving)
{
SendingOrRecieving = true;
var target = Files.FirstOrDefault(f => f.FileId == e.FileId);
if (target != null && target.State != FileItemState.Completed)
{
target.Working = true;
target.State = FileItemState.Working;
var delta = DateTime.Now - lastUpdate;
lastUpdate = DateTime.Now;
target.Speed =
(long)((e.Position - (target.Percent * target.Length))
/
(delta.TotalSeconds));
target.Percent = (double)e.Position / e.FileLength;
Log($"update recieving file prograss :percent {target.Percent},position {e.Position}");
}
}
else if (e.Type == MissionType.WaitResumeRecieving)
{
SendingOrRecieving = false;
var target = Files.FirstOrDefault(f => f.FileId == e.FileId);
if (target != null)
{
target.Working = false;
target.State = FileItemState.Stopped;
SaveFiles();
Log($"file {e.FileId}({e.FileName}) recieved stopped");
DisplayAlert("传输停止", $"{e.FileName}");
}
}
else if (e.Type == MissionType.RecievingComleted)
{
SendingOrRecieving = false;
var target = Files.FirstOrDefault(f => f.FileId == e.FileId);
if (target != null)
{
target.State = FileItemState.Completed;
if (e.Text != null)
{
target.Description = e.Text;
}
target.Path = e.FilePlatformPath;
target.Percent = 1;
Log($"file {e.FileId}({e.FileName}) recieved success");
}
else
{
TryAddMission(e);
target = ConvertToFileItem(e);
if (e.Text != null)
{
target.Description = e.Text;
}
MainThread.BeginInvokeOnMainThread(() =>
{
Files.Insert(0, target);
Log($"add {e.FileId} to recieving files");
});
}
e.Position = e.FileLength;
SaveFiles();
if (AutoOpen && !opened)
{
Task.Run(() => OpenRecievedFile(target));
return;
}
if (e.Text!= null)
{
DisplayAlert("收到文本", e.Text);
}
else
{
DisplayAlert("收到文件", e.FilePath);
}
}
}
static bool alerting;
public static void DisplayAlert(string title,string message,string cancel = "确定")
{
if (alerting)
{
return;
}
MainThread.BeginInvokeOnMainThread(async () =>
{
alerting= true;
await Shell.Current.CurrentPage.DisplayAlert(title, message, cancel);
alerting= false;
});
}
protected override void OnStart()
{
base.OnStart();
Task.Run(async () =>
{
await Client.SetUp();
Client.ExposeThisDevice();
});
}
public static FileType GetFileTypeByFileName(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
return FileType.Any;
}
var extension=Path.GetExtension(fileName).ToLower().Replace(".","");
return extension switch
{
"txt" or "pdf" or "epub" or "mobi" or "excel" or "word" or "lrc" => FileType.Text,
"jpg" or "gif" or "jpeg" or "png" or "webp" => FileType.Image,
"mp3" or "wav" or "flac" => FileType.Audio,
"mp4" or "3gp" or "avi" or "mkv" or "rmvb" or "mov" or "wmv" => FileType.Video,
_ => FileType.Any,
};
}
}