SocketClientSimulator.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Aitex.Core.Account;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.Util;
  12. using MECF.Framework.Common.Communications;
  13. using MECF.Framework.Common.Communications.Tcp.Socket.Framing;
  14. using MECF.Framework.Common.Communications.Tcp.Socket.Server.APM;
  15. using MECF.Framework.Common.Communications.Tcp.Socket.Server.APM.EventArgs;
  16. namespace MECF.Framework.Simulator.Core.Driver
  17. {
  18. public class SocketClientSimulator : DeviceSimulator, IDisposable
  19. {
  20. public override bool IsEnabled
  21. {
  22. get { return _socket.IsConnected; }
  23. }
  24. public override bool IsConnected
  25. {
  26. get { return _socket.IsConnected; }
  27. }
  28. public int LocalPort
  29. {
  30. get { return _port; }
  31. set
  32. {
  33. _port = value;
  34. }
  35. }
  36. public string RemoteConnection
  37. {
  38. get
  39. {
  40. return string.Format("{0}:{1}", "127.0.0.1", _port);
  41. }
  42. }
  43. private AsynSocketClient _socket;
  44. private LinkedList<string> _lstAsciiMsgs = new LinkedList<string>();
  45. private LinkedList<byte[]> _lstBinsMsgs = new LinkedList<byte[]>();
  46. private PeriodicJob _thread;
  47. private object _locker = new object();
  48. private string _newLine;
  49. private int _port = 0;
  50. private bool _isAsciiMode;
  51. //private bool _enable;
  52. public SocketClientSimulator(int port, int commandIndex, string lineDelimiter, char msgDelimiter, int cmdMaxLength = 4)
  53. : base(commandIndex, lineDelimiter, msgDelimiter, cmdMaxLength)
  54. {
  55. _port = port;
  56. _newLine = lineDelimiter;
  57. _socket = new AsynSocketClient($"127.0.0.1:{_port}", true, lineDelimiter);
  58. _isAsciiMode = true;
  59. _socket.OnDataChanged += _port_OnAsciiDataReceived;
  60. _socket.OnBinaryDataChanged += _port_OnBinaryDataChanged;
  61. _socket.OnErrorHappened += _port_OnErrorHappened;
  62. //TcpSocketServerConfiguration config = new TcpSocketServerConfiguration()
  63. //{
  64. // FrameBuilder = new LineBasedFrameBuilder(new LineDelimiter(lineDelimiter)),
  65. //};
  66. //_server = new TcpSocketServer(IPAddress.Parse("127.0.0.1"), port, config);
  67. //_server.ClientConnected += new EventHandler<TcpClientConnectedEventArgs>(ClientConnected);
  68. //_server.ClientDisconnected += new EventHandler<TcpClientDisconnectedEventArgs>(ClientDisconnected);
  69. //_server.ClientDataReceived += new EventHandler<TcpClientDataReceivedEventArgs>(ClientDataReceived);
  70. _thread = new PeriodicJob(100, OnMonitor, "SocketSeverLisener", true);
  71. }
  72. private void _port_OnErrorHappened(TCPErrorEventArgs args)
  73. {
  74. }
  75. private void _port_OnBinaryDataChanged(byte[] binaryData)
  76. {
  77. lock (_locker)
  78. {
  79. _lstBinsMsgs.AddLast(binaryData);
  80. }
  81. }
  82. private void _port_OnAsciiDataReceived(string oneLineMessage)
  83. {
  84. lock (_locker)
  85. {
  86. if (string.IsNullOrEmpty(_newLine))
  87. {
  88. _lstAsciiMsgs.AddLast(oneLineMessage);
  89. }
  90. else
  91. {
  92. foreach (var message in oneLineMessage.Split(_newLine.ToCharArray()))
  93. {
  94. if (!string.IsNullOrEmpty(message))
  95. _lstAsciiMsgs.AddLast(message + _newLine);
  96. }
  97. }
  98. }
  99. }
  100. private bool OnMonitor()
  101. {
  102. lock (_locker)
  103. {
  104. if (_isAsciiMode)
  105. {
  106. while (_lstAsciiMsgs.Count > 0)
  107. {
  108. string asciiMsg = _lstAsciiMsgs.First.Value;
  109. if (!string.IsNullOrEmpty(asciiMsg))
  110. {
  111. if (_socket.NeedLog)
  112. LOG.Write($"Start handler message:{asciiMsg}");
  113. _port_HandleAsciiData(asciiMsg);
  114. }
  115. _lstAsciiMsgs.RemoveFirst();
  116. }
  117. }
  118. else
  119. {
  120. while (_lstBinsMsgs.Count > 0)
  121. {
  122. byte[] binMsg = _lstBinsMsgs.First.Value;
  123. _port_HandleBinarayData(binMsg);
  124. _lstBinsMsgs.RemoveFirst();
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. private void _port_HandleBinarayData(byte[] binMsg)
  131. {
  132. string asciiMsg = Encoding.ASCII.GetString(binMsg);
  133. if (ProcessReceivedData(asciiMsg))
  134. return;
  135. OnReadMessage(asciiMsg);
  136. if (!BinaryDataMode)
  137. OnReadMessage(asciiMsg);
  138. }
  139. private void _port_HandleAsciiData(string asciiMsg)
  140. {
  141. if (ProcessReceivedData(asciiMsg))
  142. return;
  143. OnReadMessage(asciiMsg);
  144. if (!BinaryDataMode)
  145. OnReadMessage(asciiMsg);
  146. }
  147. public void Enable()
  148. {
  149. _socket.Connect();
  150. //_enable = true;
  151. }
  152. public void Disable()
  153. {
  154. //_enable = false;
  155. }
  156. //void ClientDataReceived(object sender, TcpClientDataReceivedEventArgs e)
  157. //{
  158. // if (ProcessReceivedData(e))
  159. // return;
  160. // OnReadMessage(Encoding.UTF8.GetString(e.Data, e.DataOffset, e.DataLength));
  161. // if (BinaryDataMode)
  162. // OnReadMessage(e.Data.Skip(e.DataOffset).Take(e.DataLength).ToArray());
  163. //}
  164. public virtual bool ProcessReceivedData(string msg)
  165. {
  166. return false;
  167. }
  168. void ClientDisconnected(object sender, TcpClientDisconnectedEventArgs e)
  169. {
  170. //_server.CloseSession(_session.SessionKey);
  171. //_session = null;
  172. }
  173. public virtual void ClientConnected(object sender, TcpClientConnectedEventArgs e)
  174. {
  175. //_session = e.Session;
  176. }
  177. protected override void ProcessWriteMessage(string msg)
  178. {
  179. _socket.Write(msg);
  180. }
  181. protected override void ProcessWriteMessage(byte[] msg)
  182. {
  183. _socket.Write(msg);
  184. }
  185. public void Dispose()
  186. {
  187. Disable();
  188. }
  189. public void Connect()
  190. {
  191. _socket.Connect();
  192. }
  193. public void DisConnect()
  194. {
  195. }
  196. }
  197. }