AsyncUdpClient.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading;
  6. namespace NetCore31BACNetTransfor
  7. {
  8. public class AsyncUdpClient : AsyncClientBase
  9. {
  10. private IPEndPoint localEP;
  11. private IPEndPoint remoteEP;
  12. private bool isAsync;
  13. private ILogger logger;
  14. private UdpClient udpClient;
  15. public bool shouldStop;
  16. public bool receiveStopped;
  17. public bool sendStopped;
  18. private bool WaitOne_Send;
  19. private bool WaitOne_Rece;
  20. public bool V { get; }
  21. public AsyncUdpClient(IPEndPoint localEP, IPEndPoint remoteEP, bool isAsync, ILogger logger) : base(1000)
  22. {
  23. this.localEP = localEP;
  24. this.remoteEP = remoteEP;
  25. this.isAsync = isAsync;
  26. this.logger = logger;
  27. }
  28. public AsyncUdpClient(IPEndPoint localEP, IPEndPoint remoteEP, bool isAsync, ILogger logger, int packetSize) : base(packetSize)
  29. {
  30. this.localEP = localEP;
  31. this.remoteEP = remoteEP;
  32. this.isAsync = isAsync;
  33. this.logger = logger;
  34. }
  35. public override bool IsConnected()
  36. {
  37. return false;
  38. }
  39. public override void Start()
  40. {
  41. this.udpClient = new UdpClient(this.localEP);
  42. this.udpClient.Connect(this.remoteEP);
  43. this.logger.Info("Started " + this.localEP.ToString() + " " + this.remoteEP.ToString());
  44. Thread thread = new Thread(new ThreadStart(this.ReceiveLoop));
  45. thread.Start();
  46. Thread thread2 = new Thread(new ThreadStart(this.SendLoop));
  47. thread2.Start();
  48. }
  49. public override void Stop()
  50. {
  51. this.shouldStop = true;
  52. while (!this.sendStopped || !this.receiveStopped)
  53. {
  54. Thread.Sleep(1);
  55. }
  56. if (this.udpClient != null)
  57. {
  58. this.udpClient.Close();
  59. }
  60. }
  61. private void ReceiveLoop()
  62. {
  63. this.receiveStopped = false;
  64. while (!this.shouldStop)
  65. {
  66. if (this.isAsync)
  67. {
  68. this.Receive();
  69. }
  70. else
  71. {
  72. this.ReceiveSync();
  73. }
  74. Thread.Sleep(1);
  75. }
  76. this.receiveStopped = true;
  77. }
  78. private void SendLoop()
  79. {
  80. this.sendStopped = false;
  81. while (!this.shouldStop)
  82. {
  83. string text = base.PopSend();
  84. if (text != null)
  85. {
  86. if (this.isAsync)
  87. {
  88. this.Send(text);
  89. }
  90. else
  91. {
  92. this.SendSync(text);
  93. }
  94. }
  95. Thread.Sleep(1);
  96. }
  97. this.sendStopped = true;
  98. }
  99. private void ReceiveSync()
  100. {
  101. try
  102. {
  103. IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 0);
  104. byte[] bytes = this.udpClient.Receive(ref iPEndPoint);
  105. string str = Encoding.UTF8.GetString(bytes);
  106. base.PushReceive(str);
  107. this.logger.Info("rece:" + str);
  108. }
  109. catch (Exception ex)
  110. {
  111. this.logger.Error(ex.Message);
  112. }
  113. }
  114. private void SendSync(string bag)
  115. {
  116. try
  117. {
  118. byte[] bytes = Encoding.UTF8.GetBytes(bag);
  119. this.udpClient.Send(bytes, bytes.Length);
  120. this.logger.Info("send:" + bag);
  121. }
  122. catch (Exception ex)
  123. {
  124. this.logger.Error(ex.Message);
  125. }
  126. }
  127. private void Send(string msg)
  128. {
  129. byte[] bytes = Encoding.UTF8.GetBytes(msg);
  130. this.WaitOne_Send = false;
  131. this.udpClient.BeginSend(bytes, bytes.Length, new AsyncCallback(this.SendCallback), this.udpClient);
  132. while (!this.shouldStop)
  133. {
  134. if (this.WaitOne_Send)
  135. {
  136. this.logger.Info("send:" + msg);
  137. return;
  138. }
  139. Thread.Sleep(1);
  140. }
  141. }
  142. public void SendCallback(IAsyncResult iar)
  143. {
  144. UdpClient udpClient = iar.AsyncState as UdpClient;
  145. try
  146. {
  147. udpClient.EndSend(iar);
  148. this.WaitOne_Send = true;
  149. }
  150. catch (Exception)
  151. {
  152. }
  153. }
  154. public void Receive()
  155. {
  156. this.WaitOne_Rece = false;
  157. this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), this.udpClient);
  158. while (!this.shouldStop)
  159. {
  160. if (this.WaitOne_Rece)
  161. {
  162. return;
  163. }
  164. Thread.Sleep(1);
  165. }
  166. }
  167. public void ReceiveCallback(IAsyncResult iar)
  168. {
  169. UdpClient udpClient = iar.AsyncState as UdpClient;
  170. try
  171. {
  172. IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Any, 0);
  173. byte[] bytes = udpClient.EndReceive(iar, ref iPEndPoint);
  174. this.WaitOne_Rece = true;
  175. string str = Encoding.UTF8.GetString(bytes);
  176. base.PushReceive(str);
  177. this.logger.Info("rece:" + str);
  178. }
  179. catch (Exception)
  180. {
  181. }
  182. }
  183. }
  184. }