add kcp
This commit is contained in:
8
Src/Core/Net/LAN/Core.meta
Normal file
8
Src/Core/Net/LAN/Core.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef6bc358998385e409414022bd10c876
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
16
Src/Core/Net/LAN/Core/ILANBroadcaster.cs
Normal file
16
Src/Core/Net/LAN/Core/ILANBroadcaster.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace BITKit.Net.LAN
|
||||
{
|
||||
public interface ILANBroadcaster
|
||||
{
|
||||
int Port { get; set; }
|
||||
byte[] Buffer { get; set; }
|
||||
event Action<EndPoint, string> OnReceive;
|
||||
void StartBroadcast();
|
||||
void StopBroadcast();
|
||||
void StartListen();
|
||||
void StopListen();
|
||||
}
|
||||
}
|
11
Src/Core/Net/LAN/Core/ILANBroadcaster.cs.meta
Normal file
11
Src/Core/Net/LAN/Core/ILANBroadcaster.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5874f625a9ef3e24092f070bdce13c9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
108
Src/Core/Net/LAN/UdpBasedLanBroadcaster.cs
Normal file
108
Src/Core/Net/LAN/UdpBasedLanBroadcaster.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace BITKit.Net.LAN
|
||||
{
|
||||
/// <summary>
|
||||
/// 基于UDP的LAN广播
|
||||
/// </summary>
|
||||
public class UdpBasedLanBroadcaster : ILANBroadcaster
|
||||
{
|
||||
public int Port { get; set; }
|
||||
public byte[] Buffer { get; set; } = StringHelper.GetBytes("Hello World");
|
||||
private bool _isBroadcasting;
|
||||
private bool _isListening;
|
||||
|
||||
public UdpBasedLanBroadcaster()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public event Action<EndPoint, string> OnReceive;
|
||||
|
||||
public void StartBroadcast()
|
||||
{
|
||||
_isBroadcasting = true;
|
||||
Thread thread = new(Process)
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
thread.Start();
|
||||
BIT4Log.Log<ILANBroadcaster>($"开始广播端口{Port}");
|
||||
}
|
||||
|
||||
public void StopBroadcast()
|
||||
{
|
||||
_isBroadcasting = false;
|
||||
BIT4Log.Log<ILANBroadcaster>($"停止广播端口 {Port}");
|
||||
}
|
||||
|
||||
public void StartListen()
|
||||
{
|
||||
_isListening = true;
|
||||
var thread = new Thread(ReceiveThread)
|
||||
{
|
||||
IsBackground = true
|
||||
};
|
||||
thread.Start();
|
||||
BIT4Log.Log<ILANBroadcaster>($"开始监听端口:{Port}");
|
||||
}
|
||||
|
||||
public void StopListen()
|
||||
{
|
||||
_isListening = false;
|
||||
BIT4Log.Log<ILANBroadcaster>($"停止监听端口{Port}");
|
||||
}
|
||||
|
||||
|
||||
private void Process()
|
||||
{
|
||||
var udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, 0));
|
||||
|
||||
var endpoint = new IPEndPoint(IPAddress.Broadcast, Port);
|
||||
//其实 IPAddress.Broadcast 就是 255.255.255.255
|
||||
//下面代码与上面有相同的作用
|
||||
//IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 8080);
|
||||
try
|
||||
{
|
||||
while (_isBroadcasting)
|
||||
{
|
||||
udpClient.Send(Buffer, Buffer.Length, endpoint);
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
udpClient.Dispose();
|
||||
}
|
||||
|
||||
private void ReceiveThread()
|
||||
{
|
||||
try
|
||||
{
|
||||
var udpClient = new UdpClient(new IPEndPoint(IPAddress.Any, Port));
|
||||
var endpoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
while (_isListening)
|
||||
{
|
||||
var buf = udpClient.Receive(ref endpoint);
|
||||
var msg = Encoding.Default.GetString(buf);
|
||||
if (OnReceive is not null)
|
||||
OnReceive(endpoint, msg);
|
||||
else
|
||||
BIT4Log.Log<ILANBroadcaster>($"Receive From {endpoint}:\t{msg}");
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
BIT4Log.LogException(e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
11
Src/Core/Net/LAN/UdpBasedLanBroadcaster.cs.meta
Normal file
11
Src/Core/Net/LAN/UdpBasedLanBroadcaster.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4822fff0e51bc0a43a98342ea5c1d9d0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user