Ejemplo n.º 1
0
        private static async Task DispatchHttpRequestsAsync(HttpListener httpListener, int maxThreadCount = Int32.MaxValue)
        {
            // Create a request handler factory that uses basic authentication
            var requestHandlerFactory = new WebDavMailRu.CloudStore.Mailru.RequestHandlerFactory();

            // Create WebDAV dispatcher
            var homeFolder       = new MailruStore();
            var webDavDispatcher = new WebDavDispatcher(homeFolder, requestHandlerFactory);

            try
            {
                using (var sem = new SemaphoreSlim(maxThreadCount))
                {
                    var semclo = sem;
                    while (!CancelToken.IsCancellationRequested)
                    {
                        var httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false);

                        if (httpListenerContext == null)
                        {
                            break;
                        }

                        HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)httpListenerContext.User.Identity;
                        IHttpContext httpContext           = new HttpBasicContext(httpListenerContext, i => i.Name == identity.Name && i.Password == identity.Password);

                        await semclo.WaitAsync(CancelToken.Token);

                        var task = Task.Run(async() =>
                        {
                            try
                            {
                                await webDavDispatcher.DispatchRequestAsync(httpContext)
                                .ConfigureAwait(false);
                            }
                            finally
                            {
                                semclo.Release();
                            }
                        }, CancelToken.Token);
                    }
                }
            }
            catch (HttpListenerException) when(CancelToken.IsCancellationRequested)
            {
                Logger.Info("Server stopped");
            }
            catch (OperationCanceledException)
            {
                Logger.Info("Server stopped");
            }
            catch (Exception e)
            {
                Logger.Error("Global exception", e);
            }
        }
Ejemplo n.º 2
0
        private static async Task DispatchHttpRequestsAsync(HttpListener httpListener, CancellationToken cancellationToken, int maxThreadCount = Int32.MaxValue)
        {
            // Create a request handler factory that uses basic authentication
            var requestHandlerFactory = new WebDavMailRu.CloudStore.Mailru.RequestHandlerFactory();

            // Create WebDAV dispatcher
            var homeFolder       = new MailruStore();
            var webDavDispatcher = new WebDavDispatcher(homeFolder, requestHandlerFactory);

            try
            {
                using (var sem = new SemaphoreSlim(maxThreadCount))
                {
                    var semclo = sem;
                    while (!cancellationToken.IsCancellationRequested)
                    {
                        var httpListenerContext = await httpListener.GetContextAsync().ConfigureAwait(false);

                        if (httpListenerContext == null)
                        {
                            break;
                        }

                        //if (httpListenerContext.Request.IsAuthenticated)
                        //    httpContext = new HttpBasicContext(httpListenerContext, i => i.Name == webdavUsername && i.Password == webdavPassword);
                        //else httpContext = new HttpContext(httpListenerContext);

                        HttpListenerBasicIdentity identity = (HttpListenerBasicIdentity)httpListenerContext.User.Identity;
                        IHttpContext httpContext           = new HttpBasicContext(httpListenerContext, i => i.Name == identity.Name && i.Password == identity.Password);

                        await semclo.WaitAsync(cancellationToken);

                        // ReSharper disable once UnusedVariable
                        Task tsk = Task
                                   .Run(async() =>
                        {
                            try
                            {
                                await webDavDispatcher.DispatchRequestAsync(httpContext);
                            }
                            catch (Exception ex)
                            {
                                Logger.Error("Exception", ex);
                            }
                        }, cancellationToken)
                                   .ContinueWith(t => semclo.Release(), cancellationToken);
                    }
                }
            }
            catch (HttpListenerException excListener)
            {
                if (excListener.ErrorCode != ERROR_OPERATION_ABORTED)
                {
                    throw;
                }
            }
            catch (Exception e)
            {
                Logger.Error("Global exception", e);
            }
        }