using CommonUtils.Log4; using CommonUtils.Msgbox; using CommonUtils.Tcp.TcpSocket; using Prism.Commands; using Prism.Mvvm; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Linq.Expressions; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using TcpClientModule.Model; using WpfControlLibrary.IpAddress; namespace TcpClientModule.ViewModels { public class TcpClientViewModel :CommonUtils.ViewModelBase.BaseViewModel { private string _message; public string Message { get { return _message; } set { SetProperty(ref _message, value); } } private IpAddressViewModel _ipAddressView=new IpAddressViewModel(); public IpAddressViewModel IpAddressView { get => _ipAddressView; set { Tcp_Clientmodel.Ipstring = value.AddressText; SetProperty(ref _ipAddressView, value); } } private TcpClientModel _tcpClientModel=null; public TcpClientModel SelectTcp_Clientmodel { get { return _tcpClientModel; } set { SetProperty(ref _tcpClientModel, value); } } private TcpClientModel tcpClientmodel=new TcpClientModel(); public TcpClientModel Tcp_Clientmodel { get { tcpClientmodel.Ipstring = _ipAddressView.AddressText; return tcpClientmodel; } set { SetProperty(ref tcpClientmodel, value); } } private ObservableCollection tcpClientModels =new ObservableCollection(); public ObservableCollection TcpClientModels { get { return tcpClientModels; } set { SetProperty(ref tcpClientModels, value); } } private string uiOpenText="连接"; public string UiOpenText { get => uiOpenText; set { SetProperty(ref uiOpenText, value); } } private DelegateCommand _TcpClientOpencommand; public DelegateCommand TcpClientOpencommand => _TcpClientOpencommand ?? ( _TcpClientOpencommand = new DelegateCommand(OpenTcpClientCommand) ); public TcpClientViewModel() { Message = "View A from your Prism Module"; IpAddressView.SetAddress(Tcp_Clientmodel.Ipstring); } public void OpenTcpClientCommand() { if ( UiOpenText == "断开" ) { for ( int i = 0; i < tcpClientModels.Count; i++ ) { if ( SelectTcp_Clientmodel != null ) { if ( tcpClientModels[ i ].Ipstring + ":" + tcpClientModels[ i ].Port == SelectTcp_Clientmodel.Ipstring + ":" + SelectTcp_Clientmodel.Port ) { tcpClientModels[ i ].socketClient.Close(); } } else { if ( tcpClientModels[ i ].Ipstring + ":" + tcpClientModels[ i ].Port == Tcp_Clientmodel.Ipstring + ":" + Tcp_Clientmodel.Port ) { tcpClientModels[ i ].socketClient.Close(); } } } UiOpenText = "连接"; return; } if ( Tcp_Clientmodel.Ipstring.Trim() == "" ) { WinMessageBox.Show("IP地址不能为空!"); return; } if ( Tcp_Clientmodel.Port <= 0 ) { WinMessageBox.Show("端口号不能为空!"); return; } TcpSocketClient tcpSocketClient = new TcpSocketClient(); tcpSocketClient.RemoteClose += TcpSocketClient_RemoteClose; if ( !tcpSocketClient.Connect(Tcp_Clientmodel.Ipstring, Tcp_Clientmodel.Port) ) { WinMessageBox.Show("连接服务器失败!"); return; } UiOpenText = "断开"; TcpClientModel model= new TcpClientModel() { Ipstring = tcpClientmodel.Ipstring, Port = tcpClientmodel.Port, socketClient = tcpSocketClient }; TcpAdd(model); if ( tcpSocketClient.StartReceive() == false ) return; } public void OpenAllTcpClientCommand() { try { for ( int i = 0; i < tcpClientModels.Count; i++ ) { if (tcpClientModels[ i ].socketClient == null ) { tcpClientModels[ i ].socketClient = new TcpSocketClient(); tcpClientModels[ i ].socketClient.Connect(tcpClientModels[ i ].Ipstring, tcpClientModels[ i ].Port); } if ( !tcpClientModels[ i ].socketClient.Socket.Connected ) tcpClientModels[ i ].socketClient.Connect(tcpClientModels[ i ].Ipstring, tcpClientModels[ i ].Port); } } catch(Exception e) { LogHelper.Error("Tcp客户端运行错误 "+e.Message); isErr = true; } } private void TcpSocketClient_RemoteClose(string key) { for ( int i = 0; i < TcpClientModels.Count; i++ ) { if ( TcpClientModels[ i ].Ipstring + ":" + TcpClientModels[ i ].Port == key ) { TcpRemove(i); } } LogHelper.Info(key+"连接断开"); } public static event Action> ItemAdded; public static event Action> ItemRemoved; public void TcpAdd(TcpClientModel item) { TcpClientModels.Add(item); ItemAdded?.Invoke(TcpClientModels); } public void TcpRemove(int item) { TcpClientModels.RemoveAt(item); ItemRemoved?.Invoke(TcpClientModels); } } }