blob: c37ab7f51df713bb5b92c34a7c6c84310ab52687 (
plain)
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
|
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Tango.Core;
using Tango.PMR.Discovery;
using Tango.Transport.Servers;
namespace Tango.Transport.Discovery
{
/// <summary>
/// Represents UDP discovery service broadcasting a discovery message of type <see cref="BasicDiscoveryMessage"/>.
/// </summary>
/// <seealso cref="Tango.Transport.Discovery.IDiscoveryService{Tango.PMR.Discovery.BasicDiscoveryMessage}" />
public class UdpDiscoveryService<DiscoveryMessage> : ExtendedObject, IDiscoveryService<DiscoveryMessage> where DiscoveryMessage : IMessage
{
private Timer _timer;
private TcpServer _tcpValidationServer;
/// <summary>
/// Occurs before broadcasting the discovery message and gives a chance to modify the packet message.
/// </summary>
public event EventHandler<DiscoveryMessage> BeforeBroadcasting;
/// <summary>
/// Gets or sets the current discovery message.
/// </summary>
public DiscoveryMessage CurrentDiscoveryMessage { get; set; }
/// <summary>
/// Gets or sets the interval in which the discovery message will be sent.
/// </summary>
public TimeSpan Interval { get; set; }
/// <summary>
/// Gets a value indicating whether this service has been started.
/// </summary>
public bool IsStarted { get; private set; }
/// <summary>
/// Gets the UDP port number.
/// </summary>
public int Port { get; private set; }
/// <summary>
/// Prevents a default instance of the <see cref="UdpDiscoveryService{DiscoveryMessage}"/> class from being created.
/// </summary>
private UdpDiscoveryService()
{
Interval = TimeSpan.FromSeconds(5);
CurrentDiscoveryMessage = Activator.CreateInstance<DiscoveryMessage>();
}
/// <summary>
/// Initializes a new instance of the <see cref="UdpDiscoveryService{DiscoveryMessage}"/> class.
/// </summary>
/// <param name="port">The port.</param>
public UdpDiscoveryService(int port) : this()
{
Port = port;
_tcpValidationServer = new TcpServer(Port);
_tcpValidationServer.ClientConnected += (x, e) =>
{
e.Socket.Dispose();
};
}
/// <summary>
/// Initializes a new instance of the <see cref="UdpDiscoveryService{DiscoveryMessage}"/> class.
/// </summary>
/// <param name="port">The port.</param>
/// <param name="discoveryMessage">The discovery message.</param>
public UdpDiscoveryService(int port, DiscoveryMessage discoveryMessage) : this(port)
{
CurrentDiscoveryMessage = discoveryMessage;
}
/// <summary>
/// Starts the discovery service.
/// </summary>
public void Start()
{
if (!IsStarted)
{
_tcpValidationServer.Start();
_timer = new Timer();
_timer.Interval = Interval.TotalMilliseconds;
_timer.Elapsed += _timer_Elapsed;
_timer.Enabled = true;
_timer.Start();
IsStarted = true;
}
}
/// <summary>
/// Stops the discovery service.
/// </summary>
public void Stop()
{
if (IsStarted)
{
_tcpValidationServer.Stop();
//Transmit the discovery packet one more time so clients can tell that we have disconnected.
BroadcastDiscoveryPacket();
_timer.Stop();
IsStarted = false;
}
}
/// <summary>
/// Handles the Elapsed event of the _timer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="ElapsedEventArgs"/> instance containing the event data.</param>
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
BroadcastDiscoveryPacket();
}
private void BroadcastDiscoveryPacket()
{
try
{
UdpClient client = new UdpClient();
IPEndPoint endPoint = new IPEndPoint(IPAddress.Broadcast, Port);
BeforeBroadcasting?.Invoke(this, CurrentDiscoveryMessage);
byte[] bytes = CurrentDiscoveryMessage.ToByteArray();
client.EnableBroadcast = true;
client.Send(bytes, bytes.Length, endPoint);
client.Close();
}
catch (Exception ex)
{
LogManager.Log(ex, "Error broadcasting discovery packet.");
}
}
}
}
|