2014-12-27 23:52:41 +01:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2014-12-27 19:06:32 +01:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-07-19 03:28:40 +02:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using WebSocketState = MediaBrowser.Model.Net.WebSocketState;
|
|
|
|
|
|
2016-11-08 19:44:23 +01:00
|
|
|
|
namespace Emby.Server.Implementations.HttpServer.SocketSharp
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
|
|
|
|
public class SharpWebSocket : IWebSocket
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
public event EventHandler<EventArgs> Closed;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the web socket.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The web socket.</value>
|
2015-01-03 20:38:22 +01:00
|
|
|
|
private SocketHttpListener.WebSocket WebSocket { get; set; }
|
2014-07-19 03:28:40 +02:00
|
|
|
|
|
|
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
2015-01-03 20:38:22 +01:00
|
|
|
|
public SharpWebSocket(SocketHttpListener.WebSocket socket, ILogger logger)
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
|
|
|
|
if (socket == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("socket");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("logger");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
WebSocket = socket;
|
|
|
|
|
|
|
|
|
|
socket.OnMessage += socket_OnMessage;
|
|
|
|
|
socket.OnClose += socket_OnClose;
|
|
|
|
|
socket.OnError += socket_OnError;
|
|
|
|
|
|
|
|
|
|
WebSocket.ConnectAsServer();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 20:38:22 +01:00
|
|
|
|
void socket_OnError(object sender, SocketHttpListener.ErrorEventArgs e)
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
2015-03-25 02:34:34 +01:00
|
|
|
|
_logger.Error("Error in SharpWebSocket: {0}", e.Message ?? string.Empty);
|
|
|
|
|
//EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger);
|
2014-07-19 03:28:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 20:38:22 +01:00
|
|
|
|
void socket_OnClose(object sender, SocketHttpListener.CloseEventArgs e)
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
|
|
|
|
EventHelper.FireEventIfNotNull(Closed, this, EventArgs.Empty, _logger);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-03 20:38:22 +01:00
|
|
|
|
void socket_OnMessage(object sender, SocketHttpListener.MessageEventArgs e)
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
2015-03-08 20:06:15 +01:00
|
|
|
|
//if (!string.IsNullOrWhiteSpace(e.Data))
|
|
|
|
|
//{
|
|
|
|
|
// if (OnReceive != null)
|
|
|
|
|
// {
|
|
|
|
|
// OnReceive(e.Data);
|
|
|
|
|
// }
|
|
|
|
|
// return;
|
|
|
|
|
//}
|
2015-03-08 19:49:51 +01:00
|
|
|
|
if (OnReceiveBytes != null)
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
|
|
|
|
OnReceiveBytes(e.RawData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The state.</value>
|
|
|
|
|
public WebSocketState State
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
WebSocketState commonState;
|
|
|
|
|
|
|
|
|
|
if (!Enum.TryParse(WebSocket.ReadyState.ToString(), true, out commonState))
|
|
|
|
|
{
|
|
|
|
|
_logger.Warn("Unrecognized WebSocketState: {0}", WebSocket.ReadyState.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return commonState;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the async.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bytes">The bytes.</param>
|
|
|
|
|
/// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2014-07-20 06:46:29 +02:00
|
|
|
|
public Task SendAsync(byte[] bytes, bool endOfMessage, CancellationToken cancellationToken)
|
2014-07-19 03:28:40 +02:00
|
|
|
|
{
|
2014-07-20 06:46:29 +02:00
|
|
|
|
var completionSource = new TaskCompletionSource<bool>();
|
2014-07-19 03:28:40 +02:00
|
|
|
|
|
2014-07-20 06:46:29 +02:00
|
|
|
|
WebSocket.SendAsync(bytes, res => completionSource.TrySetResult(true));
|
|
|
|
|
|
|
|
|
|
return completionSource.Task;
|
|
|
|
|
}
|
2014-07-19 03:28:40 +02:00
|
|
|
|
|
2014-07-20 06:46:29 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the asynchronous.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
|
/// <param name="endOfMessage">if set to <c>true</c> [end of message].</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendAsync(string text, bool endOfMessage, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-07-19 03:28:40 +02:00
|
|
|
|
var completionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
2014-07-20 06:46:29 +02:00
|
|
|
|
WebSocket.SendAsync(text, res => completionSource.TrySetResult(true));
|
2014-07-19 03:28:40 +02:00
|
|
|
|
|
|
|
|
|
return completionSource.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
WebSocket.OnMessage -= socket_OnMessage;
|
|
|
|
|
WebSocket.OnClose -= socket_OnClose;
|
|
|
|
|
WebSocket.OnError -= socket_OnError;
|
2015-03-09 01:36:02 +01:00
|
|
|
|
|
2014-07-19 03:28:40 +02:00
|
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
|
|
|
|
|
WebSocket.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the receive action.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The receive action.</value>
|
|
|
|
|
public Action<byte[]> OnReceiveBytes { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the on receive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The on receive.</value>
|
|
|
|
|
public Action<string> OnReceive { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|