MelsecA1EAsciiServer.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using HslCommunication.Core.Net;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. namespace HslCommunication.Enthernet
  8. {
  9. /// <summary>
  10. /// 同步消息处理服务器,主要用来实现接收客户端信息并进行消息反馈的操作
  11. /// </summary>
  12. public class MelsecA1EAsciiServer : NetworkServerBase
  13. {
  14. #region Constructor
  15. /// <summary>
  16. /// 实例化一个服务器消息请求的信息
  17. /// </summary>
  18. public MelsecA1EAsciiServer()
  19. {
  20. // cxy
  21. HslProtocol.HeadByteLength = 22;
  22. }
  23. #endregion
  24. #region 事件通知块
  25. /// <summary>
  26. /// 接收字符串信息的事件
  27. /// </summary>
  28. public event Action<AppSession, NetHandle, string> ReceiveStringEvent;
  29. /// <summary>
  30. /// 接收字节信息的事件
  31. /// </summary>
  32. public event Action<AppSession, NetHandle, byte[]> ReceivedBytesEvent;
  33. private void OnReceiveStringEvent( AppSession session, int customer, string str )
  34. {
  35. ReceiveStringEvent?.Invoke( session, customer, str );
  36. }
  37. private void OnReceivedBytesEvent( AppSession session, int customer, byte[] temp )
  38. {
  39. ReceivedBytesEvent?.Invoke( session, customer, temp );
  40. }
  41. #endregion
  42. #region 启动停止块
  43. /// <summary>
  44. /// 关闭网络的操作
  45. /// </summary>
  46. protected override void CloseAction()
  47. {
  48. ReceivedBytesEvent = null;
  49. ReceiveStringEvent = null;
  50. base.CloseAction( );
  51. }
  52. // cxy
  53. public void SendMessage(AppSession session, string customer)
  54. {
  55. SendBytesAsync(session, Encoding.ASCII.GetBytes(customer));
  56. }
  57. /// <summary>
  58. /// 向指定的通信对象发送字符串数据
  59. /// </summary>
  60. /// <param name="session">通信对象</param>
  61. /// <param name="customer">用户的指令头</param>
  62. /// <param name="str">实际发送的字符串数据</param>
  63. public void SendMessage( AppSession session, int customer, string str )
  64. {
  65. SendBytesAsync( session, HslProtocol.CommandBytes( customer, Token, str ) );
  66. }
  67. /// <summary>
  68. /// 向指定的通信对象发送字节数据
  69. /// </summary>
  70. /// <param name="session">连接对象</param>
  71. /// <param name="customer">用户的指令头</param>
  72. /// <param name="bytes">实际的数据</param>
  73. public void SendMessage( AppSession session, int customer, byte[] bytes )
  74. {
  75. SendBytesAsync( session, HslProtocol.CommandBytes( customer, Token, bytes ) );
  76. }
  77. /// <summary>
  78. /// 处理请求接收连接后的方法
  79. /// </summary>
  80. /// <param name="obj"></param>
  81. protected override void ThreadPoolLogin( object obj )
  82. {
  83. if (obj is Socket)
  84. {
  85. Socket socket = obj as Socket;
  86. AppSession session = new AppSession( );
  87. session.WorkSocket = socket;
  88. try
  89. {
  90. session.IpEndPoint = (System.Net.IPEndPoint)socket.RemoteEndPoint;
  91. session.IpAddress = session.IpEndPoint.Address.ToString( );
  92. }
  93. catch(Exception ex)
  94. {
  95. LogNet?.WriteException( ToString( ), "Ip信息获取失败", ex );
  96. }
  97. LogNet?.WriteDebug( ToString( ), $"客户端 [ {session.IpEndPoint} ] 上线" );
  98. ReBeginReceiveHead( session, false );
  99. }
  100. }
  101. /// <summary>
  102. /// 处理异常的方法
  103. /// </summary>
  104. /// <param name="session"></param>
  105. /// <param name="ex">异常信息</param>
  106. internal override void SocketReceiveException( AppSession session, Exception ex )
  107. {
  108. session.WorkSocket?.Close( );
  109. LogNet?.WriteDebug( ToString( ), $"客户端 [ {session.IpEndPoint} ] 异常下线" );
  110. }
  111. /// <summary>
  112. /// 正常下线
  113. /// </summary>
  114. /// <param name="session"></param>
  115. internal override void AppSessionRemoteClose( AppSession session )
  116. {
  117. session.WorkSocket?.Close( );
  118. LogNet?.WriteDebug( ToString( ), $"客户端 [ {session.IpEndPoint} ] 下线" );
  119. }
  120. /// <summary>
  121. /// 数据处理中心
  122. /// </summary>
  123. /// <param name="session">当前的会话</param>
  124. /// <param name="protocol">协议指令头</param>
  125. /// <param name="customer">客户端信号</param>
  126. /// <param name="content">触发的消息内容</param>
  127. internal override void DataProcessingCenter( AppSession session, int protocol, int customer, byte[] content )
  128. {
  129. //接收数据完成,进行事件通知,优先进行解密操作
  130. if (protocol == HslProtocol.ProtocolCheckSecends)
  131. {
  132. // 初始化时候的测试消息
  133. session.HeartTime = DateTime.Now;
  134. SendMessage( session, customer, content );
  135. }
  136. else if (protocol == HslProtocol.ProtocolUserBytes)
  137. {
  138. // 字节数据
  139. OnReceivedBytesEvent( session, customer, content );
  140. }
  141. else if (protocol == HslProtocol.ProtocolUserString)
  142. {
  143. // 字符串数据
  144. OnReceiveStringEvent( session, customer, Encoding.Unicode.GetString( content ) );
  145. }
  146. else
  147. {
  148. // 数据异常
  149. session?.WorkSocket?.Close( );
  150. }
  151. }
  152. #endregion
  153. #region Object Override
  154. /// <summary>
  155. /// 获取本对象的字符串表示形式
  156. /// </summary>
  157. /// <returns></returns>
  158. public override string ToString( )
  159. {
  160. return "MelsecA1EAsciiServer";
  161. }
  162. #endregion
  163. protected override int GetReceiveLength(AppSession session)
  164. {
  165. int h = Convert.ToInt32(Encoding.ASCII.GetString(session.BytesHead, 0, 2), 16);
  166. if (h == 1)
  167. {
  168. return 2;
  169. }
  170. else
  171. {
  172. return Convert.ToInt32(Encoding.ASCII.GetString(session.BytesHead, 20, 2), 16) * 4 + 2;
  173. }
  174. }
  175. protected override int GetCustomer(byte[] head)
  176. {
  177. int h = Convert.ToInt32(Encoding.ASCII.GetString(head, 0, 2), 16);
  178. int c = Convert.ToInt32(Encoding.ASCII.GetString(head, 12, 8), 16);
  179. return (h == 1 ? c : 0-c);
  180. }
  181. }
  182. }