public void Start(int port, [CanBeNull] string filename) { #if DEBUG var log = this; #else var log = new NullLog(); #endif _server = new WebServer($"http://+:{port}/", log, RoutingStrategy.Wildcard); // HTTP part: index webpage, static files nearby (if any) var pages = new PagesProvider(filename); _server.RegisterModule(new WebApiModule()); _server.Module <WebApiModule>().RegisterController(() => new IndexPageController(pages)); if (Directory.Exists(pages.StaticDirectory)) { _server.RegisterModule(new StaticFilesModule(new Dictionary <string, string> { [@"/"] = pages.StaticDirectory }) { UseRamCache = false, UseGzip = false }); } // Websockets part var module = new WebSocketsModule(); _server.RegisterModule(module); _sharedServer = new PublishDataSocketsServer(@"Current Race Shared Memory Server"); module.RegisterWebSocketsServer(@"/api/ws/shared", _sharedServer); _statsServer = new PublishDataSocketsServer(@"Current Race Stats Server"); module.RegisterWebSocketsServer(@"/api/ws/stats", _statsServer); // Starting try { _server.RunAsync(); } catch (HttpListenerException e) { NonfatalError.NotifyBackground("Can’t start web server", $"Don’t forget to allow port’s usage with something like “netsh http add urlacl url=\"http://+:{port}/\" user=everyone”.", e, new[] { new NonfatalErrorSolution($"Use “netsh” to allow usage of port {port}", null, token => { Process.Start(new ProcessStartInfo { FileName = "cmd", Arguments = $"/C netsh http add urlacl url=\"http://+:{port}/\" user=everyone & pause", Verb = "runas" }); return(Task.Delay(1000, token)); }) }); } catch (Exception e) { Logging.Error(e); } }
protected override void OnPageChanged(int page, int previousPage) { if (PagesProvider is null) { return; } if (page is < 0 || page >= PagesProvider.Count) { SetCurrentValue(PageProperty, previousPage); return; } MemoryStream?memoryStream = PagesProvider[page]; if (memoryStream is null) { PagesProvider.LoadPageAsync(page).GetAwaiter().GetResult(); memoryStream = PagesProvider[page]; } Img.SetCurrentValue(Image.SourceProperty, BitmapFrame.Create(memoryStream)); }
public IndexPageController(PagesProvider pages) { _pages = pages; }
public void Start(int port, [CanBeNull] string filename) { #if DEBUG var log = this; #else var log = new NullLog(); #endif _server = new WebServer($"http://+:{port}/", log, RoutingStrategy.Wildcard); // HTTP part: index webpage, static files nearby (if any) var pages = new PagesProvider(filename); _server.RegisterModule(new WebApiModule()); _server.Module <WebApiModule>().RegisterController(() => new IndexPageController(pages)); if (Directory.Exists(pages.StaticDirectory)) { _server.RegisterModule(new StaticFilesModule(new Dictionary <string, string> { [@"/"] = pages.StaticDirectory }) { UseRamCache = false, UseGzip = false }); } // Websockets part var module = new WebSocketsModule(); _server.RegisterModule(module); _sharedServer = new PublishDataSocketsServer(@"Current Race Shared Memory Server"); module.RegisterWebSocketsServer(@"/api/ws/shared", _sharedServer); _statsServer = new PublishDataSocketsServer(@"Current Race Stats Server"); module.RegisterWebSocketsServer(@"/api/ws/stats", _statsServer); // Starting try { _server.RunAsync(); } catch (HttpListenerException e) when(e.ToString().Contains("0x80004005")) { NonfatalError.NotifyBackground("Can’t start web server", $"Don’t forget to allow port’s usage with something like “netsh http add urlacl url=\"http://+:{port}/\" user=everyone”.", e, new[] { new NonfatalErrorSolution($"Use “netsh” to allow usage of port {port}", null, async token => { try { var command = $"netsh http add urlacl url=\"http://+:{port}/\" user=everyone"; var proc = ProcessExtension.Start("cmd", new[] { "/C", $"echo {command} & {command} & @pause" }, new ProcessStartInfo { Verb = "runas" }); await proc.WaitForExitAsync(token).ConfigureAwait(false); Logging.Debug("Done: " + proc.ExitCode); } catch (Win32Exception ex) when(ex.ErrorCode != -1) { Logging.Debug(ex.ErrorCode); throw new InformativeException("Access denied", "Unfortunately, HTTP.sys driver doesn’t allow to assign ports without administrator privileges."); } }) }); } catch (Exception e) { Logging.Error(e); } }