NativeWebSocket.cs 4.9 KB

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