1
This commit is contained in:
@@ -3,11 +3,11 @@ using System.Net;
|
||||
|
||||
namespace BITKit.Net.LAN
|
||||
{
|
||||
public interface ILANBroadcaster
|
||||
public interface ILANBroadcaster:IDisposable
|
||||
{
|
||||
int Port { get; set; }
|
||||
byte[] Buffer { get; set; }
|
||||
event Action<EndPoint, string> OnReceive;
|
||||
event Action<EndPoint, byte[]> OnReceive;
|
||||
void StartBroadcast();
|
||||
void StopBroadcast();
|
||||
void StartListen();
|
||||
|
@@ -15,22 +15,26 @@ namespace BITKit.Net.LAN
|
||||
public byte[] Buffer { get; set; } = StringHelper.GetBytes("Hello World");
|
||||
private bool _isBroadcasting;
|
||||
private bool _isListening;
|
||||
private int head;
|
||||
private int revHead;
|
||||
|
||||
private Thread _thread;
|
||||
|
||||
public UdpBasedLanBroadcaster()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public event Action<EndPoint, string> OnReceive;
|
||||
public event Action<EndPoint, byte[]> OnReceive;
|
||||
|
||||
public void StartBroadcast()
|
||||
{
|
||||
_isBroadcasting = true;
|
||||
Thread thread = new(Process)
|
||||
_thread = new(Process)
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
thread.Start();
|
||||
_thread.Start();
|
||||
BIT4Log.Log<ILANBroadcaster>($"开始广播端口{Port}");
|
||||
}
|
||||
|
||||
@@ -42,23 +46,24 @@ namespace BITKit.Net.LAN
|
||||
|
||||
public void StartListen()
|
||||
{
|
||||
if (_isListening) return;
|
||||
_isListening = true;
|
||||
var thread = new Thread(ReceiveThread)
|
||||
_thread = new Thread(ReceiveThread)
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
thread.Start();
|
||||
_thread.Start();
|
||||
BIT4Log.Log<ILANBroadcaster>($"开始监听端口:{Port}");
|
||||
}
|
||||
|
||||
public void StopListen()
|
||||
{
|
||||
if (_isListening is false) return;
|
||||
_isListening = false;
|
||||
_thread?.Abort();
|
||||
BIT4Log.Log<ILANBroadcaster>($"停止监听端口{Port}");
|
||||
}
|
||||
|
||||
|
||||
private void Process()
|
||||
public void Process()
|
||||
{
|
||||
var udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
|
||||
|
||||
@@ -68,9 +73,14 @@ namespace BITKit.Net.LAN
|
||||
//IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 8080);
|
||||
try
|
||||
{
|
||||
var tempBuffer = new byte[Buffer.Length + 1];
|
||||
tempBuffer[0] = (byte)head++;
|
||||
Buffer.CopyTo(tempBuffer, 1);
|
||||
udpClient.Send(tempBuffer, tempBuffer.Length, endpoint);
|
||||
Thread.Sleep(1000);
|
||||
while (_isBroadcasting)
|
||||
{
|
||||
udpClient.Send(Buffer, Buffer.Length, endpoint);
|
||||
udpClient.Send(tempBuffer, tempBuffer.Length, endpoint);
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
@@ -87,22 +97,44 @@ namespace BITKit.Net.LAN
|
||||
{
|
||||
var udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, Port));
|
||||
var endpoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
while (_isListening)
|
||||
while (_isListening && BITApp.CancellationToken.IsCancellationRequested is false)
|
||||
{
|
||||
var buf = udpClient.Receive(ref endpoint);
|
||||
var msg = Encoding.Default.GetString(buf);
|
||||
byte[] buf;
|
||||
try
|
||||
{
|
||||
buf = udpClient.Receive(ref endpoint);
|
||||
}
|
||||
catch (ThreadAbortException)
|
||||
{
|
||||
udpClient.Dispose();
|
||||
return;
|
||||
}
|
||||
var packageHead = buf[0];
|
||||
if(packageHead<revHead)
|
||||
continue;
|
||||
revHead = packageHead;
|
||||
buf = buf[1..];
|
||||
if (OnReceive is not null)
|
||||
OnReceive(endpoint, msg);
|
||||
OnReceive(endpoint, buf);
|
||||
else
|
||||
BIT4Log.Log<ILANBroadcaster>($"Receive From {endpoint}:\t{msg}");
|
||||
BIT4Log.Log<ILANBroadcaster>($"Receive From {endpoint}:\t{buf}");
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
udpClient.Dispose();
|
||||
}
|
||||
catch(ThreadAbortException){}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
StopBroadcast();
|
||||
StopListen();
|
||||
OnReceive = null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user