SocketReceive.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Net;
  2. using System;
  3. using System.Net.Sockets;
  4. namespace IFramework.Net.Udp
  5. {
  6. internal class SocketReceive : UdpSocket, IDisposable
  7. {
  8. #region variable
  9. private SocketAsyncEventArgs recArgs = null;
  10. private bool isStoped = false;
  11. private bool _isDisposed = false;
  12. /// <summary>
  13. /// 接收事件
  14. /// </summary>
  15. public event EventHandler<SocketAsyncEventArgs> OnReceived;
  16. #endregion
  17. #region structure
  18. /// <summary>
  19. /// 构造方法
  20. /// </summary>
  21. /// <param name="port">本机接收数据端口</param>
  22. /// <param name="bufferSize">接收缓冲区大小</param>
  23. public SocketReceive(int port, int bufferSize = 4096,
  24. bool Broadcast = false)
  25. : base(bufferSize, Broadcast)
  26. {
  27. CreateUdpSocket(port, IPAddress.Any);
  28. socket.Bind(ipEndPoint);
  29. recArgs = new SocketAsyncEventArgs();
  30. recArgs.UserToken = socket;
  31. recArgs.RemoteEndPoint = socket.LocalEndPoint;
  32. recArgs.Completed += SocketArgs_Completed;
  33. recArgs.SetBuffer(receiveBuffer, 0, receiveChunkSize);
  34. }
  35. public SocketReceive(Socket socket, int bufferSize = 4096)
  36. : base(bufferSize)
  37. {
  38. this.socket = socket;
  39. recArgs = new SocketAsyncEventArgs();
  40. recArgs.UserToken = socket;
  41. recArgs.RemoteEndPoint = socket.LocalEndPoint;
  42. recArgs.Completed += SocketArgs_Completed;
  43. recArgs.SetBuffer(receiveBuffer, 0, receiveChunkSize);
  44. }
  45. public void Dispose()
  46. {
  47. Dispose(true);
  48. GC.SuppressFinalize(this);
  49. }
  50. protected virtual void Dispose(bool isDisposing)
  51. {
  52. if (_isDisposed) return;
  53. if (isDisposing)
  54. {
  55. isStoped = true;
  56. _isDisposed = true;
  57. socket.Dispose();
  58. recArgs.Dispose();
  59. }
  60. }
  61. #endregion
  62. #region public
  63. /// <summary>
  64. /// 开始接收数据
  65. /// </summary>
  66. public void StartReceive()
  67. {
  68. bool rt = socket.ReceiveFromAsync(recArgs);
  69. if (rt == false)
  70. {
  71. ProcessReceive(recArgs);
  72. }
  73. }
  74. /// <summary>
  75. /// 停止接收数据
  76. /// </summary>
  77. public void StopReceive()
  78. {
  79. isStoped = true;
  80. socket.Dispose();
  81. if (recArgs != null)
  82. {
  83. recArgs.Dispose();
  84. }
  85. }
  86. #endregion
  87. #region private
  88. /// <summary>
  89. /// 接收完成事件
  90. /// </summary>
  91. /// <param name="sender"></param>
  92. /// <param name="e"></param>
  93. private void SocketArgs_Completed(object sender, SocketAsyncEventArgs e)
  94. {
  95. switch (e.LastOperation)
  96. {
  97. case SocketAsyncOperation.ReceiveFrom:
  98. this.ProcessReceive(e);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. /// <summary>
  105. /// 处理接收信息
  106. /// </summary>
  107. /// <param name="arg"></param>
  108. private void ProcessReceive(SocketAsyncEventArgs arg)
  109. {
  110. // receivePool.Set(args);
  111. if (arg.BytesTransferred > 0
  112. && arg.SocketError == SocketError.Success)
  113. {
  114. if (OnReceived != null)
  115. {
  116. OnReceived(arg.UserToken as Socket, arg);
  117. }
  118. }
  119. if (isStoped) return;
  120. StartReceive();
  121. }
  122. #endregion
  123. }
  124. }