Files
SunlightAggregationTerminal/Class/TcpServer.cs
T

110 lines
4.3 KiB
C#
Raw Normal View History

2026-05-11 02:25:45 +08:00
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.VisualBasic;
using SunlightAggregationTerminal.Models;
using System;
2026-05-01 14:44:49 +08:00
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Reflection.PortableExecutable;
2026-05-01 14:44:49 +08:00
using System.Text;
2026-05-03 22:17:25 +08:00
using System.Text.Json;
2026-05-01 14:44:49 +08:00
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace SunlightAggregationTerminal.Class
{
public class TcpServer
{
public static TouchSocket.Sockets.TcpClient tcpClient = new TouchSocket.Sockets.TcpClient();
2026-05-01 14:44:49 +08:00
2026-05-12 02:28:59 +08:00
public static async void Configuration(string ip, string Name, string Password, string Terminal)
2026-05-01 14:44:49 +08:00
{
DataReceived.ServerIP = ip;
DataReceived.ServerName = Name;
DataReceived.ServerPassword = Password;
DataReceived.ServerTerminal = Terminal;
2026-05-02 22:33:45 +08:00
await TcpClient(tcpClient, DataReceived.ServerIP);
2026-05-01 14:44:49 +08:00
}
2026-05-12 02:28:59 +08:00
// 引入发送锁,彻底解决连续发送导致的并发断连问题
private static readonly SemaphoreSlim _sendLock = new SemaphoreSlim(1, 1);
2026-05-03 22:17:25 +08:00
public static async void TcpTransmit(string Dat)
2026-05-01 14:44:49 +08:00
{
var dat = new DataReceived.Person()
2026-05-04 01:01:23 +08:00
{
User = DataReceived.ServerName,
Password = DataReceived.ServerPassword,
IP = DataReceived.GetLocalIpSimple(),
Terminal = DataReceived.ServerTerminal,
2026-05-03 22:17:25 +08:00
Dat = Dat
};
2026-05-12 02:28:59 +08:00
// 使用锁确保同一时间只有一个任务在发送,防止并发写入导致 Socket 异常
await _sendLock.WaitAsync();
2026-05-01 14:44:49 +08:00
//发送字符串数据
2026-05-03 22:17:25 +08:00
try
{
await tcpClient.SendAsync(JsonSerializer.Serialize(dat));
}
catch (Exception ex)
2026-05-03 22:17:25 +08:00
{
MainThread.BeginInvokeOnMainThread(() =>
{
Application.Current!.Windows[0].Page!.DisplayAlertAsync("错误", ex.Message.ToString(), "是");
});
2026-05-03 22:17:25 +08:00
}
2026-05-12 02:28:59 +08:00
finally
{
_sendLock.Release(); // 释放锁
}
2026-05-01 14:44:49 +08:00
}
public static async Task TcpClient(TouchSocket.Sockets.TcpClient tcpClient, string ip)
2026-05-01 14:44:49 +08:00
{
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) =>
{
2026-05-12 02:28:59 +08:00
TcpServer.TcpTransmit("{\"Command\":\"Log\"}");
return EasyTask.CompletedTask;
};//成功连接到服务器
tcpClient.Closing = (client, e) => {return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
tcpClient.Closed = (client, e) => {return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
2026-05-01 14:44:49 +08:00
#region Tcp客户端使用Received异步委托接收数据
tcpClient.Received = (client, e) =>
{
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
var mes = e.Memory.Span.ToString(Encoding.UTF8);
2026-05-04 01:01:23 +08:00
DataReceived.Received(mes);
2026-05-01 14:44:49 +08:00
return EasyTask.CompletedTask;
};
#endregion
2026-05-03 22:17:25 +08:00
try
{
await tcpClient.SetupAsync(new TouchSocketConfig()
.SetSingleStreamDataHandlingAdapter(() => new PeriodPackageAdapter())//以超时周期和包
.SetRemoteIPHost(ip)
.ConfigurePlugins(a =>
{
a.UseReconnection<TouchSocket.Sockets.TcpClient>(options =>
{
options.PollingInterval = TimeSpan.FromSeconds(5);
});
}));
2026-05-03 22:17:25 +08:00
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
}
catch (Exception ex)
2026-05-03 22:17:25 +08:00
{
MainThread.BeginInvokeOnMainThread(() =>
{
Application.Current!.Windows[0].Page!.DisplayAlertAsync("网络错误", ex.Message.ToString(), "是");
});
2026-05-03 22:17:25 +08:00
}
2026-05-01 14:44:49 +08:00
}
}
}