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
|
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace Hazel.Udp
{
public class BroadcastPacket
{
public string Data;
public DateTime ReceiveTime;
public IPEndPoint Sender;
public BroadcastPacket(string data, IPEndPoint sender)
{
this.Data = data;
this.Sender = sender;
this.ReceiveTime = DateTime.Now;
}
public string GetAddress()
{
return this.Sender.Address.ToString();
}
}
public class UdpBroadcastListener : IDisposable
{
private Socket socket;
private EndPoint endpoint;
private Action<string> logger;
private byte[] buffer = new byte[1024];
private List<BroadcastPacket> packets = new List<BroadcastPacket>();
public bool Running { get; private set; }
///
public UdpBroadcastListener(int port, Action<string> logger = null)
{
this.logger = logger;
this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
this.socket.EnableBroadcast = true;
this.socket.MulticastLoopback = false;
this.endpoint = new IPEndPoint(IPAddress.Any, port);
this.socket.Bind(this.endpoint);
}
///
public void StartListen()
{
if (this.Running) return;
this.Running = true;
try
{
EndPoint endpt = new IPEndPoint(IPAddress.Any, 0);
this.socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref endpt, this.HandleData, null);
}
catch (NullReferenceException) { }
catch (Exception e)
{
this.logger?.Invoke("BroadcastListener: " + e);
this.Dispose();
}
}
private void HandleData(IAsyncResult result)
{
this.Running = false;
int numBytes;
EndPoint endpt = new IPEndPoint(IPAddress.Any, 0);
try
{
numBytes = this.socket.EndReceiveFrom(result, ref endpt);
}
catch (NullReferenceException)
{
// Already disposed
return;
}
catch (Exception e)
{
this.logger?.Invoke("BroadcastListener: " + e);
this.Dispose();
return;
}
if (numBytes < 3
|| buffer[0] != 4 || buffer[1] != 2)
{
this.StartListen();
return;
}
IPEndPoint ipEnd = (IPEndPoint)endpt;
string data = UTF8Encoding.UTF8.GetString(buffer, 2, numBytes - 2);
int dataHash = data.GetHashCode();
lock (packets)
{
bool found = false;
for (int i = 0; i < this.packets.Count; ++i)
{
var pkt = this.packets[i];
if (pkt == null || pkt.Data == null)
{
this.packets.RemoveAt(i);
i--;
continue;
}
if (pkt.Data.GetHashCode() == dataHash
&& pkt.Sender.Equals(ipEnd))
{
this.packets[i].ReceiveTime = DateTime.Now;
break;
}
}
if (!found)
{
this.packets.Add(new BroadcastPacket(data, ipEnd));
}
}
this.StartListen();
}
///
public BroadcastPacket[] GetPackets()
{
lock (this.packets)
{
var output = this.packets.ToArray();
this.packets.Clear();
return output;
}
}
///
public void Dispose()
{
if (this.socket != null)
{
try { this.socket.Shutdown(SocketShutdown.Both); } catch { }
try { this.socket.Close(); } catch { }
try { this.socket.Dispose(); } catch { }
this.socket = null;
}
}
}
}
|