123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Prism.Mvvm;
- using System.Threading.Tasks;
- using System.Collections.ObjectModel;
- using TcpClientModule.Model;
- using Prism.Commands;
- using log4net.Repository.Hierarchy;
- using System.Net.Sockets;
- using System.Net;
- using CommonUtils.Log4;
- using CommonUtils.ViewModelBase;
- using Newtonsoft.Json;
- using static CommonUtils.Enums.Enums;
- using CommonUtils.内部存储;
- using System.Threading;
- using CommonUtils.运行信号量;
- namespace TcpClientModule.ViewModels
- {
- public class TcpClientRcvViewModel: BaseViewModel
- {
- private ObservableCollection<TcpClientModel> tcpClientModels =new ObservableCollection<TcpClientModel>();
- public static object TcpRcvLock = new object();
-
- public ObservableCollection<TcpClientModel> TcpClientModels
- {
- get
- {
-
- return tcpClientModels;
- }
- set
- {
- SetProperty(ref tcpClientModels, value);
- }
- }
- public TcpClientRcvViewModel()
- {
-
- }
- private List<YesNO> yesNolist = new List<YesNO>(){ YesNO.是, YesNO.否 };
- private string inputstr;
- public string RevMsg { get => inputstr; set { SetProperty(ref inputstr, value); } }
- [JsonIgnore]
- private List<string> RevMsgs=new List<string>();
- private DelegateCommand _TcpRevOpencommand;
- public DelegateCommand TcpRevOpencommand =>
- _TcpRevOpencommand ?? ( _TcpRevOpencommand = new DelegateCommand(TcpRcvCommand) );
- public List<YesNO> YesNolist { get => yesNolist; set => yesNolist = value; }
- public List<string> StorageTypes = InternalStorage.StorageTypes;
- public List<string> StorageIndexs = InternalStorage.StorageIndexs;
- public TcpRcvModel Tcp_RcvModel
- {
- get
- {
- for (int i = 0; i < TcpClientModels.Count; i++)
- {
- if (tcpRcvModel.Tcp_ClientModel.Ipstring == TcpClientModels[i].Ipstring &&
- tcpRcvModel.Tcp_ClientModel.Port == TcpClientModels[i].Port)
- {
- tcpRcvModel.Tcp_ClientModel = TcpClientModels[i];
- }
- }
- if (tcpRcvModel.Tcp_ClientModel.socketClient == null)
- {
- tcpRcvModel.Tcp_ClientModel.socketClient = new CommonUtils.Tcp.TcpSocket.TcpSocketClient();
- tcpRcvModel.Tcp_ClientModel.socketClient.Connect(tcpRcvModel.Tcp_ClientModel.Ipstring, tcpRcvModel.Tcp_ClientModel.Port);
- tcpRcvModel.Tcp_ClientModel.socketClient.StartReceive();
- if (tcpRcvModel.Tcp_ClientModel != null && tcpRcvModel.Tcp_ClientModel.socketClient != null)
- {
- tcpRcvModel.Tcp_ClientModel.socketClient.ReceiveData -= SocketClient_ReceiveData;
- tcpRcvModel.Tcp_ClientModel.socketClient.ReceiveData += SocketClient_ReceiveData;
- }
- }
-
- return tcpRcvModel;
- }
- set
- {
- SetProperty(ref tcpRcvModel, value);
- }
- }
- private TcpRcvModel tcpRcvModel=new TcpRcvModel();
- public void TcpClientConfig_ItemRemoved(ObservableCollection<TcpClientModel> models)
- {
- TcpClientModels = models;
- for (int i = 0; i < TcpClientModels.Count; i++)
- {
- if (TcpClientModels[i].socketClient == null)
- {
- TcpClientModels[i].socketClient = new CommonUtils.Tcp.TcpSocket.TcpSocketClient();
- TcpClientModels[i].socketClient.Connect(TcpClientModels[i].Ipstring, TcpClientModels[i].Port);
- }
- if (TcpClientModels[i] != null && TcpClientModels[i].socketClient != null)
- {
- TcpClientModels[i].socketClient.ReceiveData -= SocketClient_ReceiveData;
- TcpClientModels[i].socketClient.ReceiveData += SocketClient_ReceiveData;
- }
- }
- }
- public void TcpClientConfig_ItemAdded(ObservableCollection<TcpClientModel> models)
- {
- TcpClientModels = models;
- for (int i = 0; i < TcpClientModels.Count; i++)
- {
- if (TcpClientModels[i].socketClient == null)
- {
- TcpClientModels[i].socketClient = new CommonUtils.Tcp.TcpSocket.TcpSocketClient();
- TcpClientModels[i].socketClient.Connect(TcpClientModels[i].Ipstring, TcpClientModels[i].Port);
- }
- if (TcpClientModels[i] != null && TcpClientModels[i].socketClient != null)
- {
- TcpClientModels[i].socketClient.ReceiveData -= SocketClient_ReceiveData;
- TcpClientModels[i].socketClient.ReceiveData += SocketClient_ReceiveData;
- }
- }
- }
- public void ReceiveThread()//ReceiveThread
- {
- //ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveThread));
- try
- {
- lock (TcpRcvLock)
- {
- bool stop = true;
- while (stop)
- {
- for (int i = 0; i < RevMsgs.Count; i++)
- {
- if (RevMsgs[i] == Tcp_RcvModel.PassStr)
- {
- if (Tcp_RcvModel.StorageType != "无" && Tcp_RcvModel.StorageIndex != "无")
- InternalStorage.SetValue(Tcp_RcvModel.StorageType + ":" + Tcp_RcvModel.StorageIndex, RevMsgs[i]);
- RevMsgs.RemoveAt(i);
- stop = false;
- break;
- }
- }
- if (!StaticManualResetEvent.ManualisStopEvent.WaitOne(100))
- {
- break;
- }
-
- }
- }
-
- }
- catch ( Exception e )
- {
- LogHelper.Error("Tcp接收运行错误 " + e.Message);
- isErr = true;
- }
- }
- /// <summary>
- /// 循环接收数据
- /// </summary>
- /// <param name="state"></param>
- private void TcpRcvCommand()//TcpRcvCommand
- {
- //Tcp_RcvModel.Tcp_ClientModel.socketClient.ReceiveData -= SocketClient_ReceiveData;
- //Tcp_RcvModel.Tcp_ClientModel.socketClient.ReceiveData += SocketClient_ReceiveData;
-
- }
- private void SocketClient_ReceiveData(IPEndPoint remote, byte[] buffer, int count)
- {
- string str=Tcp_RcvModel.RcvEncoding.GetString(buffer, 0, count);
- RevMsgs.Add(str);
- RevMsg = str;
- }
- }
- }
|