SocketDeviceSimulator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.Tcp.Socket.Framing;
  13. using MECF.Framework.Common.Communications.Tcp.Socket.Server.APM;
  14. using MECF.Framework.Common.Communications.Tcp.Socket.Server.APM.EventArgs;
  15. namespace MECF.Framework.Simulator.Core.Driver
  16. {
  17. public class SocketDeviceSimulator : DeviceSimulator, IDisposable
  18. {
  19. public override bool IsEnabled
  20. {
  21. get { return _server != null && _server.IsListening; }
  22. }
  23. public override bool IsConnected
  24. {
  25. get { return IsEnabled && _session != null; }
  26. }
  27. public int LocalPort
  28. {
  29. get { return _port; }
  30. set {
  31. {
  32. IPEndPoint ip = _server.ListenedEndPoint;
  33. ip.Port = value;
  34. _server.ListenedEndPoint = ip;
  35. } }
  36. }
  37. public string RemoteConnection
  38. {
  39. get
  40. {
  41. if (!IsConnected)
  42. return "--";
  43. TcpSocketSession session = _session;
  44. return string.Format("{0}:{1}", session.RemoteEndPoint.Address, session.RemoteEndPoint.Port);
  45. }
  46. }
  47. private TcpSocketServer _server;
  48. private TcpSocketSession _session;
  49. private PeriodicJob _thread;
  50. private int _port = 0;
  51. private bool _enable;
  52. public SocketDeviceSimulator(int port, int commandIndex, string lineDelimiter, char msgDelimiter, int cmdMaxLength = 4)
  53. : base(commandIndex, lineDelimiter, msgDelimiter, cmdMaxLength)
  54. {
  55. _port = port;
  56. TcpSocketServerConfiguration config = new TcpSocketServerConfiguration()
  57. {
  58. FrameBuilder = new LineBasedFrameBuilder(new LineDelimiter(lineDelimiter)),
  59. };
  60. _server = new TcpSocketServer(IPAddress.Parse("127.0.0.1"), port, config);
  61. _server.ClientConnected += new EventHandler<TcpClientConnectedEventArgs>(ClientConnected);
  62. _server.ClientDisconnected += new EventHandler<TcpClientDisconnectedEventArgs>(ClientDisconnected);
  63. _server.ClientDataReceived += new EventHandler<TcpClientDataReceivedEventArgs>(ClientDataReceived);
  64. _thread = new PeriodicJob(100, OnMonitor, "SocketSeverLisener", true);
  65. }
  66. private bool OnMonitor()
  67. {
  68. if (_enable)
  69. {
  70. if (!_server.IsListening)
  71. {
  72. _server.Listen();
  73. }
  74. }
  75. else
  76. {
  77. if (_server.IsListening)
  78. {
  79. _server.Shutdown();
  80. }
  81. }
  82. return true;
  83. }
  84. public void Enable()
  85. {
  86. _enable = true;
  87. }
  88. public void Disable()
  89. {
  90. _enable = false;
  91. }
  92. void ClientDataReceived(object sender, TcpClientDataReceivedEventArgs e)
  93. {
  94. if (ProcessReceivedData(e))
  95. return;
  96. OnReadMessage(Encoding.UTF8.GetString(e.Data, e.DataOffset, e.DataLength));
  97. if (BinaryDataMode)
  98. OnReadMessage(e.Data.Skip(e.DataOffset).Take(e.DataLength).ToArray());
  99. }
  100. public virtual bool ProcessReceivedData(TcpClientDataReceivedEventArgs e)
  101. {
  102. return false;
  103. }
  104. void ClientDisconnected(object sender, TcpClientDisconnectedEventArgs e)
  105. {
  106. _server.CloseSession(_session.SessionKey);
  107. _session = null;
  108. }
  109. public virtual void ClientConnected(object sender, TcpClientConnectedEventArgs e)
  110. {
  111. _session = e.Session;
  112. }
  113. protected override void ProcessWriteMessage(string msg)
  114. {
  115. if (IsEnabled && _session != null)
  116. {
  117. _session.Send(Encoding.ASCII.GetBytes(msg));
  118. }
  119. }
  120. protected override void ProcessWriteMessage(byte[] msg)
  121. {
  122. if (IsEnabled && _session != null)
  123. {
  124. _session.Send(msg);
  125. }
  126. }
  127. public void Dispose()
  128. {
  129. Disable();
  130. }
  131. }
  132. }