JetTcpClient.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.Utilities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MECF.Framework.Common.Net
  12. {
  13. public class JetTcpClient
  14. {
  15. #region 内部变量
  16. private string _ip="127.0.0.1";
  17. private int _port = 9600;
  18. private bool _connected = false;
  19. protected int _reconnectInterval = 500;
  20. protected DateTime _connectTime = DateTime.Now;
  21. private int _connectTimeout = 2000;
  22. private int _receiveTimeout = 1000;
  23. private int _sendTimeout = 1000;
  24. private object _connectLocker=new object();
  25. private object _reconnectLocker = new object();
  26. private object _sendLocker = new object();
  27. private object _receiveLocker = new object();
  28. private object _closeLocker = new object();
  29. private AutoResetEvent _connectAutoResetEvent=new AutoResetEvent(false);
  30. private Socket _socket = null;
  31. #endregion
  32. #region 属性
  33. /// <summary>
  34. /// 重连间隔时长
  35. /// </summary>
  36. public int ReconnectInterval { set { _reconnectInterval = value; } }
  37. /// <summary>
  38. /// 连接超时时长
  39. /// </summary>
  40. public int ConnectTimeout { set { _connectTimeout = value; } }
  41. /// <summary>
  42. /// 接收超时时长
  43. /// </summary>
  44. public int ReceiveTimeout { set { _receiveTimeout = value; } }
  45. /// <summary>
  46. /// 发送超时时长
  47. /// </summary>
  48. public int SendTimeout { set { _sendTimeout = value; } }
  49. /// <summary>
  50. /// 连接状态
  51. /// </summary>
  52. public bool Connected { get { return _connected; } }
  53. #endregion
  54. /// <summary>
  55. /// 构造函数
  56. /// </summary>
  57. /// <param name="ip"></param>
  58. /// <param name="port"></param>
  59. public JetTcpClient(string ip,int port)
  60. {
  61. _ip= ip;
  62. _port= port;
  63. }
  64. /// <summary>
  65. /// 连接
  66. /// </summary>
  67. public NetResult Connect()
  68. {
  69. if (_connected)
  70. {
  71. return NetResult.CreateSuccessResult();
  72. }
  73. if(!Monitor.TryEnter(_connectLocker,_connectTimeout))
  74. {
  75. return NetResult.CreateFailedResult(NetErrorCode.LockerOccupied);
  76. }
  77. _connectTime = DateTime.Now;
  78. _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  79. _socket.ReceiveTimeout = _receiveTimeout;
  80. _socket.SendTimeout=_sendTimeout;
  81. NetStateObject stateObject=new NetStateObject();
  82. stateObject.Socket = _socket;
  83. stateObject.AutoResetEvent = _connectAutoResetEvent;
  84. EndPoint localEndpoint = _socket.LocalEndPoint;
  85. if(IPAddress.TryParse(_ip,out IPAddress ipAddress))
  86. {
  87. try
  88. {
  89. _socket.BeginConnect(new IPEndPoint(ipAddress, _port), new AsyncCallback(ConnectCallBack), stateObject);
  90. }
  91. catch(Exception ex)
  92. {
  93. stateObject.Dispose();
  94. stateObject = null;
  95. ConnectFailedBusiness();
  96. Monitor.Exit(_connectLocker);
  97. return NetResult.CreateFailedResult((int)NetErrorCode.InnerException, ex.Message);
  98. }
  99. if (!_connectAutoResetEvent.WaitOne(_connectTimeout))
  100. {
  101. stateObject.Dispose();
  102. stateObject = null;
  103. ConnectFailedBusiness();
  104. Monitor.Exit(_connectLocker);
  105. return NetResult.CreateFailedResult(NetErrorCode.ConnectTimeout);
  106. }
  107. LOG.WriteLog(eEvent.EV_DEVICE_INFO, "System", $"Connect {_ip}:{_port} Success,local {localEndpoint}");
  108. _connected = true;
  109. stateObject.Dispose();
  110. stateObject = null;
  111. Monitor.Exit(_connectLocker);
  112. return NetResult.CreateSuccessResult();
  113. }
  114. else
  115. {
  116. stateObject.Dispose();
  117. stateObject = null;
  118. ConnectFailedBusiness();
  119. Monitor.Exit(_connectLocker);
  120. return NetResult.CreateFailedResult(NetErrorCode.InvalidIpAddress);
  121. }
  122. }
  123. /// <summary>
  124. /// 中止
  125. /// </summary>
  126. public void Stop()
  127. {
  128. CloseSocket();
  129. }
  130. /// <summary>
  131. /// 连接失败后处理
  132. /// </summary>
  133. /// <param name="stateObject"></param>
  134. private void ConnectFailedBusiness()
  135. {
  136. _connectAutoResetEvent.Reset();
  137. CloseSocket();
  138. }
  139. /// <summary>
  140. /// 关闭Socket
  141. /// </summary>
  142. private void CloseSocket()
  143. {
  144. if (Monitor.TryEnter(_closeLocker, 10))
  145. {
  146. if (_socket != null)
  147. {
  148. try
  149. {
  150. _socket.Shutdown(SocketShutdown.Both);
  151. }
  152. catch
  153. {
  154. }
  155. try
  156. {
  157. _socket.Close();
  158. }
  159. catch
  160. {
  161. }
  162. _socket = null;
  163. _connected = false;
  164. }
  165. Monitor.Exit(_closeLocker);
  166. }
  167. }
  168. /// <summary>
  169. /// 当连接的结果返回
  170. /// </summary>
  171. /// <param name="ar">异步对象</param>
  172. private void ConnectCallBack(IAsyncResult ar)
  173. {
  174. if (ar.AsyncState is NetStateObject state)
  175. {
  176. if (state.Socket != null)
  177. {
  178. try
  179. {
  180. state.Socket.EndConnect(ar);
  181. state.AutoResetEvent.Set();
  182. }
  183. catch (Exception ex)
  184. {
  185. }
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 发送数据
  191. /// </summary>
  192. /// <param name="data"></param>
  193. /// <returns></returns>
  194. public NetResult Send(byte[] data)
  195. {
  196. if(!Connected)
  197. {
  198. return NetResult.CreateFailedResult(NetErrorCode.NetOffline);
  199. }
  200. //清除缓存数据
  201. ClearPreData();
  202. //进入发送
  203. if (Monitor.TryEnter(_sendLocker,_sendTimeout))
  204. {
  205. if(_socket==null)
  206. {
  207. return NetResult.CreateFailedResult(NetErrorCode.NullSocketObject);
  208. }
  209. try
  210. {
  211. _socket.Send(data);
  212. Monitor.Exit(_sendLocker);
  213. return NetResult.CreateSuccessResult();
  214. }
  215. catch (Exception ex)
  216. {
  217. Monitor.Exit(_sendLocker);
  218. ConnectFailedBusiness();
  219. return NetResult.CreateFailedResult((int)NetErrorCode.InnerException, ex.Message);
  220. }
  221. }
  222. else
  223. {
  224. return NetResult.CreateFailedResult(NetErrorCode.GetLockTimeout);
  225. }
  226. }
  227. /// <summary>
  228. /// 接收数据
  229. /// </summary>
  230. /// <param name="length"></param>
  231. /// <returns></returns>
  232. public NetResult<byte[]> Receive(int length)
  233. {
  234. if (!Connected)
  235. {
  236. return NetResult.CreateFailedResult<byte[]>(NetErrorCode.NetOffline);
  237. }
  238. if (Monitor.TryEnter(_receiveLocker,_receiveTimeout))
  239. {
  240. if(_socket==null)
  241. {
  242. return NetResult.CreateFailedResult<byte[]>(NetErrorCode.NullSocketObject);
  243. }
  244. try
  245. {
  246. byte[] buffer = null;
  247. if (length == -1)
  248. {
  249. buffer = new byte[_socket.Available];
  250. }
  251. else
  252. {
  253. buffer = new byte[length];
  254. }
  255. _socket.Receive(buffer, length, SocketFlags.None);
  256. Monitor.Exit(_receiveLocker);
  257. return NetResult.CreateSuccessResult<byte[]>(buffer);
  258. }
  259. catch(SocketException ex)
  260. {
  261. Monitor.Exit(_receiveLocker);
  262. ConnectFailedBusiness();
  263. return NetResult.CreateFailedResult<byte[]>((int)NetErrorCode.InnerException, ex.Message);
  264. }
  265. catch (Exception ex)
  266. {
  267. Monitor.Exit(_receiveLocker);
  268. ConnectFailedBusiness();
  269. return NetResult.CreateFailedResult<byte[]>((int)NetErrorCode.InnerException, ex.Message);
  270. }
  271. }
  272. else
  273. {
  274. return NetResult.CreateFailedResult<byte[]>(NetErrorCode.GetLockTimeout);
  275. }
  276. }
  277. /// <summary>
  278. /// 清除先前的数据
  279. /// </summary>
  280. public void ClearPreData()
  281. {
  282. if (!Connected)
  283. {
  284. return;
  285. }
  286. if (Monitor.TryEnter(_receiveLocker, _receiveTimeout))
  287. {
  288. if (_socket == null)
  289. {
  290. return;
  291. }
  292. try
  293. {
  294. while (_socket.Available != 0)
  295. {
  296. byte[] buffer = new byte[_socket.Available];
  297. _socket.Receive(buffer, buffer.Length, SocketFlags.None);
  298. }
  299. Monitor.Exit(_receiveLocker);
  300. }
  301. catch (SocketException ex)
  302. {
  303. Monitor.Exit(_receiveLocker);
  304. ConnectFailedBusiness();
  305. }
  306. catch (Exception ex)
  307. {
  308. Monitor.Exit(_receiveLocker);
  309. ConnectFailedBusiness();
  310. }
  311. }
  312. }
  313. }
  314. }