SocketAcceptor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. _logger = logger;
  16. _originalSocket = originalSocket;
  17. _isClosed = isClosed;
  18. _onAccept = onAccept;
  19. }
  20. public void StartAccept()
  21. {
  22. Socket dummy = null;
  23. StartAccept(null, ref dummy);
  24. }
  25. public void StartAccept(SocketAsyncEventArgs acceptEventArg, ref Socket accepted)
  26. {
  27. if (acceptEventArg == null)
  28. {
  29. acceptEventArg = new SocketAsyncEventArgs();
  30. acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
  31. }
  32. else
  33. {
  34. // socket must be cleared since the context object is being reused
  35. acceptEventArg.AcceptSocket = null;
  36. }
  37. try
  38. {
  39. bool willRaiseEvent = _originalSocket.AcceptAsync(acceptEventArg);
  40. if (!willRaiseEvent)
  41. {
  42. ProcessAccept(acceptEventArg);
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. if (accepted != null)
  48. {
  49. try
  50. {
  51. #if NET46
  52. accepted.Close();
  53. #else
  54. accepted.Dispose();
  55. #endif
  56. }
  57. catch
  58. {
  59. }
  60. accepted = null;
  61. }
  62. }
  63. }
  64. // This method is the callback method associated with Socket.AcceptAsync
  65. // operations and is invoked when an accept operation is complete
  66. //
  67. void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
  68. {
  69. ProcessAccept(e);
  70. }
  71. private void ProcessAccept(SocketAsyncEventArgs e)
  72. {
  73. if (_isClosed())
  74. {
  75. return;
  76. }
  77. // http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.acceptasync%28v=vs.110%29.aspx
  78. // Under certain conditions ConnectionReset can occur
  79. // Need to attept to re-accept
  80. if (e.SocketError == SocketError.ConnectionReset)
  81. {
  82. _logger.Error("SocketError.ConnectionReset reported. Attempting to re-accept.");
  83. Socket dummy = null;
  84. StartAccept(e, ref dummy);
  85. return;
  86. }
  87. var acceptSocket = e.AcceptSocket;
  88. if (acceptSocket != null)
  89. {
  90. //ProcessAccept(acceptSocket);
  91. _onAccept(new NetSocket(acceptSocket, _logger));
  92. }
  93. if (_originalSocket != null)
  94. {
  95. // Accept the next connection request
  96. StartAccept(e, ref acceptSocket);
  97. }
  98. }
  99. }
  100. }