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
|
using Hazel.UPnP;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Hazel.Udp
{
public class UdpBroadcaster : IDisposable
{
private SocketBroadcast[] socketBroadcasts;
private byte[] data;
private Action<string> logger;
///
public UdpBroadcaster(int port, Action<string> logger = null)
{
this.logger = logger;
var addresses = NetUtility.GetAddressesFromNetworkInterfaces(AddressFamily.InterNetwork);
this.socketBroadcasts = new SocketBroadcast[addresses.Count > 0 ? addresses.Count : 1];
int count = 0;
foreach (var addressInformation in addresses)
{
Socket socket = CreateSocket(new IPEndPoint(addressInformation.Address, 0));
IPAddress broadcast = NetUtility.GetBroadcastAddress(addressInformation);
this.socketBroadcasts[count] = new SocketBroadcast(socket, new IPEndPoint(broadcast, port));
count++;
}
if (count == 0)
{
Socket socket = CreateSocket(new IPEndPoint(IPAddress.Any, 0));
this.socketBroadcasts[0] = new SocketBroadcast(socket, new IPEndPoint(IPAddress.Broadcast, port));
}
}
private static Socket CreateSocket(IPEndPoint endPoint)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.EnableBroadcast = true;
socket.MulticastLoopback = false;
socket.Bind(endPoint);
return socket;
}
///
public void SetData(string data)
{
int len = UTF8Encoding.UTF8.GetByteCount(data);
this.data = new byte[len + 2];
this.data[0] = 4;
this.data[1] = 2;
UTF8Encoding.UTF8.GetBytes(data, 0, data.Length, this.data, 2);
}
///
public void Broadcast()
{
if (this.data == null)
{
return;
}
foreach (SocketBroadcast socketBroadcast in this.socketBroadcasts)
{
try
{
Socket socket = socketBroadcast.Socket;
socket.BeginSendTo(data, 0, data.Length, SocketFlags.None, socketBroadcast.Broadcast, this.FinishSendTo, socket);
}
catch (Exception e)
{
this.logger?.Invoke("BroadcastListener: " + e);
}
}
}
private void FinishSendTo(IAsyncResult evt)
{
try
{
Socket socket = (Socket)evt.AsyncState;
socket.EndSendTo(evt);
}
catch (Exception e)
{
this.logger?.Invoke("BroadcastListener: " + e);
}
}
///
public void Dispose()
{
if (this.socketBroadcasts != null)
{
foreach (SocketBroadcast socketBroadcast in this.socketBroadcasts)
{
Socket socket = socketBroadcast.Socket;
if (socket != null)
{
try { socket.Shutdown(SocketShutdown.Both); } catch { }
try { socket.Close(); } catch { }
try { socket.Dispose(); } catch { }
}
}
Array.Clear(this.socketBroadcasts, 0, this.socketBroadcasts.Length);
}
}
private struct SocketBroadcast
{
public Socket Socket;
public IPEndPoint Broadcast;
public SocketBroadcast(Socket socket, IPEndPoint broadcast)
{
Socket = socket;
Broadcast = broadcast;
}
}
}
}
|