|
|
|
@ -9,6 +9,7 @@ using System.IO;
|
|
|
|
|
using System.IO.Ports;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
@ -36,6 +37,8 @@ using TouchSocket.Core;
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
using ZstdSharp.Unsafe;
|
|
|
|
|
|
|
|
|
|
using static SocketExample.MyTouchClass;
|
|
|
|
|
|
|
|
|
|
namespace SocketExample
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
@ -49,13 +52,14 @@ namespace SocketExample
|
|
|
|
|
#region 变量定义
|
|
|
|
|
IPAddress ip;
|
|
|
|
|
int port;
|
|
|
|
|
TcpClient client;
|
|
|
|
|
ITcpClient client;
|
|
|
|
|
ITcpSessionClient TcpSessionClient;
|
|
|
|
|
public string option;//命令选项
|
|
|
|
|
public ICommand TextChangedCommand { get; }
|
|
|
|
|
public ICommand SelectionChangedCommand { get; }
|
|
|
|
|
public ICommand SendTextChangedCommand { get; }
|
|
|
|
|
public ICommand TextChangedCommand { get; set; }
|
|
|
|
|
public ICommand SelectionChangedCommand { get; set; }
|
|
|
|
|
public ICommand SendTextChangedCommand { get; set; }
|
|
|
|
|
|
|
|
|
|
public ICommand GPOSelectionChangedCommand { get; }
|
|
|
|
|
public ICommand GPOSelectionChangedCommand { get; set; }
|
|
|
|
|
|
|
|
|
|
private ObservableCollection<TagItem> _tagItems;
|
|
|
|
|
public ObservableCollection<TagItem> TagItems { get => _tagItems; set { _tagItems = value; OnPropertyChanged(); } }
|
|
|
|
@ -64,6 +68,7 @@ namespace SocketExample
|
|
|
|
|
public string Porttext { get; set; }//端口文本框
|
|
|
|
|
private string _messagetext;
|
|
|
|
|
|
|
|
|
|
public string SessionID { get; set; }
|
|
|
|
|
public int Borderwidth { get => _borderwidth; set { _borderwidth = value; OnPropertyChanged(); } }
|
|
|
|
|
private int _borderwidth { get; set; }
|
|
|
|
|
public string MessageText { get => _messagetext; set { _messagetext = value; OnPropertyChanged(); } }//发送框
|
|
|
|
@ -191,6 +196,22 @@ namespace SocketExample
|
|
|
|
|
public PanelItem(int index)
|
|
|
|
|
{
|
|
|
|
|
Text = $"客户端{index + 1}";
|
|
|
|
|
PanelInitialization();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PanelItem(TcpSessionClient recieveClient)
|
|
|
|
|
{
|
|
|
|
|
Text = $"客户端{recieveClient.IP}";
|
|
|
|
|
TcpSessionClient = recieveClient as ITcpSessionClient;
|
|
|
|
|
SessionID = recieveClient.Id;
|
|
|
|
|
//断连事件
|
|
|
|
|
//TcpSessionClient.Closed = (client, e) => { try { GPIOthread.Abort(); Infotext += "已断开!\n"; StateColour = "Red"; LinkState = "未连接"; } catch (Exception ex) { Infotext += ex; } return EasyTask.CompletedTask; };
|
|
|
|
|
//接收事件
|
|
|
|
|
PanelInitialization();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void PanelInitialization()//终端初始化
|
|
|
|
|
{
|
|
|
|
|
linkcomn = new LinkCommand(Button_Link);//绑定按钮的链接事件
|
|
|
|
|
readcomn = new LinkCommand(Button_Read);
|
|
|
|
|
stopreadingcomn = new LinkCommand(Button_StopReading);
|
|
|
|
@ -232,7 +253,8 @@ namespace SocketExample
|
|
|
|
|
GPI2 = "低";
|
|
|
|
|
GPI3 = "低";
|
|
|
|
|
GPI4 = "低";
|
|
|
|
|
}
|
|
|
|
|
}//终端初始化
|
|
|
|
|
|
|
|
|
|
#region 界面功能绑定定义
|
|
|
|
|
public LinkCommand _powerselectchanged { get { return powerselectchanged; } }
|
|
|
|
|
|
|
|
|
@ -645,7 +667,8 @@ namespace SocketExample
|
|
|
|
|
{
|
|
|
|
|
int number = 4;
|
|
|
|
|
int width = ((int)SystemParameters.PrimaryScreenWidth - 60) / number;
|
|
|
|
|
while(width<360 || width > 700){
|
|
|
|
|
while (width < 360 || width > 700)
|
|
|
|
|
{
|
|
|
|
|
if (width < 360)
|
|
|
|
|
{
|
|
|
|
|
number--;
|
|
|
|
@ -1194,6 +1217,7 @@ namespace SocketExample
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
EnsureFolderExists("日志文件夹");
|
|
|
|
|
this.Closing += TCPWindowV2_Closing;
|
|
|
|
|
PanelContainer.ItemsSource = items;
|
|
|
|
|
SaveWindowSettings();
|
|
|
|
|
Buttonflag = 0;
|
|
|
|
|
}
|
|
|
|
@ -1267,7 +1291,112 @@ namespace SocketExample
|
|
|
|
|
PanelContainer.ItemsSource = null;
|
|
|
|
|
currentcount = 0;
|
|
|
|
|
}
|
|
|
|
|
private void ListenButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Thread ListeningPortThread = new Thread(ListeningTheard);
|
|
|
|
|
ListeningPortThread.IsBackground = true;
|
|
|
|
|
ListeningPortThread.Start();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region 其他功能
|
|
|
|
|
|
|
|
|
|
private void ListeningTheard() //监听线程
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<NetworkInterface> networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
|
|
|
|
|
.Where(nic => nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet // 仅以太网
|
|
|
|
|
&& nic.OperationalStatus == OperationalStatus.Up); // 仅已启用的接口
|
|
|
|
|
CreateListenConnect(networkInterfaces);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<NetworkInterface> NewnetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
|
|
|
|
|
.Where(nic => nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet // 仅以太网
|
|
|
|
|
&& nic.OperationalStatus == OperationalStatus.Up); // 仅已启用的接口
|
|
|
|
|
|
|
|
|
|
var UniquenetworkInterfaces = NewnetworkInterfaces
|
|
|
|
|
.Where(newNic => !networkInterfaces.Any(oldNic => oldNic.Id == newNic.Id))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (!UniquenetworkInterfaces.Any())
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
networkInterfaces = NewnetworkInterfaces;
|
|
|
|
|
CreateListenConnect(UniquenetworkInterfaces);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateListenConnect(IEnumerable<NetworkInterface> networkInterfaces) //创建连接
|
|
|
|
|
{
|
|
|
|
|
foreach (var nic in networkInterfaces)
|
|
|
|
|
{
|
|
|
|
|
var ipProperties = nic.GetIPProperties();
|
|
|
|
|
var unicastAddresses = ipProperties.UnicastAddresses
|
|
|
|
|
.Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
|
|
|
|
|
|
|
|
|
|
foreach (var addr in unicastAddresses)
|
|
|
|
|
{
|
|
|
|
|
var tcpService = new TcpService();
|
|
|
|
|
tcpService.SetupAsync(new TouchSocketConfig()//载入配置
|
|
|
|
|
.SetListenIPHosts($"tcp://{addr.Address}:20108", 7790)//可以同时监听两个地址
|
|
|
|
|
.ConfigureContainer(a =>//容器的配置顺序应该在最前面
|
|
|
|
|
{
|
|
|
|
|
a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用)
|
|
|
|
|
})
|
|
|
|
|
.ConfigurePlugins(a =>
|
|
|
|
|
{
|
|
|
|
|
//a.Add();//此处可以添加插件
|
|
|
|
|
}));
|
|
|
|
|
tcpService.Connected += ListeningConnected;
|
|
|
|
|
tcpService.Closed += ListeningClosed;
|
|
|
|
|
tcpService.StartAsync();
|
|
|
|
|
Console.WriteLine($"网卡: {nic.Description}, IP: {addr.Address}");
|
|
|
|
|
MessageBox.Show($"{addr.Address}监听成功!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task ListeningConnected(TcpSessionClient client, ConnectedEventArgs e) //连接事件
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
items.Add(new PanelItem(client));
|
|
|
|
|
PanelContainer.ItemsSource = null;
|
|
|
|
|
PanelContainer.ItemsSource = items;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task ListeningClosed(TcpSessionClient client, ClosedEventArgs e) //断开事件
|
|
|
|
|
{
|
|
|
|
|
Application.Current.Dispatcher.Invoke(() =>
|
|
|
|
|
{
|
|
|
|
|
foreach (PanelItem item in items)
|
|
|
|
|
{
|
|
|
|
|
if (item.SessionID.Equals(client.Id))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
items.Remove(item);
|
|
|
|
|
if (items.Count == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
PanelContainer.ItemsSource = null;
|
|
|
|
|
PanelContainer.ItemsSource = items;
|
|
|
|
|
});
|
|
|
|
|
return EasyTask.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class LinkCommand : ICommand //command方法实现
|
|
|
|
|
{
|
|
|
|
|
private Action _excute;
|
|
|
|
@ -1529,5 +1658,6 @@ namespace SocketExample
|
|
|
|
|
OriginWindowState = this.WindowState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|