Program.cs 4.7 KB

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