TcpClientRcvViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Prism.Mvvm;
  6. using System.Threading.Tasks;
  7. using System.Collections.ObjectModel;
  8. using TcpClientModule.Model;
  9. using Prism.Commands;
  10. using log4net.Repository.Hierarchy;
  11. using System.Net.Sockets;
  12. using System.Net;
  13. using CommonUtils.Log4;
  14. using CommonUtils.ViewModelBase;
  15. using Newtonsoft.Json;
  16. using static CommonUtils.Enums.Enums;
  17. using CommonUtils.内部存储;
  18. using System.Threading;
  19. using CommonUtils.运行信号量;
  20. namespace TcpClientModule.ViewModels
  21. {
  22. public class TcpClientRcvViewModel: BaseViewModel
  23. {
  24. private ObservableCollection<TcpClientModel> tcpClientModels =new ObservableCollection<TcpClientModel>();
  25. public static object TcpRcvLock = new object();
  26. public ObservableCollection<TcpClientModel> TcpClientModels
  27. {
  28. get
  29. {
  30. return tcpClientModels;
  31. }
  32. set
  33. {
  34. SetProperty(ref tcpClientModels, value);
  35. }
  36. }
  37. public TcpClientRcvViewModel()
  38. {
  39. }
  40. private List<YesNO> yesNolist = new List<YesNO>(){ YesNO.是, YesNO.否 };
  41. private string inputstr;
  42. public string RevMsg { get => inputstr; set { SetProperty(ref inputstr, value); } }
  43. [JsonIgnore]
  44. private List<string> RevMsgs=new List<string>();
  45. private DelegateCommand _TcpRevOpencommand;
  46. public DelegateCommand TcpRevOpencommand =>
  47. _TcpRevOpencommand ?? ( _TcpRevOpencommand = new DelegateCommand(TcpRcvCommand) );
  48. public List<YesNO> YesNolist { get => yesNolist; set => yesNolist = value; }
  49. public List<string> StorageTypes = InternalStorage.StorageTypes;
  50. public List<string> StorageIndexs = InternalStorage.StorageIndexs;
  51. public TcpRcvModel Tcp_RcvModel
  52. {
  53. get
  54. {
  55. for (int i = 0; i < TcpClientModels.Count; i++)
  56. {
  57. if (tcpRcvModel.Tcp_ClientModel.Ipstring == TcpClientModels[i].Ipstring &&
  58. tcpRcvModel.Tcp_ClientModel.Port == TcpClientModels[i].Port)
  59. {
  60. tcpRcvModel.Tcp_ClientModel = TcpClientModels[i];
  61. }
  62. }
  63. if (tcpRcvModel.Tcp_ClientModel.socketClient == null)
  64. {
  65. tcpRcvModel.Tcp_ClientModel.socketClient = new CommonUtils.Tcp.TcpSocket.TcpSocketClient();
  66. tcpRcvModel.Tcp_ClientModel.socketClient.Connect(tcpRcvModel.Tcp_ClientModel.Ipstring, tcpRcvModel.Tcp_ClientModel.Port);
  67. tcpRcvModel.Tcp_ClientModel.socketClient.StartReceive();
  68. if (tcpRcvModel.Tcp_ClientModel != null && tcpRcvModel.Tcp_ClientModel.socketClient != null)
  69. {
  70. tcpRcvModel.Tcp_ClientModel.socketClient.ReceiveData -= SocketClient_ReceiveData;
  71. tcpRcvModel.Tcp_ClientModel.socketClient.ReceiveData += SocketClient_ReceiveData;
  72. }
  73. }
  74. return tcpRcvModel;
  75. }
  76. set
  77. {
  78. SetProperty(ref tcpRcvModel, value);
  79. }
  80. }
  81. private TcpRcvModel tcpRcvModel=new TcpRcvModel();
  82. public void TcpClientConfig_ItemRemoved(ObservableCollection<TcpClientModel> models)
  83. {
  84. TcpClientModels = models;
  85. for (int i = 0; i < TcpClientModels.Count; i++)
  86. {
  87. if (TcpClientModels[i].socketClient == null)
  88. {
  89. TcpClientModels[i].socketClient = new CommonUtils.Tcp.TcpSocket.TcpSocketClient();
  90. TcpClientModels[i].socketClient.Connect(TcpClientModels[i].Ipstring, TcpClientModels[i].Port);
  91. }
  92. if (TcpClientModels[i] != null && TcpClientModels[i].socketClient != null)
  93. {
  94. TcpClientModels[i].socketClient.ReceiveData -= SocketClient_ReceiveData;
  95. TcpClientModels[i].socketClient.ReceiveData += SocketClient_ReceiveData;
  96. }
  97. }
  98. }
  99. public void TcpClientConfig_ItemAdded(ObservableCollection<TcpClientModel> models)
  100. {
  101. TcpClientModels = models;
  102. for (int i = 0; i < TcpClientModels.Count; i++)
  103. {
  104. if (TcpClientModels[i].socketClient == null)
  105. {
  106. TcpClientModels[i].socketClient = new CommonUtils.Tcp.TcpSocket.TcpSocketClient();
  107. TcpClientModels[i].socketClient.Connect(TcpClientModels[i].Ipstring, TcpClientModels[i].Port);
  108. }
  109. if (TcpClientModels[i] != null && TcpClientModels[i].socketClient != null)
  110. {
  111. TcpClientModels[i].socketClient.ReceiveData -= SocketClient_ReceiveData;
  112. TcpClientModels[i].socketClient.ReceiveData += SocketClient_ReceiveData;
  113. }
  114. }
  115. }
  116. public void ReceiveThread()//ReceiveThread
  117. {
  118. //ThreadPool.QueueUserWorkItem(new WaitCallback(ReceiveThread));
  119. try
  120. {
  121. lock (TcpRcvLock)
  122. {
  123. bool stop = true;
  124. while (stop)
  125. {
  126. for (int i = 0; i < RevMsgs.Count; i++)
  127. {
  128. if (RevMsgs[i] == Tcp_RcvModel.PassStr)
  129. {
  130. if (Tcp_RcvModel.StorageType != "无" && Tcp_RcvModel.StorageIndex != "无")
  131. InternalStorage.SetValue(Tcp_RcvModel.StorageType + ":" + Tcp_RcvModel.StorageIndex, RevMsgs[i]);
  132. RevMsgs.RemoveAt(i);
  133. stop = false;
  134. break;
  135. }
  136. }
  137. if (!StaticManualResetEvent.ManualisStopEvent.WaitOne(100))
  138. {
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. catch ( Exception e )
  145. {
  146. LogHelper.Error("Tcp接收运行错误 " + e.Message);
  147. isErr = true;
  148. }
  149. }
  150. /// <summary>
  151. /// 循环接收数据
  152. /// </summary>
  153. /// <param name="state"></param>
  154. private void TcpRcvCommand()//TcpRcvCommand
  155. {
  156. //Tcp_RcvModel.Tcp_ClientModel.socketClient.ReceiveData -= SocketClient_ReceiveData;
  157. //Tcp_RcvModel.Tcp_ClientModel.socketClient.ReceiveData += SocketClient_ReceiveData;
  158. }
  159. private void SocketClient_ReceiveData(IPEndPoint remote, byte[] buffer, int count)
  160. {
  161. string str=Tcp_RcvModel.RcvEncoding.GetString(buffer, 0, count);
  162. RevMsgs.Add(str);
  163. RevMsg = str;
  164. }
  165. }
  166. }