AsyncTCPClient.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading;
  6. namespace NetCore31BACNetTransfor
  7. {
  8. public class AsyncTCPClient : AsyncClientBase
  9. {
  10. private string address;
  11. private int port;
  12. private ILogger logger;
  13. private TcpClient tcpClient;
  14. private NetworkStream stream;
  15. private int bufferSize = 4096;
  16. private byte[] sendBuffer;
  17. private byte[] receiveBuffer;
  18. private TCPStream tcpstream;
  19. private bool separate;
  20. private bool separateBytes;
  21. private byte prefix;
  22. private byte suffix;
  23. private byte[] prefixBytes;
  24. private byte[] suffixBytes;
  25. private volatile bool started;
  26. private volatile bool connected;
  27. private volatile bool needStop;
  28. public AsyncTCPClient(string address, int port, ILogger logger) : base(1000)
  29. {
  30. this.address = address;
  31. this.port = port;
  32. this.logger = logger;
  33. this.sendBuffer = new byte[this.bufferSize];
  34. this.receiveBuffer = new byte[this.bufferSize];
  35. }
  36. public AsyncTCPClient(string address, int port, ILogger logger, int packetSize, int bufferSize) : base(packetSize)
  37. {
  38. this.address = address;
  39. this.port = port;
  40. this.bufferSize = bufferSize;
  41. this.logger = logger;
  42. this.sendBuffer = new byte[this.bufferSize];
  43. this.receiveBuffer = new byte[this.bufferSize];
  44. }
  45. public AsyncTCPClient(string address, int port, ILogger logger, int packetSize, int bufferSize, byte prefix, byte suffix) : base(packetSize)
  46. {
  47. this.address = address;
  48. this.port = port;
  49. this.bufferSize = bufferSize;
  50. this.logger = logger;
  51. this.sendBuffer = new byte[this.bufferSize];
  52. this.receiveBuffer = new byte[this.bufferSize];
  53. this.separate = true;
  54. this.separateBytes = false;
  55. this.prefix = prefix;
  56. this.suffix = suffix;
  57. this.tcpstream = new TCPStream(this.prefix, this.suffix);
  58. }
  59. public AsyncTCPClient(string address, int port, ILogger logger, int packetSize, int bufferSize, byte[] prefixBytes, byte[] suffixBytes) : base(packetSize)
  60. {
  61. this.address = address;
  62. this.port = port;
  63. this.bufferSize = bufferSize;
  64. this.logger = logger;
  65. this.sendBuffer = new byte[this.bufferSize];
  66. this.receiveBuffer = new byte[this.bufferSize];
  67. this.separate = true;
  68. this.separateBytes = true;
  69. this.prefixBytes = prefixBytes;
  70. this.suffixBytes = suffixBytes;
  71. this.tcpstream = new TCPStream(this.prefixBytes, this.suffixBytes);
  72. }
  73. public override bool IsConnected()
  74. {
  75. return this.connected;
  76. }
  77. public void RequestStop()
  78. {
  79. this.needStop = true;
  80. }
  81. public override void Start()
  82. {
  83. if (!this.started)
  84. {
  85. Thread thread = new Thread(new ThreadStart(this.ProcessLoop));
  86. thread.Start();
  87. this.started = true;
  88. }
  89. }
  90. public override void Stop()
  91. {
  92. this.needStop = true;
  93. }
  94. private void ProcessLoop()
  95. {
  96. while (!this.needStop)
  97. {
  98. if (!this.connected)
  99. {
  100. try
  101. {
  102. this.tcpClient = new TcpClient();
  103. this.tcpClient.Connect(this.address, this.port);
  104. this.connected = true;
  105. this.stream = this.tcpClient.GetStream();
  106. this.stream.BeginRead(this.receiveBuffer, 0, this.receiveBuffer.Length, new AsyncCallback(this.ReceiveCallback), this.stream);
  107. this.logger.Info(string.Concat(new object[]
  108. {
  109. "Started ",
  110. this.address,
  111. ":",
  112. this.port
  113. }));
  114. }
  115. catch (Exception ex)
  116. {
  117. this.logger.Error("Connect Error:" + ex.Message);
  118. Thread.Sleep(1000);
  119. }
  120. }
  121. if (this.connected)
  122. {
  123. try
  124. {
  125. string text = base.PopSend();
  126. if (text != null)
  127. {
  128. byte[] bytes = Encoding.UTF8.GetBytes(text);
  129. byte[] array = bytes;
  130. if (this.separate)
  131. {
  132. if (this.separateBytes)
  133. {
  134. array = new byte[this.prefixBytes.Length + bytes.Length + this.suffixBytes.Length];
  135. Array.Copy(this.prefixBytes, 0, array, 0, this.prefixBytes.Length);
  136. Array.Copy(bytes, 0, array, this.prefixBytes.Length, bytes.Length);
  137. Array.Copy(this.suffixBytes, 0, array, this.prefixBytes.Length + bytes.Length, this.suffixBytes.Length);
  138. }
  139. else
  140. {
  141. array = new byte[1 + bytes.Length + 1];
  142. array[0] = this.prefix;
  143. Array.Copy(bytes, 0, array, 1, bytes.Length);
  144. array[1 + bytes.Length] = this.suffix;
  145. }
  146. }
  147. this.stream.BeginWrite(array, 0, array.Length, new AsyncCallback(this.SendCallback), text);
  148. Thread.Sleep(10);
  149. }
  150. }
  151. catch (Exception ex2)
  152. {
  153. if (this.tcpClient != null)
  154. {
  155. try
  156. {
  157. this.tcpClient.Close();
  158. }
  159. catch (Exception)
  160. {
  161. }
  162. }
  163. if (this.stream != null)
  164. {
  165. try
  166. {
  167. this.stream.Close();
  168. }
  169. catch (Exception)
  170. {
  171. }
  172. }
  173. this.connected = false;
  174. this.logger.Error("Loop Error:" + ex2.Message);
  175. }
  176. }
  177. Thread.Sleep(1);
  178. }
  179. if (this.tcpClient != null)
  180. {
  181. try
  182. {
  183. this.tcpClient.Close();
  184. }
  185. catch (Exception)
  186. {
  187. }
  188. }
  189. if (this.stream != null)
  190. {
  191. try
  192. {
  193. this.stream.Close();
  194. }
  195. catch (Exception)
  196. {
  197. }
  198. }
  199. this.connected = false;
  200. this.started = false;
  201. }
  202. public void SendCallback(IAsyncResult iar)
  203. {
  204. try
  205. {
  206. string str = (string)iar.AsyncState;
  207. this.stream.EndWrite(iar);
  208. this.logger.Info("send:" + str);
  209. }
  210. catch (Exception ex)
  211. {
  212. this.connected = false;
  213. this.logger.Error("SendCallback Error:" + ex.Message);
  214. }
  215. }
  216. public void ReceiveCallback(IAsyncResult iar)
  217. {
  218. try
  219. {
  220. int num = this.stream.EndRead(iar);
  221. if (num > 0)
  222. {
  223. List<byte[]> list = this.tcpstream.Process(this.receiveBuffer, num);
  224. foreach (byte[] current in list)
  225. {
  226. string @string = Encoding.UTF8.GetString(current);
  227. base.PushReceive(@string);
  228. this.logger.Info("rece:" + @string);
  229. }
  230. }
  231. this.stream.BeginRead(this.receiveBuffer, 0, this.receiveBuffer.Length, new AsyncCallback(this.ReceiveCallback), this.stream);
  232. }
  233. catch (Exception ex)
  234. {
  235. this.connected = false;
  236. this.logger.Error("ReceiveCallback Error:" + ex.Message);
  237. }
  238. }
  239. }
  240. }