private async Task ProcessRequestAsync(HttpListenerContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            var localPath = request.Url.LocalPath.Trim('/');

            _logger.Info("Http {0} {1}", request.HttpMethod, localPath);

            try
            {
                if (string.Equals(localPath, "windowstate-maximized", StringComparison.OrdinalIgnoreCase))
                {
                    _windowSync.OnElectronWindowStateChanged("maximized");
                }
                else if (string.Equals(localPath, "windowstate-normal", StringComparison.OrdinalIgnoreCase))
                {
                    _windowSync.OnElectronWindowStateChanged("normal");
                }
                else if (string.Equals(localPath, "windowstate-minimized", StringComparison.OrdinalIgnoreCase))
                {
                    _windowSync.OnElectronWindowStateChanged("minimized");
                }
                else if (string.Equals(localPath, "windowstate-fullscreen", StringComparison.OrdinalIgnoreCase))
                {
                    _windowSync.OnElectronWindowStateChanged("fullscreen");
                }
                else if (string.Equals(localPath, "windowsize", StringComparison.OrdinalIgnoreCase))
                {
                    _windowSync.OnElectronWindowSizeChanged();
                }
                else if (localPath.StartsWith("directshowplayer", StringComparison.OrdinalIgnoreCase))
                {
                    await _dsPlayerBridge.ProcessRequest(context, localPath).ConfigureAwait(false);
                }
                else if (localPath.StartsWith("fileexists", StringComparison.OrdinalIgnoreCase))
                {
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        var path   = reader.ReadToEnd();
                        var exists = File.Exists(path);
                        exists = false;
                        var bytes = Encoding.UTF8.GetBytes(exists.ToString().ToLower());
                        context.Response.ContentLength64 = bytes.Length;
                        context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    }
                }
                else if (localPath.StartsWith("directoryexists", StringComparison.OrdinalIgnoreCase))
                {
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        var path   = reader.ReadToEnd();
                        var exists = Directory.Exists(path);
                        var bytes  = Encoding.UTF8.GetBytes(exists.ToString().ToLower());
                        context.Response.ContentLength64 = bytes.Length;
                        context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    }
                }
            }
            finally
            {
                response.Close();
            }
        }
Exemple #2
0
        private void ProcessRequest(HttpListenerContext context)
        {
            var request  = context.Request;
            var response = context.Response;

            var localPath = request.Url.LocalPath.Trim('/');

            var logRequest = !string.Equals(localPath, "directshowplayer/refresh", StringComparison.OrdinalIgnoreCase);

            if (logRequest)
            {
                _logger.Info("Http {0} {1}", request.HttpMethod, localPath);
            }

            try
            {
                if (string.Equals(localPath, "windowstate-maximized", StringComparison.OrdinalIgnoreCase))
                {
                    _dsPlayerBridge.HandleWindowSizeChanged();
                }
                else if (string.Equals(localPath, "windowstate-normal", StringComparison.OrdinalIgnoreCase))
                {
                    _dsPlayerBridge.HandleWindowSizeChanged();
                }
                else if (string.Equals(localPath, "windowstate-minimized", StringComparison.OrdinalIgnoreCase))
                {
                }
                else if (string.Equals(localPath, "windowstate-fullscreen", StringComparison.OrdinalIgnoreCase))
                {
                    _dsPlayerBridge.HandleWindowSizeChanged();
                }
                else if (string.Equals(localPath, "windowsize", StringComparison.OrdinalIgnoreCase))
                {
                    _dsPlayerBridge.HandleWindowSizeChanged();
                }
                else if (string.Equals(localPath, "runatstartup-true", StringComparison.OrdinalIgnoreCase))
                {
                    if (!_config.Configuration.RunAtStartup)
                    {
                        _config.Configuration.RunAtStartup = true;
                        _config.SaveConfiguration();
                    }
                }
                else if (string.Equals(localPath, "runatstartup-false", StringComparison.OrdinalIgnoreCase))
                {
                    if (_config.Configuration.RunAtStartup)
                    {
                        _config.Configuration.RunAtStartup = false;
                        _config.SaveConfiguration();
                    }
                }
                else if (localPath.StartsWith("directshowplayer", StringComparison.OrdinalIgnoreCase))
                {
                    _dsPlayerBridge.ProcessRequest(context, localPath);
                }
                else if (localPath.StartsWith("fileexists", StringComparison.OrdinalIgnoreCase))
                {
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        var path   = reader.ReadToEnd();
                        var exists = File.Exists(path);
                        var bytes  = Encoding.UTF8.GetBytes(exists.ToString().ToLower());
                        context.Response.ContentLength64 = bytes.Length;
                        context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    }
                }
                else if (localPath.StartsWith("directoryexists", StringComparison.OrdinalIgnoreCase))
                {
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        var path   = reader.ReadToEnd();
                        var exists = Directory.Exists(path);
                        var bytes  = Encoding.UTF8.GetBytes(exists.ToString().ToLower());
                        context.Response.ContentLength64 = bytes.Length;
                        context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    }
                }
                else if (localPath.StartsWith("mediafilterinfo", StringComparison.OrdinalIgnoreCase))
                {
                    using (var reader = new StreamReader(context.Request.InputStream))
                    {
                        var bytes = Encoding.UTF8.GetBytes(_dsPlayerBridge.MediaFilterPath);
                        context.Response.ContentLength64 = bytes.Length;
                        context.Response.OutputStream.Write(bytes, 0, bytes.Length);
                    }
                }
                else if (localPath.StartsWith("openmadvr", StringComparison.OrdinalIgnoreCase))
                {
                    System.Diagnostics.Process.Start(Path.Combine(_dsPlayerBridge.MediaFilterPath, "madVR"));
                }
            }
            finally
            {
                if (logRequest)
                {
                    _logger.Info("Http Completed {0} {1}", request.HttpMethod, localPath);
                }

                response.Close();
            }
        }