AsyncUdpSever.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 
  2. using System;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading;
  7. namespace NetCore31BACNetTransfor
  8. {
  9. public class AsyncUdpSever : AsyncServerBase
  10. {
  11. private IPEndPoint ipEndPoint;
  12. private bool isAsync;
  13. private ILogger logger;
  14. private UdpClient udpReceive;
  15. private UdpClient udpSend;
  16. private ManualResetEvent sendDone = new ManualResetEvent(false);
  17. private ManualResetEvent receiveDone = new ManualResetEvent(false);
  18. public AsyncUdpSever(IPEndPoint ipEndPoint, bool isAsync, ILogger logger) : base(1000)
  19. {
  20. this.ipEndPoint = ipEndPoint;
  21. this.isAsync = isAsync;
  22. this.logger = logger;
  23. }
  24. public AsyncUdpSever(IPEndPoint ipEndPoint, bool isAsync, ILogger logger, int packetSize) : base(packetSize)
  25. {
  26. this.ipEndPoint = ipEndPoint;
  27. this.isAsync = isAsync;
  28. this.logger = logger;
  29. }
  30. public void Start()
  31. {
  32. this.udpReceive = new UdpClient(this.ipEndPoint);
  33. this.udpSend = new UdpClient();
  34. this.logger.Info("Started " + this.ipEndPoint.ToString());
  35. Thread thread = new Thread(new ThreadStart(this.ReceiveLoop));
  36. thread.Start();
  37. Thread thread2 = new Thread(new ThreadStart(this.SendLoop));
  38. thread2.Start();
  39. }
  40. public void Stop()
  41. {
  42. if (this.udpReceive != null)
  43. {
  44. this.udpReceive.Close();
  45. }
  46. if (this.udpSend != null)
  47. {
  48. this.udpSend.Close();
  49. }
  50. }
  51. private void ReceiveLoop()
  52. {
  53. while (true)
  54. {
  55. if (this.isAsync)
  56. {
  57. this.Receive();
  58. }
  59. else
  60. {
  61. this.ReceiveSync();
  62. }
  63. Thread.Sleep(1);
  64. }
  65. }
  66. private void SendLoop()
  67. {
  68. while (true)
  69. {
  70. AsyncServerBag asyncServerBag = base.PopSend();
  71. if (asyncServerBag != null)
  72. {
  73. if (this.isAsync)
  74. {
  75. this.Send(asyncServerBag);
  76. }
  77. else
  78. {
  79. this.SendSync(asyncServerBag);
  80. }
  81. }
  82. Thread.Sleep(1);
  83. }
  84. }
  85. private void ReceiveSync()
  86. {
  87. try
  88. {
  89. IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
  90. byte[] bytes = this.udpReceive.Receive(ref remoteEP);
  91. AsyncServerBag asyncServerBag = new AsyncServerBag();
  92. asyncServerBag.remoteEP = remoteEP;
  93. asyncServerBag.bag = Encoding.UTF8.GetString(bytes);
  94. base.PushReceive(asyncServerBag);
  95. this.logger.Info("rece:" + asyncServerBag.remoteEP.ToString() + " " + asyncServerBag.bag);
  96. }
  97. catch (Exception ex)
  98. {
  99. this.logger.Error(ex.Message);
  100. }
  101. }
  102. private void SendSync(AsyncServerBag bag)
  103. {
  104. try
  105. {
  106. byte[] bytes = Encoding.UTF8.GetBytes(bag.bag);
  107. this.udpReceive.Send(bytes, bytes.Length, bag.remoteEP);
  108. this.logger.Info("send:" + bag.remoteEP.ToString() + " " + bag.bag);
  109. }
  110. catch (Exception ex)
  111. {
  112. this.logger.Error(ex.Message);
  113. }
  114. }
  115. private void Receive()
  116. {
  117. this.udpReceive.BeginReceive(new AsyncCallback(this.ReceiveCallback), this.udpReceive);
  118. this.receiveDone.WaitOne();
  119. this.receiveDone.Reset();
  120. }
  121. private void ReceiveCallback(IAsyncResult iar)
  122. {
  123. UdpClient udpClient = iar.AsyncState as UdpClient;
  124. IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
  125. byte[] bytes = udpClient.EndReceive(iar, ref remoteEP);
  126. this.receiveDone.Set();
  127. AsyncServerBag asyncServerBag = new AsyncServerBag();
  128. asyncServerBag.remoteEP = remoteEP;
  129. asyncServerBag.bag = Encoding.UTF8.GetString(bytes);
  130. base.PushReceive(asyncServerBag);
  131. this.logger.Info("rece:" + asyncServerBag.remoteEP.ToString() + " " + asyncServerBag.bag);
  132. }
  133. private void Send(AsyncServerBag bag)
  134. {
  135. this.udpSend.Connect(bag.remoteEP);
  136. byte[] bytes = Encoding.UTF8.GetBytes(bag.bag);
  137. this.udpSend.BeginSend(bytes, bytes.Length, new AsyncCallback(this.SendCallback), this.udpSend);
  138. this.sendDone.WaitOne();
  139. this.sendDone.Reset();
  140. this.logger.Info("send:" + bag.remoteEP.ToString() + " " + bag.bag);
  141. }
  142. private void SendCallback(IAsyncResult iar)
  143. {
  144. UdpClient udpClient = iar.AsyncState as UdpClient;
  145. udpClient.EndSend(iar);
  146. this.sendDone.Set();
  147. }
  148. }
  149. }