Repository navigation
Expand file tree
/
Copy pathMessage.cs
More file actions
81 lines (69 loc) · 2.35 KB
/
Copy pathMessage.cs
File metadata and controls
81 lines (69 loc) · 2.35 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using NetworkCommsDotNet;
using NetworkCommsDotNet.Connections;
using NetworkCommsDotNet.Connections.UDP;
using NORSU.EncodeMe.Annotations;
using ProtoBuf;
namespace NORSU.EncodeMe.Network
{
[ProtoContract]
abstract class Message<T> : INotifyPropertyChanged where T : Message<T>
{
private static Dictionary<Type, string> _headers = new Dictionary<Type, string>();
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private static object _headerLock = new object();
public static string GetHeader()
{
lock (_headerLock)
{
if (_headers.ContainsKey(typeof(T)))
return _headers[typeof(T)];
var header = typeof(T).Name;
_headers.Add(typeof(T),header);
return header;
}
}
public virtual Task Send(IPEndPoint ip)
{
return Send(GetHeader(), (T)this, ip);
}
protected static async Task Send(string msgType, T message, IPEndPoint ip)
{
var sent = false;
while (!sent)
{
try
{
UDPConnection.SendObject(msgType, message, ip, NetworkComms.DefaultSendReceiveOptions, ApplicationLayerProtocolStatus.Enabled);
sent = true;
break;
}
catch (Exception ex)
{
#if __ANDROID__
await Task.Delay(100);
#else
await TaskEx.Delay(100);
#endif
}
}
}
private static IPEndPoint _broadcastEP;
public static Task Broadcast(string type, T message)
{
if (_broadcastEP == null)
_broadcastEP = new IPEndPoint(IPAddress.Broadcast, Config.ServerPort);
return Send(type, message, _broadcastEP);
}
}
}