NativeWebSocket.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Common.Serialization;
  2. using MediaBrowser.Model.Logging;
  3. using System;
  4. using System.IO;
  5. using System.Net.WebSockets;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Common.Net
  9. {
  10. /// <summary>
  11. /// Class NativeWebSocket
  12. /// </summary>
  13. public class NativeWebSocket : IWebSocket
  14. {
  15. /// <summary>
  16. /// The logger
  17. /// </summary>
  18. private readonly ILogger _logger;
  19. /// <summary>
  20. /// Gets or sets the web socket.
  21. /// </summary>
  22. /// <value>The web socket.</value>
  23. private WebSocket WebSocket { get; set; }
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="NativeWebSocket" /> class.
  26. /// </summary>
  27. /// <param name="socket">The socket.</param>
  28. /// <param name="logger">The logger.</param>
  29. /// <exception cref="System.ArgumentNullException">socket</exception>
  30. public NativeWebSocket(WebSocket socket, ILogger logger)
  31. {
  32. if (socket == null)
  33. {
  34. throw new ArgumentNullException("socket");
  35. }
  36. if (logger == null)
  37. {
  38. throw new ArgumentNullException("logger");
  39. }
  40. _logger = logger;
  41. WebSocket = socket;
  42. Receive();
  43. }
  44. /// <summary>
  45. /// Gets or sets the state.
  46. /// </summary>
  47. /// <value>The state.</value>
  48. public WebSocketState State
  49. {
  50. get { return WebSocket.State; }
  51. }
  52. /// <summary>
  53. /// Receives this instance.
  54. /// </summary>
  55. private async void Receive()
  56. {
  57. while (true)
  58. {
  59. byte[] bytes;
  60. try
  61. {
  62. bytes = await ReceiveBytesAsync(CancellationToken.None).ConfigureAwait(false);
  63. }
  64. catch (WebSocketException ex)
  65. {
  66. _logger.ErrorException("Error reveiving web socket message", ex);
  67. break;
  68. }
  69. if (OnReceiveDelegate != null)
  70. {
  71. using (var memoryStream = new MemoryStream(bytes))
  72. {
  73. try
  74. {
  75. var messageResult = JsonSerializer.DeserializeFromStream<WebSocketMessageInfo>(memoryStream);
  76. OnReceiveDelegate(messageResult);
  77. }
  78. catch (Exception ex)
  79. {
  80. _logger.ErrorException("Error processing web socket message", ex);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /// <summary>
  87. /// Receives the async.
  88. /// </summary>
  89. /// <param name="cancellationToken">The cancellation token.</param>
  90. /// <returns>Task{WebSocketMessageInfo}.</returns>
  91. /// <exception cref="System.Net.WebSockets.WebSocketException">Connection closed</exception>
  92. private async Task<byte[]> ReceiveBytesAsync(CancellationToken cancellationToken)
  93. {
  94. var bytes = new byte[4096];
  95. var buffer = new ArraySegment<byte>(bytes);
  96. var result = await WebSocket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);
  97. if (result.CloseStatus.HasValue)
  98. {
  99. throw new WebSocketException("Connection closed");
  100. }
  101. return buffer.Array;
  102. }
  103. /// <summary>
  104. /// Sends the async.
  105. /// </summary>
  106. /// <param name="bytes">The bytes.</param>
  107. /// <param name="type">The type.</param>
  108. /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
  109. /// <param name="cancellationToken">The cancellation token.</param>
  110. /// <returns>Task.</returns>
  111. public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken)
  112. {
  113. return WebSocket.SendAsync(new ArraySegment<byte>(bytes), type, true, cancellationToken);
  114. }
  115. /// <summary>
  116. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  117. /// </summary>
  118. public void Dispose()
  119. {
  120. Dispose(true);
  121. }
  122. /// <summary>
  123. /// Releases unmanaged and - optionally - managed resources.
  124. /// </summary>
  125. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  126. protected virtual void Dispose(bool dispose)
  127. {
  128. if (dispose)
  129. {
  130. WebSocket.Dispose();
  131. }
  132. }
  133. /// <summary>
  134. /// Gets or sets the receive action.
  135. /// </summary>
  136. /// <value>The receive action.</value>
  137. public Action<WebSocketMessageInfo> OnReceiveDelegate { get; set; }
  138. }
  139. }