1
This commit is contained in:
@@ -11,6 +11,7 @@ namespace BITKit.Net.Http
|
||||
public int Port { get; set; }
|
||||
private readonly HttpListener _httpListener = new();
|
||||
public event Func<HttpListenerRequest,HttpContent> OnRequest;
|
||||
public event Action<byte[]> OnRequestBytes;
|
||||
private readonly CancellationTokenSource _cancellationTokenSource = new();
|
||||
private CancellationToken _cancellationToken=>_cancellationTokenSource.Token;
|
||||
|
||||
@@ -69,7 +70,6 @@ namespace BITKit.Net.Http
|
||||
|
||||
var response = context.Response;
|
||||
|
||||
|
||||
var output = response.OutputStream;
|
||||
|
||||
if (request.RawUrl is "/favicon.ico")
|
||||
@@ -83,11 +83,16 @@ namespace BITKit.Net.Http
|
||||
response.Headers.Add("Access-Control-Allow-Origin", "*"); // 允许任何来源访问,生产环境应更具体设置
|
||||
response.Headers.Add("Access-Control-Allow-Methods", "*"); // 允许的HTTP方法
|
||||
response.Headers.Add("Access-Control-Allow-Headers", "*"); // 允许的标头
|
||||
|
||||
|
||||
var content = OnRequest?.Invoke(request);
|
||||
//添加返回的文本为utf-8
|
||||
response.ContentType = "application/json;charset=utf-8";
|
||||
response.ContentEncoding = System.Text.Encoding.UTF8;
|
||||
|
||||
|
||||
|
||||
var buffer = StringHelper.GetBytes(ContextModel.Error("没有注册请求事件"));
|
||||
|
||||
var content = OnRequest?.Invoke(request);
|
||||
if (content is not null)
|
||||
{
|
||||
#if NET5_0_OR_GREATER
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -28,6 +28,7 @@ namespace BITKit
|
||||
AllClientCommand=5,
|
||||
Message=6,
|
||||
Heartbeat=7,
|
||||
Ping=8,
|
||||
}
|
||||
/// <summary>
|
||||
/// 网络提供服务,包括了基础网络服务,e.g
|
||||
@@ -43,6 +44,7 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface INetProvider
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 向服务端发送指令
|
||||
/// </summary>
|
||||
@@ -138,6 +140,14 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface INetServer
|
||||
{
|
||||
/// <summary>
|
||||
/// 源物体,用于通过代理直接访问
|
||||
/// </summary>
|
||||
public object Source => this;
|
||||
/// <summary>
|
||||
/// 手动Tick
|
||||
/// </summary>
|
||||
public bool ManualTick { get; set; }
|
||||
/// <summary>
|
||||
/// 回调:当客户端连接时
|
||||
/// </summary>
|
||||
@@ -207,6 +217,11 @@ namespace BITKit
|
||||
/// </summary>
|
||||
public interface INetClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 源物体,用于通过代理直接访问
|
||||
/// </summary>
|
||||
public object Source => this;
|
||||
|
||||
//基本客户端回调
|
||||
public event Action OnStartConnect;
|
||||
public event Action OnConnected;
|
||||
@@ -217,6 +232,10 @@ namespace BITKit
|
||||
/// </summary>
|
||||
bool IsConnected { get; }
|
||||
/// <summary>
|
||||
/// 手动Tick
|
||||
/// </summary>
|
||||
bool ManualTick { get; set; }
|
||||
/// <summary>
|
||||
/// 连接服务端的延迟
|
||||
/// </summary>
|
||||
int Ping { get; }
|
||||
|
Reference in New Issue
Block a user