2013-02-26 11:10:55 -05:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-03-25 23:01:47 -04:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-02-24 16:53:54 -05:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
using System;
|
2013-02-24 19:13:45 -05:00
|
|
|
|
using System.IO;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-03-07 00:34:00 -05:00
|
|
|
|
namespace MediaBrowser.Server.Implementations.ServerManager
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class WebSocketConnection
|
|
|
|
|
/// </summary>
|
2013-02-26 11:10:55 -05:00
|
|
|
|
public class WebSocketConnection : IWebSocketConnection
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IWebSocket _socket;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _remote end point
|
|
|
|
|
/// </summary>
|
2013-02-26 11:10:55 -05:00
|
|
|
|
public string RemoteEndPoint { get; private set; }
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _cancellation token source
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _send semaphore
|
|
|
|
|
/// </summary>
|
2013-05-10 08:18:07 -04:00
|
|
|
|
private readonly SemaphoreSlim _sendSemaphore = new SemaphoreSlim(1, 1);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The logger
|
|
|
|
|
/// </summary>
|
2013-02-21 16:06:23 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-02-24 16:53:54 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _json serializer
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2013-02-25 22:43:04 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the receive action.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The receive action.</value>
|
|
|
|
|
public Action<WebSocketMessageInfo> OnReceive { get; set; }
|
|
|
|
|
|
2013-05-25 20:53:51 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the last activity date.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The last activity date.</value>
|
|
|
|
|
public DateTime LastActivityDate { get; private set; }
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="WebSocketConnection" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="socket">The socket.</param>
|
|
|
|
|
/// <param name="remoteEndPoint">The remote end point.</param>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
/// <param name="jsonSerializer">The json serializer.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">socket</exception>
|
2013-02-25 22:43:04 -05:00
|
|
|
|
public WebSocketConnection(IWebSocket socket, string remoteEndPoint, IJsonSerializer jsonSerializer, ILogger logger)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
if (socket == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("socket");
|
|
|
|
|
}
|
2013-02-23 17:31:51 -05:00
|
|
|
|
if (string.IsNullOrEmpty(remoteEndPoint))
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("remoteEndPoint");
|
|
|
|
|
}
|
2013-02-24 16:53:54 -05:00
|
|
|
|
if (jsonSerializer == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("jsonSerializer");
|
|
|
|
|
}
|
2013-02-21 16:06:23 -05:00
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("logger");
|
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
2013-02-24 16:53:54 -05:00
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
_socket = socket;
|
2013-09-05 17:34:46 -04:00
|
|
|
|
_socket.OnReceiveBytes = OnReceiveInternal;
|
|
|
|
|
_socket.OnReceive = OnReceiveInternal;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
RemoteEndPoint = remoteEndPoint;
|
2013-02-21 16:06:23 -05:00
|
|
|
|
_logger = logger;
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [receive].
|
|
|
|
|
/// </summary>
|
2013-02-24 16:53:54 -05:00
|
|
|
|
/// <param name="bytes">The bytes.</param>
|
2013-02-25 22:43:04 -05:00
|
|
|
|
private void OnReceiveInternal(byte[] bytes)
|
2013-02-20 20:33:05 -05:00
|
|
|
|
{
|
2013-05-25 20:53:51 -04:00
|
|
|
|
LastActivityDate = DateTime.UtcNow;
|
|
|
|
|
|
2013-02-25 22:43:04 -05:00
|
|
|
|
if (OnReceive == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-02-20 20:33:05 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2013-02-24 16:53:54 -05:00
|
|
|
|
WebSocketMessageInfo info;
|
|
|
|
|
|
|
|
|
|
using (var memoryStream = new MemoryStream(bytes))
|
|
|
|
|
{
|
2013-05-10 08:18:07 -04:00
|
|
|
|
var stub = (WebSocketMessage<object>)_jsonSerializer.DeserializeFromStream(memoryStream, typeof(WebSocketMessage<object>));
|
|
|
|
|
|
|
|
|
|
info = new WebSocketMessageInfo
|
|
|
|
|
{
|
|
|
|
|
MessageType = stub.MessageType,
|
|
|
|
|
Data = stub.Data == null ? null : stub.Data.ToString()
|
|
|
|
|
};
|
2013-02-24 16:53:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
info.Connection = this;
|
|
|
|
|
|
2013-02-25 22:43:04 -05:00
|
|
|
|
OnReceive(info);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 16:06:23 -05:00
|
|
|
|
_logger.ErrorException("Error processing web socket message", ex);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-05 17:34:46 -04:00
|
|
|
|
private void OnReceiveInternal(string message)
|
|
|
|
|
{
|
|
|
|
|
LastActivityDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
if (OnReceive == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var stub = (WebSocketMessage<object>)_jsonSerializer.DeserializeFromString(message, typeof(WebSocketMessage<object>));
|
|
|
|
|
|
|
|
|
|
var info = new WebSocketMessageInfo
|
|
|
|
|
{
|
|
|
|
|
MessageType = stub.MessageType,
|
|
|
|
|
Data = stub.Data == null ? null : stub.Data.ToString()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info.Connection = this;
|
|
|
|
|
|
|
|
|
|
OnReceive(info);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error processing web socket message", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message asynchronously.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">message</exception>
|
|
|
|
|
public Task SendAsync<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (message == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("message");
|
|
|
|
|
}
|
2013-02-24 16:53:54 -05:00
|
|
|
|
|
|
|
|
|
var bytes = _jsonSerializer.SerializeToBytes(message);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
return SendAsync(bytes, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message asynchronously.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="buffer">The buffer.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendAsync(byte[] buffer, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return SendAsync(buffer, WebSocketMessageType.Text, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends a message asynchronously.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="buffer">The buffer.</param>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentNullException">buffer</exception>
|
|
|
|
|
public async Task SendAsync(byte[] buffer, WebSocketMessageType type, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (buffer == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("buffer");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cancellationToken == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("cancellationToken");
|
|
|
|
|
}
|
2013-05-10 08:18:07 -04:00
|
|
|
|
|
2013-02-20 20:33:05 -05:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
// Per msdn docs, attempting to send simultaneous messages will result in one failing.
|
|
|
|
|
// This should help us workaround that and ensure all messages get sent
|
|
|
|
|
await _sendSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _socket.SendAsync(buffer, type, true, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2013-02-21 16:06:23 -05:00
|
|
|
|
_logger.Info("WebSocket message to {0} was cancelled", RemoteEndPoint);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 16:06:23 -05:00
|
|
|
|
_logger.ErrorException("Error sending WebSocket message {0}", ex, RemoteEndPoint);
|
2013-02-20 20:33:05 -05:00
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_sendSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The state.</value>
|
|
|
|
|
public WebSocketState State
|
|
|
|
|
{
|
|
|
|
|
get { return _socket.State; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
_cancellationTokenSource.Dispose();
|
|
|
|
|
_socket.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|