From 2e4db7554041ecf481d3a38656fccc309e13eb5b Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Sat, 23 Feb 2013 17:31:51 -0500 Subject: extracted http server, web socket server and udp server dependancies --- .../WebSocket/AlchemyWebSocket.cs | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 MediaBrowser.Networking/WebSocket/AlchemyWebSocket.cs (limited to 'MediaBrowser.Networking/WebSocket/AlchemyWebSocket.cs') diff --git a/MediaBrowser.Networking/WebSocket/AlchemyWebSocket.cs b/MediaBrowser.Networking/WebSocket/AlchemyWebSocket.cs new file mode 100644 index 000000000..fbdb3186f --- /dev/null +++ b/MediaBrowser.Networking/WebSocket/AlchemyWebSocket.cs @@ -0,0 +1,133 @@ +using Alchemy.Classes; +using MediaBrowser.Common.Net; +using MediaBrowser.Common.Serialization; +using MediaBrowser.Model.Logging; +using System; +using System.Net.WebSockets; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Networking.WebSocket +{ + /// + /// Class AlchemyWebSocket + /// + public class AlchemyWebSocket : IWebSocket + { + /// + /// The logger + /// + private readonly ILogger _logger; + + /// + /// Gets or sets the web socket. + /// + /// The web socket. + private UserContext UserContext { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The context. + /// The logger. + /// context + public AlchemyWebSocket(UserContext context, ILogger logger) + { + if (context == null) + { + throw new ArgumentNullException("context"); + } + + _logger = logger; + UserContext = context; + + context.SetOnDisconnect(OnDisconnected); + context.SetOnReceive(OnReceive); + + _logger.Info("Client connected from {0}", context.ClientAddress); + } + + /// + /// The _disconnected + /// + private bool _disconnected = false; + /// + /// Gets or sets the state. + /// + /// The state. + public WebSocketState State + { + get { return _disconnected ? WebSocketState.Closed : WebSocketState.Open; } + } + + /// + /// Called when [disconnected]. + /// + /// The context. + private void OnDisconnected(UserContext context) + { + _disconnected = true; + } + + /// + /// Called when [receive]. + /// + /// The context. + private void OnReceive(UserContext context) + { + if (OnReceiveDelegate != null) + { + var json = context.DataFrame.ToString(); + + if (!string.IsNullOrWhiteSpace(json)) + { + try + { + var messageResult = JsonSerializer.DeserializeFromString(json); + + OnReceiveDelegate(messageResult); + } + catch (Exception ex) + { + _logger.ErrorException("Error processing web socket message", ex); + } + } + } + } + + /// + /// Sends the async. + /// + /// The bytes. + /// The type. + /// if set to true [end of message]. + /// The cancellation token. + /// Task. + public Task SendAsync(byte[] bytes, WebSocketMessageType type, bool endOfMessage, CancellationToken cancellationToken) + { + return Task.Run(() => UserContext.Send(bytes)); + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + } + + /// + /// Gets or sets the receive action. + /// + /// The receive action. + public Action OnReceiveDelegate { get; set; } + } +} -- cgit v1.2.3