SocketAcceptor.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Net.Sockets;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Model.Net;
  5. namespace Emby.Common.Implementations.Net
  6. {
  7. public class SocketAcceptor
  8. {
  9. private readonly ILogger _logger;
  10. private readonly Socket _originalSocket;
  11. private readonly Func<bool> _isClosed;
  12. private readonly Action<ISocket> _onAccept;
  13. public SocketAcceptor(ILogger logger, Socket originalSocket, Action<ISocket> onAccept, Func<bool> isClosed)
  14. {
  15. if (logger == null)
  16. {
  17. throw new ArgumentNullException("logger");
  18. }
  19. if (originalSocket == null)
  20. {
  21. throw new ArgumentNullException("originalSocket");
  22. }
  23. if (onAccept == null)
  24. {
  25. throw new ArgumentNullException("onAccept");
  26. }
  27. if (isClosed == null)
  28. {
  29. throw new ArgumentNullException("isClosed");
  30. }
  31. _logger = logger;
  32. _originalSocket = originalSocket;
  33. _isClosed = isClosed;
  34. _onAccept = onAccept;
  35. }
  36. public void StartAccept()
  37. {
  38. Socket dummy = null;
  39. StartAccept(null, ref dummy);
  40. }
  41. public void StartAccept(SocketAsyncEventArgs acceptEventArg, ref Socket accepted)
  42. {
  43. if (acceptEventArg == null)
  44. {
  45. acceptEventArg = new SocketAsyncEventArgs();
  46. acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
  47. }
  48. else
  49. {
  50. // socket must be cleared since the context object is being reused
  51. acceptEventArg.AcceptSocket = null;
  52. }
  53. try
  54. {
  55. bool willRaiseEvent = _originalSocket.AcceptAsync(acceptEventArg);
  56. if (!willRaiseEvent)
  57. {
  58. ProcessAccept(acceptEventArg);
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. if (accepted != null)
  64. {
  65. try
  66. {
  67. #if NET46
  68. accepted.Close();
  69. #else
  70. accepted.Dispose();
  71. #endif
  72. }
  73. catch
  74. {
  75. }
  76. accepted = null;
  77. }
  78. }
  79. }
  80. // This method is the callback method associated with Socket.AcceptAsync
  81. // operations and is invoked when an accept operation is complete
  82. //
  83. void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
  84. {
  85. ProcessAccept(e);
  86. }
  87. private void ProcessAccept(SocketAsyncEventArgs e)
  88. {
  89. if (_isClosed())
  90. {
  91. return;
  92. }
  93. // http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.acceptasync%28v=vs.110%29.aspx
  94. // Under certain conditions ConnectionReset can occur
  95. // Need to attept to re-accept
  96. if (e.SocketError == SocketError.ConnectionReset)
  97. {
  98. _logger.Error("SocketError.ConnectionReset reported. Attempting to re-accept.");
  99. Socket dummy = null;
  100. StartAccept(e, ref dummy);
  101. return;
  102. }
  103. var acceptSocket = e.AcceptSocket;
  104. if (acceptSocket != null)
  105. {
  106. //ProcessAccept(acceptSocket);
  107. _onAccept(new NetSocket(acceptSocket, _logger));
  108. }
  109. // Accept the next connection request
  110. StartAccept(e, ref acceptSocket);
  111. }
  112. }
  113. }