You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. namespace Apewer.Network
  9. {
  10. /// <summary>UDP 服务端。</summary>
  11. public class UdpServer : IDisposable
  12. {
  13. /// <summary>释放资源。</summary>
  14. public void Dispose() { Quit(); }
  15. private Thread _thread = null;
  16. private System.Net.Sockets.UdpClient _udp = null;
  17. private string _address = "0.0.0.0";
  18. private int _port = 0;
  19. /// <summary>Exception。</summary>
  20. public Event<Exception> Excepted { get; set; }
  21. /// <summary>服务端已启动。</summary>
  22. public Event Started { get; set; }
  23. /// <summary>服务端已关闭。</summary>
  24. public Event Quitted { get; set; }
  25. /// <summary>已收到客户端数据。</summary>
  26. public Event<SocketReceived> Received { get; set; }
  27. /// <summary>构造函数。</summary>
  28. public UdpServer()
  29. {
  30. }
  31. /// <summary>服务端是否正在运行。</summary>
  32. public bool Alive
  33. {
  34. get { return (_thread != null) ? _thread.IsAlive : false; }
  35. }
  36. /// <summary>指定监听地址,默认为 0.0.0.0。</summary>
  37. public string Address
  38. {
  39. get { return _address; }
  40. set { if (!Alive) _address = string.IsNullOrEmpty(value) ? "" : value; }
  41. }
  42. /// <summary>获取或设置服务端端口。</summary>
  43. public int Port
  44. {
  45. get { return _port; }
  46. set
  47. {
  48. int vp = value;
  49. if (vp < 0) vp = 0;
  50. if (vp > 65535) vp = 65535;
  51. if (!Alive) _port = vp;
  52. }
  53. }
  54. /// <summary>启动服务端。</summary>
  55. public void Start()
  56. {
  57. Quit();
  58. var isIP = NetworkUtility.IsIP(_address);
  59. if (isIP && (Port > 0))
  60. {
  61. _thread = new Thread(Listener);
  62. _thread.IsBackground = true;
  63. _thread.Start();
  64. }
  65. }
  66. /// <summary>关闭服务端。</summary>
  67. public void Quit()
  68. {
  69. if (_thread != null)
  70. {
  71. if (_thread.IsAlive) _thread.Abort();
  72. _thread = null;
  73. }
  74. if (_udp != null)
  75. {
  76. _udp.Close();
  77. _udp = null;
  78. Quitted?.Invoke(this);
  79. }
  80. }
  81. private void Listener()
  82. {
  83. try
  84. {
  85. var ep = new IPEndPoint(IPAddress.Any, Port);
  86. _udp = new System.Net.Sockets.UdpClient(ep);
  87. Started?.Invoke(this);
  88. while (true)
  89. {
  90. var bytes = _udp.Receive(ref ep);
  91. if ((Received != null) && (bytes.Length > 0))
  92. {
  93. var ip = ep.Address.ToString();
  94. var port = ep.Port;
  95. Received?.Invoke(this, new SocketReceived(ip, ep.Port, bytes));
  96. }
  97. // Thread.Sleep(1);
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. if (Excepted != null) Excepted(this, ex);
  103. }
  104. Quitted.Invoke(this);
  105. }
  106. }
  107. }