|
@ -32,7 +32,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass |
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
public class AsyncTcpClient |
|
|
public class AsyncTcpClient |
|
|
{ |
|
|
{ |
|
|
public static async Task TcpClient(TcpClient tcpClient, string ip,string port) |
|
|
|
|
|
|
|
|
public static async Task TcpClient(TcpClient tcpClient, string ip, string port) |
|
|
{ |
|
|
{ |
|
|
//TcpClient tcpClient = new TcpClient();
|
|
|
//TcpClient tcpClient = new TcpClient();
|
|
|
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
|
|
|
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
|
|
@ -56,7 +57,8 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass |
|
|
drEmployee.AcceptChanges(); |
|
|
drEmployee.AcceptChanges(); |
|
|
drEmployee.ClearErrors(); |
|
|
drEmployee.ClearErrors(); |
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
|
|
|
return EasyTask.CompletedTask; |
|
|
|
|
|
};//即将从服务器断开连接。此处仅主动断开才有效。
|
|
|
tcpClient.Closed = (client, e) => |
|
|
tcpClient.Closed = (client, e) => |
|
|
{ |
|
|
{ |
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First(); |
|
|
DataRow drEmployee = MainWindowViewModel.Machines.Select("IP='" + client.IP + "' AND port='" + client.Port + "'").First(); |
|
@ -66,22 +68,27 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass |
|
|
drEmployee.AcceptChanges(); |
|
|
drEmployee.AcceptChanges(); |
|
|
drEmployee.ClearErrors(); |
|
|
drEmployee.ClearErrors(); |
|
|
|
|
|
|
|
|
return EasyTask.CompletedTask; };//从服务器断开连接,当连接不成功时不会触发。
|
|
|
return EasyTask.CompletedTask; |
|
|
|
|
|
};//从服务器断开连接,当连接不成功时不会触发。
|
|
|
tcpClient.Received = (client, e) => |
|
|
tcpClient.Received = (client, e) => |
|
|
{ |
|
|
{ |
|
|
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
|
|
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
|
|
|
string SYSAPI = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(0, 5); |
|
|
string SYSAPI = e.ByteBlock.Span.ToString(Encoding.ASCII).Substring(0, 5); |
|
|
string DAT = e.ByteBlock.Span.ToString(Encoding.ASCII); |
|
|
string DAT = e.ByteBlock.Span.ToString(Encoding.UTF8); |
|
|
|
|
|
|
|
|
if (SYSAPI == "SC851") |
|
|
if (SYSAPI == "SC851") |
|
|
{ |
|
|
{ |
|
|
|
|
|
DAT = DAT.Substring(DAT.IndexOf("]") + 1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataTable obj = JsonConvert.DeserializeObject<DataTable>(DAT);//反序列化
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// client.SendAsync("SC851" + "[" + MainWindowViewModel.S01 + "]" + MainWindowViewModel.dt_d.ToJsonString());//数字开关信息
|
|
|
// client.SendAsync("SC851" + "[" + MainWindowViewModel.S01 + "]" + MainWindowViewModel.dt_d.ToJsonString());//数字开关信息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}//数字开关表
|
|
|
}//数字开关表
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -91,37 +98,41 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass |
|
|
|
|
|
|
|
|
//载入配置
|
|
|
//载入配置
|
|
|
await tcpClient.SetupAsync(new TouchSocketConfig() |
|
|
await tcpClient.SetupAsync(new TouchSocketConfig() |
|
|
.SetRemoteIPHost(ip+":"+port) |
|
|
.SetMaxBufferSize(1024*1024) |
|
|
|
|
|
.SetMinBufferSize(1024*64) |
|
|
|
|
|
.SetRemoteIPHost(ip + ":" + port) |
|
|
.ConfigurePlugins(a => |
|
|
.ConfigurePlugins(a => |
|
|
{ |
|
|
{ |
|
|
a.UseCheckClear() |
|
|
a.UseCheckClear() |
|
|
.SetCheckClearType(CheckClearType.All) |
|
|
.SetCheckClearType(CheckClearType.All) |
|
|
.SetTick(TimeSpan.FromSeconds(30)) |
|
|
.SetTick(TimeSpan.FromSeconds(30)) |
|
|
.SetOnClose((c, t) => |
|
|
.SetOnClose((c, t) => |
|
|
{ |
|
|
{ |
|
|
c.TryShutdown(); |
|
|
c.TryShutdown(); |
|
|
}); |
|
|
}); |
|
|
a.UseTcpReconnection();//触发型重连
|
|
|
a.UseTcpReconnection();//触发型重连
|
|
|
}) |
|
|
}) |
|
|
.ConfigureContainer(a => |
|
|
.ConfigureContainer(a => |
|
|
{ |
|
|
{ |
|
|
// a.AddConsoleLogger();//添加一个日志注入
|
|
|
// a.AddConsoleLogger();//添加一个日志注入
|
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
// Result result = await
|
|
|
// Result result = await
|
|
|
_ = tcpClient.TryConnectAsync(); |
|
|
_ = tcpClient.TryConnectAsync(); |
|
|
// return result.IsSuccess;
|
|
|
// return result.IsSuccess;
|
|
|
/* try |
|
|
/* try |
|
|
{ |
|
|
{ |
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
|
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
catch |
|
|
catch |
|
|
{ |
|
|
{ |
|
|
return false; |
|
|
return false; |
|
|
}*/ |
|
|
}*/ |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|