2024-07-31 09:14:07 +00:00
|
|
|
using System.Net.NetworkInformation;
|
2024-09-07 06:46:08 +00:00
|
|
|
using System.Net.WebSockets;
|
2024-07-30 07:36:42 +00:00
|
|
|
using System.Text;
|
2024-09-05 09:48:08 +00:00
|
|
|
using Common;
|
2024-09-07 06:46:08 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
2024-07-30 07:36:42 +00:00
|
|
|
namespace LocalServer.Controllers
|
|
|
|
{
|
|
|
|
public class LocalServerController(LocalSyncServerFactory factory) : ControllerBase
|
|
|
|
{
|
|
|
|
private readonly LocalSyncServerFactory Factory = factory;
|
|
|
|
|
2024-10-10 05:15:14 +00:00
|
|
|
private static async Task Echo(WebSocket webSocket)
|
|
|
|
{
|
|
|
|
var buffer = new byte[1024 * 4];
|
|
|
|
var receiveResult = await webSocket.ReceiveAsync(
|
|
|
|
new ArraySegment<byte>(buffer),
|
|
|
|
CancellationToken.None
|
|
|
|
);
|
|
|
|
|
|
|
|
while (!receiveResult.CloseStatus.HasValue)
|
|
|
|
{
|
|
|
|
await webSocket.SendAsync(
|
|
|
|
new ArraySegment<byte>(buffer, 0, receiveResult.Count),
|
|
|
|
receiveResult.MessageType,
|
|
|
|
receiveResult.EndOfMessage,
|
|
|
|
CancellationToken.None
|
|
|
|
);
|
|
|
|
|
|
|
|
receiveResult = await webSocket.ReceiveAsync(
|
|
|
|
new ArraySegment<byte>(buffer),
|
|
|
|
CancellationToken.None
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await webSocket.CloseAsync(
|
|
|
|
receiveResult.CloseStatus.Value,
|
|
|
|
receiveResult.CloseStatusDescription,
|
|
|
|
CancellationToken.None
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-05 01:59:57 +00:00
|
|
|
[Route("/websoc")]
|
2024-07-30 07:36:42 +00:00
|
|
|
public async Task WebsocketConnection(string Name)
|
|
|
|
{
|
|
|
|
if (HttpContext.WebSockets.IsWebSocketRequest)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-09-07 06:46:08 +00:00
|
|
|
if (Factory.GetServerByName(Name) == null)
|
|
|
|
{
|
|
|
|
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
|
2024-10-10 05:15:14 +00:00
|
|
|
//await Echo(webSocket);
|
2024-09-23 05:55:17 +00:00
|
|
|
var pipeLine = new WebSocPipeLine<WebSocket>(webSocket, false);
|
2024-10-10 05:15:14 +00:00
|
|
|
await Factory.CreateLocalSyncServer(
|
2024-09-23 05:55:17 +00:00
|
|
|
pipeLine,
|
|
|
|
Name,
|
2024-10-11 01:04:58 +00:00
|
|
|
new WebSocPipeLine<ClientWebSocket>(new ClientWebSocket(), true)
|
2024-09-23 05:55:17 +00:00
|
|
|
);
|
2024-09-07 06:46:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("LocalServer: 已经存在同名的发布正在进行!");
|
|
|
|
}
|
2024-07-30 07:36:42 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
HttpContext.Response.Body = new MemoryStream(Encoding.UTF8.GetBytes(e.Message));
|
|
|
|
HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
|
|
|
|
}
|
|
|
|
}
|
2024-07-31 09:14:07 +00:00
|
|
|
|
|
|
|
[Route("/macaddr")]
|
|
|
|
public string GetMacAddress()
|
|
|
|
{
|
|
|
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
|
|
|
string macaddrs = "";
|
|
|
|
foreach (NetworkInterface nic in nics)
|
|
|
|
{
|
|
|
|
PhysicalAddress physicalAddress = nic.GetPhysicalAddress();
|
|
|
|
macaddrs += physicalAddress.ToString() + ";";
|
|
|
|
}
|
|
|
|
return macaddrs;
|
|
|
|
}
|
2024-07-30 07:36:42 +00:00
|
|
|
//TODO 是否在本地记载同步日志?
|
|
|
|
}
|
|
|
|
}
|