From bc657237aa4c541fe0079fcbb7616dbe87bbf0a7 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 18 Jul 2014 21:28:40 -0400 Subject: consolidate web socket onto one port --- .../HttpServer/HttpListenerHost.cs | 44 ++++++---------------- 1 file changed, 11 insertions(+), 33 deletions(-) (limited to 'MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs') diff --git a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs index 68b69cdf0..62a43751d 100644 --- a/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/MediaBrowser.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -6,6 +6,7 @@ using MediaBrowser.Common.Net; using MediaBrowser.Controller.Net; using MediaBrowser.Model.Logging; using MediaBrowser.Server.Implementations.HttpServer.NetListener; +using MediaBrowser.Server.Implementations.HttpServer.SocketSharp; using ServiceStack; using ServiceStack.Api.Swagger; using ServiceStack.Host; @@ -18,7 +19,6 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -42,7 +42,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer private readonly ContainerAdapter _containerAdapter; - private readonly ConcurrentDictionary _localEndPoints = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); public event EventHandler WebSocketConnected; /// @@ -157,12 +156,13 @@ namespace MediaBrowser.Server.Implementations.HttpServer { HostContext.Config.HandlerFactoryPath = ListenerRequest.GetHandlerPathIfAny(UrlPrefixes.First()); - _listener = new HttpListenerServer(_logger, _threadPoolManager) - { - WebSocketHandler = WebSocketHandler, - ErrorHandler = ErrorHandler, - RequestHandler = RequestHandler - }; + _listener = NativeWebSocket.IsSupported + ? _listener = new HttpListenerServer(_logger, _threadPoolManager) + : _listener = new WebSocketSharpListener(_logger, _threadPoolManager); + + _listener.WebSocketHandler = WebSocketHandler; + _listener.ErrorHandler = ErrorHandler; + _listener.RequestHandler = RequestHandler; _listener.Start(UrlPrefixes); } @@ -175,24 +175,6 @@ namespace MediaBrowser.Server.Implementations.HttpServer } } - /// - /// Logs the HTTP request. - /// - /// The request. - private void LogHttpRequest(HttpListenerRequest request) - { - var endpoint = request.LocalEndPoint; - - if (endpoint != null) - { - var address = endpoint.ToString(); - - _localEndPoints.GetOrAdd(address, address); - } - - LoggerUtils.LogRequest(_logger, request); - } - private void ErrorHandler(Exception ex, IRequest httpReq) { try @@ -261,6 +243,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer /// Overridable method that can be used to implement a custom hnandler /// /// The HTTP req. + /// The URL. /// Task. protected Task RequestHandler(IHttpRequest httpReq, Uri url) { @@ -310,17 +293,17 @@ namespace MediaBrowser.Server.Implementations.HttpServer task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent); //Matches Exceptions handled in HttpListenerBase.InitTask() - var statusCode = httpRes.StatusCode; var urlString = url.ToString(); task.ContinueWith(x => { + var statusCode = httpRes.StatusCode; + var duration = DateTime.Now - date; LoggerUtils.LogResponse(_logger, statusCode, urlString, remoteIp, duration); }, TaskContinuationOptions.None); - return task; } @@ -393,10 +376,5 @@ namespace MediaBrowser.Server.Implementations.HttpServer UrlPrefixes = urlPrefixes.ToList(); Start(UrlPrefixes.First()); } - - public bool SupportsWebSockets - { - get { return NativeWebSocket.IsSupported; } - } } } \ No newline at end of file -- cgit v1.2.3