Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using MediaBrowser.Common.Implementations.IO;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Implementations;
  5. using MediaBrowser.Server.Mono.Native;
  6. using MediaBrowser.Server.Startup.Common;
  7. using Microsoft.Win32;
  8. using System;
  9. using System.Diagnostics;
  10. using System.Net;
  11. using System.Net.Security;
  12. using System.Reflection;
  13. using System.Security.Cryptography.X509Certificates;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Server.Mono
  16. {
  17. public class MainClass
  18. {
  19. private static ApplicationHost _appHost;
  20. private static ILogger _logger;
  21. public static void Main (string[] args)
  22. {
  23. var applicationPath = Assembly.GetEntryAssembly().Location;
  24. var options = new StartupOptions();
  25. // Allow this to be specified on the command line.
  26. var customProgramDataPath = options.GetOption("-programdata");
  27. var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
  28. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  29. logManager.ReloadLogger(LogSeverity.Info);
  30. logManager.AddConsoleOutput();
  31. var logger = _logger = logManager.GetLogger("Main");
  32. ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
  33. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  34. try
  35. {
  36. RunApplication(appPaths, logManager, options);
  37. }
  38. finally
  39. {
  40. logger.Info("Shutting down");
  41. _appHost.Dispose();
  42. }
  43. }
  44. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
  45. {
  46. if (string.IsNullOrEmpty(programDataPath))
  47. {
  48. return new ServerApplicationPaths(ApplicationPathHelper.GetProgramDataPath(applicationPath), applicationPath);
  49. }
  50. return new ServerApplicationPaths(programDataPath, applicationPath);
  51. }
  52. private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
  53. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
  54. {
  55. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  56. // Allow all https requests
  57. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
  58. var fileSystem = new CommonFileSystem(logManager.GetLogger("FileSystem"), false, true);
  59. var nativeApp = new NativeApp();
  60. _appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "MBServer.Mono", false, nativeApp);
  61. if (options.ContainsOption("-v")) {
  62. Console.WriteLine (_appHost.ApplicationVersion.ToString());
  63. return;
  64. }
  65. Console.WriteLine ("appHost.Init");
  66. var initProgress = new Progress<double>();
  67. var task = _appHost.Init(initProgress);
  68. Task.WaitAll (task);
  69. Console.WriteLine ("Running startup tasks");
  70. task = _appHost.RunStartupTasks();
  71. Task.WaitAll (task);
  72. task = ApplicationTaskCompletionSource.Task;
  73. Task.WaitAll (task);
  74. }
  75. /// <summary>
  76. /// Handles the SessionEnding event of the SystemEvents control.
  77. /// </summary>
  78. /// <param name="sender">The source of the event.</param>
  79. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  80. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  81. {
  82. if (e.Reason == SessionEndReasons.SystemShutdown)
  83. {
  84. Shutdown();
  85. }
  86. }
  87. /// <summary>
  88. /// Handles the UnhandledException event of the CurrentDomain control.
  89. /// </summary>
  90. /// <param name="sender">The source of the event.</param>
  91. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  92. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  93. {
  94. var exception = (Exception)e.ExceptionObject;
  95. new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
  96. if (!Debugger.IsAttached)
  97. {
  98. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  99. }
  100. }
  101. public static void Shutdown()
  102. {
  103. ApplicationTaskCompletionSource.SetResult (true);
  104. }
  105. }
  106. class NoCheckCertificatePolicy : ICertificatePolicy
  107. {
  108. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  109. {
  110. return true;
  111. }
  112. }
  113. }