SharpWebSocket.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Controller.Net;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using WebSocketState = MediaBrowser.Model.Net.WebSocketState;
  8. namespace Emby.Server.Implementations.HttpServer.SocketSharp
  9. {
  10. public class SharpWebSocket : IWebSocket
  11. {
  12. /// <summary>
  13. /// The logger
  14. /// </summary>
  15. private readonly ILogger _logger;
  16. public event EventHandler<EventArgs> Closed;
  17. /// <summary>
  18. /// Gets or sets the web socket.
  19. /// </summary>
  20. /// <value>The web socket.</value>
  21. private SocketHttpListener.WebSocket WebSocket { get; set; }
  22. private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  23. public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger)
  24. {
  25. if (socket == null)
  26. {
  27. throw new ArgumentNullException("socket");
  28. }
  29. if (logger == null)
  30. {
  31. throw new ArgumentNullException("logger");
  32. }
  33. _logger = logger;
  34. WebSocket = socket;
  35. socket.OnMessage += socket_OnMessage;
  36. socket.OnClose += socket_OnClose;
  37. socket.OnError += socket_OnError;
  38. WebSocket.ConnectAsServer();
  39. }
  40. void socket_OnError(object sender, SocketHttpListener.ErrorEventArgs e)
  41. {
  42. _logger.Error("Error in SharpWebSocket: {0}", e.Message ?? string.Empty);
  43. //EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger);
  44. }
  45. void socket_OnClose(object sender, SocketHttpListener.CloseEventArgs e)
  46. {
  47. EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger);
  48. }
  49. void socket_OnMessage(object sender, SocketHttpListener.MessageEventArgs e)
  50. {
  51. //if (!string.IsNullOrWhiteSpace(e.Data))
  52. //{
  53. // if (OnReceive != null)
  54. // {
  55. // OnReceive(e.Data);
  56. // }
  57. // return;
  58. //}
  59. if (OnReceiveBytes != null)
  60. {
  61. OnReceiveBytes(e.RawData);
  62. }
  63. }
  64. /// <summary>
  65. /// Gets or sets the state.
  66. /// </summary>
  67. /// <value>The state.</value>
  68. public WebSocketState State
  69. {
  70. get
  71. {
  72. WebSocketState commonState;
  73. if (!Enum.TryParse(WebSocket.ReadyState.ToString(), true, out commonState))
  74. {
  75. _logger.Warn("Unrecognized WebSocketState: {0}", WebSocket.ReadyState.ToString());
  76. }
  77. return commonState;
  78. }
  79. }
  80. /// <summary>
  81. /// Sends the async.
  82. /// </summary>
  83. /// <param name="bytes">The bytes.</param>
  84. /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
  85. /// <param name="cancellationToken">The cancellation token.</param>
  86. /// <returns>Task.</returns>
  87. public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
  88. {
  89. return WebSocket.SendAsync(bytes);
  90. }
  91. /// <summary>
  92. /// Sends the asynchronous.
  93. /// </summary>
  94. /// <param name="text">The text.</param>
  95. /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
  96. /// <param name="cancellationToken">The cancellation token.</param>
  97. /// <returns>Task.</returns>
  98. public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken)
  99. {
  100. return WebSocket.SendAsync(text);
  101. }
  102. /// <summary>
  103. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  104. /// </summary>
  105. public void Dispose()
  106. {
  107. Dispose(true);
  108. GC.SuppressFinalize(this);
  109. }
  110. /// <summary>
  111. /// Releases unmanaged and - optionally - managed resources.
  112. /// </summary>
  113. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  114. protected virtual void Dispose(bool dispose)
  115. {
  116. if (dispose)
  117. {
  118. WebSocket.OnMessage -= socket_OnMessage;
  119. WebSocket.OnClose -= socket_OnClose;
  120. WebSocket.OnError -= socket_OnError;
  121. _cancellationTokenSource.Cancel();
  122. WebSocket.Close();
  123. }
  124. }
  125. /// <summary>
  126. /// Gets or sets the receive action.
  127. /// </summary>
  128. /// <value>The receive action.</value>
  129. public Action<byte[]> OnReceiveBytes { get; set; }
  130. /// <summary>
  131. /// Gets or sets the on receive.
  132. /// </summary>
  133. /// <value>The on receive.</value>
  134. public Action<string> OnReceive { get; set; }
  135. }
  136. }