Program.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using MediaBrowser.Common.Constants;
  2. using MediaBrowser.Common.Implementations.Logging;
  3. using MediaBrowser.Common.Implementations.Updates;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Server.Implementations;
  6. using MediaBrowser.ServerApplication;
  7. using MediaBrowser.ServerApplication.Native;
  8. using Microsoft.Win32;
  9. using System;
  10. using System.Diagnostics;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Windows;
  14. using System.Net;
  15. using System.Net.Security;
  16. using System.Security.Cryptography.X509Certificates;
  17. using System.Threading.Tasks;
  18. using System.Reflection;
  19. namespace MediaBrowser.Server.Mono
  20. {
  21. public class MainClass
  22. {
  23. private static ApplicationHost _appHost;
  24. private static ILogger _logger;
  25. public static void Main (string[] args)
  26. {
  27. var applicationPath = Assembly.GetEntryAssembly ().Location;
  28. var appPaths = CreateApplicationPaths(applicationPath);
  29. var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
  30. logManager.ReloadLogger(LogSeverity.Info);
  31. var logger = _logger = logManager.GetLogger("Main");
  32. BeginLog(logger);
  33. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  34. if (PerformUpdateIfNeeded(appPaths, logger))
  35. {
  36. logger.Info("Exiting to perform application update.");
  37. return;
  38. }
  39. try
  40. {
  41. RunApplication(appPaths, logManager);
  42. }
  43. finally
  44. {
  45. logger.Info("Shutting down");
  46. _appHost.Dispose();
  47. }
  48. }
  49. private static ServerApplicationPaths CreateApplicationPaths(string applicationPath)
  50. {
  51. return new ServerApplicationPaths(applicationPath);
  52. }
  53. /// <summary>
  54. /// Determines whether this instance [can self restart].
  55. /// </summary>
  56. /// <returns><c>true</c> if this instance [can self restart]; otherwise, <c>false</c>.</returns>
  57. public static bool CanSelfRestart
  58. {
  59. get
  60. {
  61. return false;
  62. }
  63. }
  64. /// <summary>
  65. /// Gets a value indicating whether this instance can self update.
  66. /// </summary>
  67. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  68. public static bool CanSelfUpdate
  69. {
  70. get
  71. {
  72. return false;
  73. }
  74. }
  75. private static RemoteCertificateValidationCallback _ignoreCertificates = new RemoteCertificateValidationCallback(delegate { return true; });
  76. private static TaskCompletionSource<bool> _applicationTaskCompletionSource = new TaskCompletionSource<bool>();
  77. private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager)
  78. {
  79. SystemEvents.SessionEnding += SystemEvents_SessionEnding;
  80. // Allow all https requests
  81. ServicePointManager.ServerCertificateValidationCallback = _ignoreCertificates;
  82. _appHost = new ApplicationHost(appPaths, logManager);
  83. Console.WriteLine ("appHost.Init");
  84. var initProgress = new Progress<double>();
  85. var task = _appHost.Init(initProgress);
  86. Task.WaitAll (task);
  87. Console.WriteLine ("Running startup tasks");
  88. task = _appHost.RunStartupTasks();
  89. Task.WaitAll (task);
  90. task = _applicationTaskCompletionSource.Task;
  91. Task.WaitAll (task);
  92. }
  93. /// <summary>
  94. /// Handles the SessionEnding event of the SystemEvents control.
  95. /// </summary>
  96. /// <param name="sender">The source of the event.</param>
  97. /// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
  98. static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
  99. {
  100. if (e.Reason == SessionEndReasons.SystemShutdown)
  101. {
  102. Shutdown();
  103. }
  104. }
  105. /// <summary>
  106. /// Begins the log.
  107. /// </summary>
  108. /// <param name="logger">The logger.</param>
  109. private static void BeginLog(ILogger logger)
  110. {
  111. logger.Info("Media Browser Server started");
  112. logger.Info("Command line: {0}", string.Join(" ", Environment.GetCommandLineArgs()));
  113. logger.Info("Server: {0}", Environment.MachineName);
  114. logger.Info("Operating system: {0}", Environment.OSVersion.ToString());
  115. }
  116. /// <summary>
  117. /// Handles the UnhandledException event of the CurrentDomain control.
  118. /// </summary>
  119. /// <param name="sender">The source of the event.</param>
  120. /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
  121. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  122. {
  123. var exception = (Exception)e.ExceptionObject;
  124. LogUnhandledException(exception);
  125. if (!Debugger.IsAttached)
  126. {
  127. Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
  128. }
  129. }
  130. private static void LogUnhandledException(Exception ex)
  131. {
  132. _logger.ErrorException("UnhandledException", ex);
  133. _appHost.LogManager.Flush ();
  134. var path = Path.Combine(_appHost.ServerConfigurationManager.ApplicationPaths.LogDirectoryPath, "crash_" + Guid.NewGuid() + ".txt");
  135. var builder = LogHelper.GetLogMessage(ex);
  136. Console.WriteLine ("UnhandledException");
  137. Console.WriteLine (builder.ToString());
  138. File.WriteAllText(path, builder.ToString());
  139. }
  140. /// <summary>
  141. /// Performs the update if needed.
  142. /// </summary>
  143. /// <param name="appPaths">The app paths.</param>
  144. /// <param name="logger">The logger.</param>
  145. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  146. private static bool PerformUpdateIfNeeded(ServerApplicationPaths appPaths, ILogger logger)
  147. {
  148. return false;
  149. }
  150. public static void Shutdown()
  151. {
  152. _applicationTaskCompletionSource.SetResult (true);
  153. }
  154. public static void Restart()
  155. {
  156. // Second instance will start first, so dispose so that the http ports will be available to the new instance
  157. _appHost.Dispose();
  158. // Right now this method will just shutdown, but not restart
  159. Shutdown ();
  160. }
  161. }
  162. class NoCheckCertificatePolicy : ICertificatePolicy
  163. {
  164. public bool CheckValidationResult (ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
  165. {
  166. return true;
  167. }
  168. }
  169. }