This commit is contained in:
LukePulverenti
2013-02-24 16:53:54 -05:00
parent 6c86721f6d
commit 8ce3e74e81
93 changed files with 2458 additions and 1584 deletions

View File

@@ -1,6 +1,5 @@
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Serialization;
using System;
using System.IO;
using System.Threading.Tasks;
@@ -104,15 +103,15 @@ namespace MediaBrowser.Common.Net.Handlers
{
return Task.Run(() =>
{
switch (SerializationFormat)
{
case SerializationFormat.Protobuf:
Kernel.ProtobufSerializer.SerializeToStream(_objectToSerialize, stream);
break;
default:
JsonSerializer.SerializeToStream(_objectToSerialize, stream);
break;
}
//switch (SerializationFormat)
//{
// case SerializationFormat.Protobuf:
// Kernel.ProtobufSerializer.SerializeToStream(_objectToSerialize, stream);
// break;
// default:
// JsonSerializer.SerializeToStream(_objectToSerialize, stream);
// break;
//}
});
}
}

View File

@@ -20,7 +20,7 @@ namespace MediaBrowser.Common.Net
/// Gets or sets the receive action.
/// </summary>
/// <value>The receive action.</value>
Action<WebSocketMessageInfo> OnReceiveDelegate { get; set; }
Action<byte[]> OnReceiveDelegate { get; set; }
/// <summary>
/// Sends the async.

View File

@@ -1,159 +0,0 @@
using MediaBrowser.Common.Serialization;
using MediaBrowser.Model.Logging;
using System;
using System.IO;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Class NativeWebSocket
/// </summary>
public class NativeWebSocket : IWebSocket
{
/// <summary>
/// The logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Gets or sets the web socket.
/// </summary>
/// <value>The web socket.</value>
private WebSocket WebSocket { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="NativeWebSocket" /> class.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="logger">The logger.</param>
/// <exception cref="System.ArgumentNullException">socket</exception>
public NativeWebSocket(WebSocket socket, ILogger logger)
{
if (socket == null)
{
throw new ArgumentNullException("socket");
}
if (logger == null)
{
throw new ArgumentNullException("logger");
}
_logger = logger;
WebSocket = socket;
Receive();
}
/// <summary>
/// Gets or sets the state.
/// </summary>
/// <value>The state.</value>
public WebSocketState State
{
get { return WebSocket.State; }
}
/// <summary>
/// Receives this instance.
/// </summary>
private async void Receive()
{
while (true)
{
byte[] bytes;
try
{
bytes = await ReceiveBytesAsync(CancellationToken.None).ConfigureAwait(false);
}
catch (WebSocketException ex)
{
_logger.ErrorException("Error reveiving web socket message", ex);
break;
}
if (OnReceiveDelegate != null)
{
using (var memoryStream = new MemoryStream(bytes))
{
try
{
var messageResult = JsonSerializer.DeserializeFromStream<WebSocketMessageInfo>(memoryStream);
OnReceiveDelegate(messageResult);
}
catch (Exception ex)
{
_logger.ErrorException("Error processing web socket message", ex);
}
}
}
}
}
/// <summary>
/// Receives the async.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{WebSocketMessageInfo}.</returns>
/// <exception cref="System.Net.WebSockets.WebSocketException">Connection closed</exception>
private async Task<byte[]> ReceiveBytesAsync(CancellationToken cancellationToken)
{
var bytes = new byte[4096];
var buffer = new ArraySegment<byte>(bytes);
var result = await WebSocket.ReceiveAsync(buffer, cancellationToken).ConfigureAwait(false);
if (result.CloseStatus.HasValue)
{
throw new WebSocketException("Connection closed");
}
return buffer.Array;
}
/// <summary>
/// Sends the async.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <param name="type">The type.</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(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken)
{
return WebSocket.SendAsync(new ArraySegment<byte>(bytes), type, true, cancellationToken);
}
/// <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.Dispose();
}
}
/// <summary>
/// Gets or sets the receive action.
/// </summary>
/// <value>The receive action.</value>
public Action<WebSocketMessageInfo> OnReceiveDelegate { get; set; }
}
}

View File

@@ -1,7 +1,7 @@
using MediaBrowser.Common.Serialization;
using System.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Net;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
@@ -38,14 +38,21 @@ namespace MediaBrowser.Common.Net
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The _json serializer
/// </summary>
private readonly IJsonSerializer _jsonSerializer;
/// <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>
/// <param name="receiveAction">The receive action.</param>
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logger">The logger.</param>
/// <exception cref="System.ArgumentNullException">socket</exception>
public WebSocketConnection(IWebSocket socket, string remoteEndPoint, Action<WebSocketMessageInfo> receiveAction, ILogger logger)
public WebSocketConnection(IWebSocket socket, string remoteEndPoint, Action<WebSocketMessageInfo> receiveAction, IJsonSerializer jsonSerializer, ILogger logger)
{
if (socket == null)
{
@@ -59,11 +66,16 @@ namespace MediaBrowser.Common.Net
{
throw new ArgumentNullException("receiveAction");
}
if (jsonSerializer == null)
{
throw new ArgumentNullException("jsonSerializer");
}
if (logger == null)
{
throw new ArgumentNullException("logger");
}
_jsonSerializer = jsonSerializer;
_socket = socket;
_socket.OnReceiveDelegate = info => OnReceive(info, receiveAction);
RemoteEndPoint = remoteEndPoint;
@@ -73,12 +85,19 @@ namespace MediaBrowser.Common.Net
/// <summary>
/// Called when [receive].
/// </summary>
/// <param name="info">The info.</param>
/// <param name="bytes">The bytes.</param>
/// <param name="callback">The callback.</param>
private void OnReceive(WebSocketMessageInfo info, Action<WebSocketMessageInfo> callback)
private void OnReceive(byte[] bytes, Action<WebSocketMessageInfo> callback)
{
try
{
WebSocketMessageInfo info;
using (var memoryStream = new MemoryStream(bytes))
{
info = _jsonSerializer.DeserializeFromStream<WebSocketMessageInfo>(memoryStream);
}
info.Connection = this;
callback(info);
@@ -103,8 +122,8 @@ namespace MediaBrowser.Common.Net
{
throw new ArgumentNullException("message");
}
var bytes = JsonSerializer.SerializeToBytes(message);
var bytes = _jsonSerializer.SerializeToBytes(message);
return SendAsync(bytes, cancellationToken);
}