SharpWebSocket.cs 3.5 KB

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