Fix some namings
This commit is contained in:
1 parent
790eda4e15
commit
37895da9b2
1 file changed
+28
-31
@@ -27,32 +27,32 @@ using System.Timers;
|
|||||||
|
|
||||||
namespace ServerLib
|
namespace ServerLib
|
||||||
{
|
{
|
||||||
public delegate void OnServerDiscovered(string IPAddress, string Hostname);
|
public delegate void OnServerDiscovered(string ipAddress, string hostname);
|
||||||
|
|
||||||
public class CDiscover
|
public class CDiscover
|
||||||
{
|
{
|
||||||
public static readonly string sTimeout = "Timeout";
|
public const string Timeout = "Timeout";
|
||||||
public static readonly string sFinished = "Finished";
|
public const string Finished = "Finished";
|
||||||
|
|
||||||
private string _Keyword;
|
private readonly string _Keyword;
|
||||||
private string _BroadcastAddress;
|
private readonly string _BroadcastAddress;
|
||||||
private int _Port;
|
private readonly int _Port;
|
||||||
|
|
||||||
private Thread _DiscoverThread;
|
private Thread _DiscoverThread;
|
||||||
private bool _DiscoverRunning;
|
private bool _DiscoverRunning;
|
||||||
private OnServerDiscovered _OnDiscovered;
|
private OnServerDiscovered _OnDiscovered;
|
||||||
|
|
||||||
private System.Timers.Timer _BroadcastTimer;
|
private readonly System.Timers.Timer _BroadcastTimer;
|
||||||
|
|
||||||
public CDiscover(int Port, string Keyword, string BroadcastAddress = "255.255.255.255")
|
public CDiscover(int port, string keyword, string broadcastAddress = "255.255.255.255")
|
||||||
{
|
{
|
||||||
_BroadcastAddress = BroadcastAddress;
|
_BroadcastAddress = broadcastAddress;
|
||||||
_Port = Port;
|
_Port = port;
|
||||||
_Keyword = Keyword;
|
_Keyword = keyword;
|
||||||
|
|
||||||
_DiscoverRunning = false;
|
_DiscoverRunning = false;
|
||||||
_BroadcastTimer = new System.Timers.Timer();
|
_BroadcastTimer = new System.Timers.Timer();
|
||||||
_BroadcastTimer.Elapsed += OnBroadcastEvent;
|
_BroadcastTimer.Elapsed += _OnBroadcastEvent;
|
||||||
_BroadcastTimer.Interval = 2500;
|
_BroadcastTimer.Interval = 2500;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,21 +67,20 @@ namespace ServerLib
|
|||||||
_DiscoverRunning = false;
|
_DiscoverRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Discover(OnServerDiscovered OnDiscovered, int Timeout = 5000)
|
public void Discover(OnServerDiscovered onDiscovered, int timeout = 5000)
|
||||||
{
|
{
|
||||||
if (_DiscoverRunning)
|
if (_DiscoverRunning)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_DiscoverRunning = true;
|
_DiscoverRunning = true;
|
||||||
_DiscoverThread = new Thread(() => _Discover(Timeout)) {Name = "Client discover"};
|
_DiscoverThread = new Thread(() => _Discover(timeout)) {Name = "Client discover"};
|
||||||
_OnDiscovered = OnDiscovered;
|
_OnDiscovered = onDiscovered;
|
||||||
_DiscoverThread.Start();
|
_DiscoverThread.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBroadcastEvent(object source, ElapsedEventArgs e)
|
private void _OnBroadcastEvent(object source, ElapsedEventArgs e)
|
||||||
{
|
{
|
||||||
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) {EnableBroadcast = true};
|
||||||
s.EnableBroadcast = true;
|
|
||||||
IPAddress broadcast = IPAddress.Parse(_BroadcastAddress);
|
IPAddress broadcast = IPAddress.Parse(_BroadcastAddress);
|
||||||
byte[] sendbuf = Encoding.UTF8.GetBytes(_Keyword);
|
byte[] sendbuf = Encoding.UTF8.GetBytes(_Keyword);
|
||||||
IPEndPoint ep = new IPEndPoint(broadcast, _Port);
|
IPEndPoint ep = new IPEndPoint(broadcast, _Port);
|
||||||
@@ -90,7 +89,7 @@ namespace ServerLib
|
|||||||
s.Close();
|
s.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void _Discover(int Timeout)
|
private void _Discover(int timeout)
|
||||||
{
|
{
|
||||||
UdpClient listener = null;
|
UdpClient listener = null;
|
||||||
Stopwatch timer = new Stopwatch();
|
Stopwatch timer = new Stopwatch();
|
||||||
@@ -98,27 +97,27 @@ namespace ServerLib
|
|||||||
List<string> knownServer = new List<string>();
|
List<string> knownServer = new List<string>();
|
||||||
bool foundSomething = false;
|
bool foundSomething = false;
|
||||||
timer.Start();
|
timer.Start();
|
||||||
while (_DiscoverRunning && timer.ElapsedMilliseconds < Timeout * 2)
|
while (_DiscoverRunning && timer.ElapsedMilliseconds < timeout * 2)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
listener = new UdpClient(_Port);
|
listener = new UdpClient(_Port);
|
||||||
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, _Port);
|
IPEndPoint groupEp = new IPEndPoint(IPAddress.Any, _Port);
|
||||||
listener.Client.ReceiveTimeout = Timeout;
|
listener.Client.ReceiveTimeout = timeout;
|
||||||
|
|
||||||
byte[] bytes = listener.Receive(ref groupEP);
|
byte[] bytes = listener.Receive(ref groupEp);
|
||||||
string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
||||||
|
|
||||||
foundSomething = true;
|
foundSomething = true;
|
||||||
if (!knownServer.Contains(groupEP.Address.ToString()))
|
if (!knownServer.Contains(groupEp.Address.ToString()))
|
||||||
{
|
{
|
||||||
knownServer.Add(groupEP.Address.ToString());
|
knownServer.Add(groupEp.Address.ToString());
|
||||||
if (_OnDiscovered != null)
|
if (_OnDiscovered != null)
|
||||||
{
|
{
|
||||||
IPHostEntry e = Dns.GetHostEntry(groupEP.Address);
|
IPHostEntry e = Dns.GetHostEntry(groupEp.Address);
|
||||||
string hostname = e.HostName;
|
string hostname = e.HostName;
|
||||||
|
|
||||||
_OnDiscovered(groupEP.Address.ToString(), hostname);
|
_OnDiscovered(groupEp.Address.ToString(), hostname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,10 +131,8 @@ namespace ServerLib
|
|||||||
}
|
}
|
||||||
_DiscoverRunning = false;
|
_DiscoverRunning = false;
|
||||||
|
|
||||||
if (!foundSomething)
|
if (_OnDiscovered != null)
|
||||||
_OnDiscovered(sTimeout, String.Empty);
|
_OnDiscovered(!foundSomething ? Timeout : Finished, String.Empty);
|
||||||
else
|
|
||||||
_OnDiscovered(sFinished, String.Empty);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user