Program.cs 5.3 KB

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