added CDiscover: searching the network for running vocaluxe server, could be useful for users who do not know what a ip address is.
This commit is contained in:
1 parent
74af5ae691
commit
b18c688936
8 files changed
+192
No files matched your search
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
|
||||
namespace ClientServerLib
|
||||
{
|
||||
public delegate void OnServerDiscovered(string IPAddress, string Hostname);
|
||||
|
||||
public class CDiscover
|
||||
{
|
||||
private string _Keyword;
|
||||
private string _BroadcastAddress;
|
||||
private int _Port;
|
||||
|
||||
private Thread _DiscoverThread;
|
||||
private bool _DiscoverRunning;
|
||||
private OnServerDiscovered _OnDiscovered;
|
||||
|
||||
|
||||
private System.Timers.Timer _BroadcastTimer;
|
||||
|
||||
public CDiscover(int Port, string Keyword, string BroadcastAddress = "255.255.255.255")
|
||||
{
|
||||
_BroadcastAddress = BroadcastAddress;
|
||||
_Port = Port;
|
||||
_Keyword = Keyword;
|
||||
|
||||
_DiscoverRunning = false;
|
||||
_BroadcastTimer = new System.Timers.Timer();
|
||||
_BroadcastTimer.Elapsed += new ElapsedEventHandler(OnBroadcastEvent);
|
||||
_BroadcastTimer.Interval = 2500;
|
||||
}
|
||||
|
||||
public void StartBroadcasting()
|
||||
{
|
||||
_BroadcastTimer.Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_BroadcastTimer.Stop();
|
||||
_DiscoverRunning = false;
|
||||
}
|
||||
|
||||
public void Discover(OnServerDiscovered OnDiscovered, int Timeout = 5000)
|
||||
{
|
||||
if (_DiscoverRunning)
|
||||
return;
|
||||
|
||||
_DiscoverRunning = true;
|
||||
_DiscoverThread = new Thread(() => _Discover(Timeout));
|
||||
_OnDiscovered = OnDiscovered;
|
||||
_DiscoverThread.Start();
|
||||
}
|
||||
|
||||
private void OnBroadcastEvent(object source, ElapsedEventArgs e)
|
||||
{
|
||||
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
||||
s.EnableBroadcast = true;
|
||||
IPAddress broadcast = IPAddress.Parse(_BroadcastAddress);
|
||||
byte[] sendbuf = Encoding.UTF8.GetBytes(_Keyword);
|
||||
IPEndPoint ep = new IPEndPoint(broadcast, _Port);
|
||||
|
||||
s.SendTo(sendbuf, ep);
|
||||
s.Close();
|
||||
}
|
||||
|
||||
private void _Discover(int Timeout)
|
||||
{
|
||||
UdpClient listener = null;
|
||||
Stopwatch timer = new Stopwatch();
|
||||
|
||||
List<string> knownServer = new List<string>();
|
||||
bool foundSomething = false;
|
||||
timer.Start();
|
||||
while (_DiscoverRunning && timer.ElapsedMilliseconds < Timeout * 2)
|
||||
{
|
||||
try
|
||||
{
|
||||
listener = new UdpClient(_Port);
|
||||
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, _Port);
|
||||
listener.Client.ReceiveTimeout = Timeout;
|
||||
|
||||
byte[] bytes = listener.Receive(ref groupEP);
|
||||
string message = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
|
||||
|
||||
foundSomething = true;
|
||||
if (!knownServer.Contains(groupEP.Address.ToString()))
|
||||
{
|
||||
knownServer.Add(groupEP.Address.ToString());
|
||||
if (_OnDiscovered != null)
|
||||
{
|
||||
IPHostEntry e = Dns.GetHostEntry(groupEP.Address);
|
||||
string hostname = e.HostName;
|
||||
|
||||
_OnDiscovered(groupEP.Address.ToString(), hostname);
|
||||
}
|
||||
}
|
||||
|
||||
listener.Close();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (listener != null)
|
||||
listener.Close();
|
||||
}
|
||||
}
|
||||
_DiscoverRunning = false;
|
||||
|
||||
if (!foundSomething)
|
||||
_OnDiscovered("Timeout", "unknown");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="CClient.cs" />
|
||||
<Compile Include="CConnection.cs" />
|
||||
<Compile Include="CDiscover.cs" />
|
||||
<Compile Include="CServer.cs" />
|
||||
<Compile Include="DeffieHellman\BigInt.cs" />
|
||||
<Compile Include="DeffieHellman\DeffieHellman.cs" />
|
||||
|
||||
@@ -14,6 +14,8 @@ namespace Vocaluxe.Base.Server
|
||||
private static UTF8Encoding encoder = new UTF8Encoding();
|
||||
public static SHA256Managed SHA256 = new SHA256Managed();
|
||||
|
||||
public const string BroadcastKeyword = "I am a Vocaluxe Server";
|
||||
|
||||
#region Commands
|
||||
public const int ResponseOK = 1;
|
||||
public const int ResponseNOK = 2;
|
||||
|
||||
Reference in new issue
Block a user