UdpServerProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. namespace IFramework.Net.Udp
  6. {
  7. internal class UdpServerProvider : UdpSocket, IDisposable, IUdpServerProvider
  8. {
  9. #region variable
  10. private SocketReceive socketRecieve = null;
  11. private SocketSend socketSend = null;
  12. private bool _isDisposed = false;
  13. private Encoding encoding = Encoding.UTF8;
  14. private int bufferSizeByConnection = 4096;
  15. private int maxNumberOfConnections = 8;
  16. #endregion
  17. #region property
  18. public OnReceivedSegmentHandler ReceivedOffsetHanlder { get; set; }
  19. /// <summary>
  20. /// 接收事件响应回调
  21. /// </summary>
  22. public OnReceivedHandler ReceivedCallbackHandler { get; set; }
  23. /// <summary>
  24. /// 发送事件响应回调
  25. /// </summary>
  26. public OnSentHandler SentCallbackHandler { get; set; }
  27. /// <summary>
  28. /// 断开连接事件回调
  29. /// </summary>
  30. public OnDisconnectedHandler DisconnectedCallbackHandler { get; set; }
  31. #endregion
  32. #region structure
  33. public void Dispose()
  34. {
  35. Dispose(true);
  36. GC.SuppressFinalize(this);
  37. }
  38. protected virtual void Dispose(bool isDisposing)
  39. {
  40. if (_isDisposed) return;
  41. if (isDisposing)
  42. {
  43. socketRecieve.Dispose();
  44. socketSend.Dispose();
  45. _isDisposed = true;
  46. }
  47. }
  48. /// <summary>
  49. /// 构造方法
  50. /// </summary>
  51. public UdpServerProvider(int bufferSizeByConnection, int maxNumberOfConnections,bool Broadcast=false)
  52. :base(bufferSizeByConnection,Broadcast)
  53. {
  54. this.bufferSizeByConnection = bufferSizeByConnection;
  55. this.maxNumberOfConnections = maxNumberOfConnections;
  56. }
  57. #endregion
  58. #region public method
  59. /// <summary>
  60. /// 启动服务
  61. /// </summary>
  62. /// <param name="port">接收数据端口</param>
  63. /// <param name="recBufferSize">接收缓冲区</param>
  64. /// <param name="maxConnectionCount">最大客户端连接数</param>
  65. public void Start(int port)
  66. {
  67. socketRecieve = new SocketReceive(port, bufferSizeByConnection,
  68. Broadcast);
  69. socketRecieve.OnReceived += receiveSocket_OnReceived;
  70. socketRecieve.StartReceive();
  71. socketSend = new SocketSend(socketRecieve.socket, maxNumberOfConnections, bufferSizeByConnection);
  72. socketSend.SentEventHandler += sendSocket_SentEventHandler;
  73. }
  74. /// <summary>
  75. /// 停止服务
  76. /// </summary>
  77. public void Stop()
  78. {
  79. if (socketSend != null)
  80. {
  81. socketSend.Dispose();
  82. }
  83. if (socketRecieve != null)
  84. {
  85. socketRecieve.StopReceive();
  86. }
  87. }
  88. public bool Send(SegmentOffset dataSegment,IPEndPoint remoteEP ,bool waiting = true)
  89. {
  90. return socketSend.Send(dataSegment, remoteEP, waiting);
  91. }
  92. public int SendSync(IPEndPoint remoteEP, SegmentOffset dataSegment)
  93. {
  94. return socketSend.SendSync(dataSegment , remoteEP);
  95. }
  96. #endregion
  97. #region private method
  98. private void sendSocket_SentEventHandler(object sender, SocketAsyncEventArgs e)
  99. {
  100. if (SentCallbackHandler != null)
  101. {
  102. SentCallbackHandler(new SegmentToken(new SocketToken()
  103. {
  104. TokenIpEndPoint = (IPEndPoint)e.RemoteEndPoint
  105. }, e.Buffer, e.Offset, e.BytesTransferred));
  106. }
  107. }
  108. private void receiveSocket_OnReceived(object sender, SocketAsyncEventArgs e)
  109. {
  110. SocketToken sToken = new SocketToken()
  111. {
  112. TokenIpEndPoint = (IPEndPoint)e.RemoteEndPoint
  113. };
  114. if (ReceivedOffsetHanlder != null)
  115. ReceivedOffsetHanlder(new SegmentToken(sToken, e.Buffer, e.Offset, e.BytesTransferred));
  116. if (ReceivedCallbackHandler != null
  117. && e.BytesTransferred > 0)
  118. {
  119. ReceivedCallbackHandler(sToken, encoding.GetString(e.Buffer, e.Offset, e.BytesTransferred));
  120. }
  121. }
  122. #endregion
  123. }
  124. }