SharpWebSocket.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Net.WebSockets;
  3. using System.Text;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Emby.Server.Implementations.Net;
  7. using Microsoft.Extensions.Logging;
  8. namespace Emby.Server.Implementations.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 readonly WebSocket _webSocket;
  22. private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
  23. private bool _disposed;
  24. public SharpWebSocket(WebSocket socket, ILogger logger)
  25. {
  26. _logger = logger ?? throw new ArgumentNullException(nameof(logger));
  27. _webSocket = socket ?? throw new ArgumentNullException(nameof(socket));
  28. }
  29. /// <summary>
  30. /// Gets or sets the state.
  31. /// </summary>
  32. /// <value>The state.</value>
  33. public WebSocketState State => _webSocket.State;
  34. /// <summary>
  35. /// Sends the async.
  36. /// </summary>
  37. /// <param name="bytes">The bytes.</param>
  38. /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
  39. /// <param name="cancellationToken">The cancellation token.</param>
  40. /// <returns>Task.</returns>
  41. public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
  42. {
  43. return _webSocket.SendAsync(new ArraySegment<byte>(bytes), WebSocketMessageType.Binary, endOfMessage, cancellationToken);
  44. }
  45. /// <summary>
  46. /// Sends the asynchronous.
  47. /// </summary>
  48. /// <param name="text">The text.</param>
  49. /// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
  50. /// <param name="cancellationToken">The cancellation token.</param>
  51. /// <returns>Task.</returns>
  52. public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken)
  53. {
  54. return _webSocket.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(text)), WebSocketMessageType.Text, endOfMessage, cancellationToken);
  55. }
  56. /// <summary>
  57. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  58. /// </summary>
  59. public void Dispose()
  60. {
  61. Dispose(true);
  62. GC.SuppressFinalize(this);
  63. }
  64. /// <summary>
  65. /// Releases unmanaged and - optionally - managed resources.
  66. /// </summary>
  67. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  68. protected virtual void Dispose(bool dispose)
  69. {
  70. if (_disposed)
  71. {
  72. return;
  73. }
  74. if (dispose)
  75. {
  76. _cancellationTokenSource.Cancel();
  77. if (_webSocket.State == WebSocketState.Open)
  78. {
  79. _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closed by client",
  80. CancellationToken.None);
  81. }
  82. Closed?.Invoke(this, EventArgs.Empty);
  83. }
  84. _disposed = true;
  85. }
  86. /// <summary>
  87. /// Gets or sets the receive action.
  88. /// </summary>
  89. /// <value>The receive action.</value>
  90. public Action<byte[]> OnReceiveBytes { get; set; }
  91. }
  92. }