2013-09-26 14:20:26 -07:00
|
|
|
using MediaBrowser.Common.Implementations.Logging;
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
using MediaBrowser.Server.Implementations;
|
2014-11-09 11:24:57 -07:00
|
|
|
using MediaBrowser.Server.Mono.Native;
|
|
|
|
using MediaBrowser.Server.Startup.Common;
|
2013-09-26 14:20:26 -07:00
|
|
|
using Microsoft.Win32;
|
2013-09-24 14:06:21 -07:00
|
|
|
using System;
|
2013-09-26 14:20:26 -07:00
|
|
|
using System.Diagnostics;
|
2015-05-23 15:01:13 -07:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2013-10-14 08:08:33 -07:00
|
|
|
using System.Net;
|
|
|
|
using System.Net.Security;
|
2014-10-06 16:58:46 -07:00
|
|
|
using System.Reflection;
|
2013-10-14 08:08:33 -07:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2013-09-26 14:20:26 -07:00
|
|
|
using System.Threading.Tasks;
|
2015-10-03 21:23:11 -07:00
|
|
|
using CommonIO;
|
|
|
|
using MediaBrowser.Server.Implementations.Logging;
|
2013-09-24 14:06:21 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Mono
|
|
|
|
{
|
2015-05-23 15:28:26 -07:00
|
|
|
public class MainClass
|
|
|
|
{
|
|
|
|
private static ApplicationHost _appHost;
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
private static ILogger _logger;
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
public static void Main(string[] args)
|
|
|
|
{
|
2014-11-13 23:27:10 -07:00
|
|
|
var applicationPath = Assembly.GetEntryAssembly().Location;
|
2014-09-14 08:26:33 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
var options = new StartupOptions();
|
|
|
|
|
|
|
|
// Allow this to be specified on the command line.
|
|
|
|
var customProgramDataPath = options.GetOption("-programdata");
|
2013-12-04 07:52:38 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
var appPaths = CreateApplicationPaths(applicationPath, customProgramDataPath);
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
|
|
|
|
logManager.ReloadLogger(LogSeverity.Info);
|
|
|
|
logManager.AddConsoleOutput();
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
var logger = _logger = logManager.GetLogger("Main");
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2014-11-13 23:27:10 -07:00
|
|
|
ApplicationHost.LogEnvironmentInfo(logger, appPaths, true);
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
RunApplication(appPaths, logManager, options);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
logger.Info("Shutting down");
|
|
|
|
|
|
|
|
_appHost.Dispose();
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
private static ServerApplicationPaths CreateApplicationPaths(string applicationPath, string programDataPath)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(programDataPath))
|
|
|
|
{
|
|
|
|
programDataPath = ApplicationPathHelper.GetProgramDataPath(applicationPath);
|
|
|
|
}
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
return new ServerApplicationPaths(programDataPath, applicationPath, Path.GetDirectoryName(applicationPath));
|
|
|
|
}
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
private static readonly TaskCompletionSource<bool> ApplicationTaskCompletionSource = new TaskCompletionSource<bool>();
|
2013-12-05 09:50:21 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, StartupOptions options)
|
|
|
|
{
|
|
|
|
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
// Allow all https requests
|
|
|
|
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
|
2013-10-14 08:08:33 -07:00
|
|
|
|
2015-11-11 12:06:56 -07:00
|
|
|
var fileSystem = new ManagedFileSystem(new PatternsLogger(logManager.GetLogger("FileSystem")), false, false);
|
2015-10-03 21:23:11 -07:00
|
|
|
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
|
2014-10-06 16:58:46 -07:00
|
|
|
|
2016-05-01 14:48:37 -07:00
|
|
|
var nativeApp = new NativeApp(options, logManager.GetLogger("App"));
|
2014-11-09 11:24:57 -07:00
|
|
|
|
2016-01-03 12:01:05 -07:00
|
|
|
_appHost = new ApplicationHost(appPaths, logManager, options, fileSystem, "emby.mono.zip", nativeApp);
|
2015-05-23 15:28:26 -07:00
|
|
|
|
|
|
|
if (options.ContainsOption("-v"))
|
|
|
|
{
|
|
|
|
Console.WriteLine(_appHost.ApplicationVersion.ToString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine("appHost.Init");
|
|
|
|
|
|
|
|
var initProgress = new Progress<double>();
|
|
|
|
|
|
|
|
var task = _appHost.Init(initProgress);
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
|
|
|
Console.WriteLine("Running startup tasks");
|
|
|
|
|
|
|
|
task = _appHost.RunStartupTasks();
|
|
|
|
Task.WaitAll(task);
|
|
|
|
|
|
|
|
task = ApplicationTaskCompletionSource.Task;
|
|
|
|
|
|
|
|
Task.WaitAll(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the SessionEnding event of the SystemEvents control.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
|
|
|
|
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Reason == SessionEndReasons.SystemShutdown)
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the UnhandledException event of the CurrentDomain control.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
|
|
|
|
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
|
|
{
|
|
|
|
var exception = (Exception)e.ExceptionObject;
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2014-11-23 15:36:40 -07:00
|
|
|
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
if (!Debugger.IsAttached)
|
|
|
|
{
|
|
|
|
Environment.Exit(System.Runtime.InteropServices.Marshal.GetHRForException(exception));
|
|
|
|
}
|
|
|
|
}
|
2013-09-26 14:20:26 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
public static void Shutdown()
|
|
|
|
{
|
|
|
|
ApplicationTaskCompletionSource.SetResult(true);
|
|
|
|
}
|
2015-05-23 15:01:13 -07:00
|
|
|
|
2015-06-05 07:27:01 -07:00
|
|
|
public static void Restart(StartupOptions startupOptions)
|
2015-05-23 15:01:13 -07:00
|
|
|
{
|
|
|
|
_logger.Info("Disposing app host");
|
|
|
|
_appHost.Dispose();
|
|
|
|
|
|
|
|
_logger.Info("Starting new instance");
|
|
|
|
|
2015-06-05 07:27:01 -07:00
|
|
|
string module = startupOptions.GetOption("-restartpath");
|
|
|
|
string commandLineArgsString = startupOptions.GetOption("-restartargs") ?? string.Empty;
|
2015-05-23 15:01:13 -07:00
|
|
|
|
2015-06-05 07:27:01 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(module))
|
|
|
|
{
|
|
|
|
module = Environment.GetCommandLineArgs().First();
|
|
|
|
}
|
|
|
|
if (!startupOptions.ContainsOption("-restartargs"))
|
|
|
|
{
|
|
|
|
var args = Environment.GetCommandLineArgs()
|
|
|
|
.Skip(1)
|
|
|
|
.Select(NormalizeCommandLineArgument);
|
|
|
|
|
|
|
|
commandLineArgsString = string.Join(" ", args.ToArray());
|
|
|
|
}
|
2015-05-23 15:28:26 -07:00
|
|
|
|
|
|
|
_logger.Info("Executable: {0}", module);
|
|
|
|
_logger.Info("Arguments: {0}", commandLineArgsString);
|
2015-05-23 15:01:13 -07:00
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
Process.Start(module, commandLineArgsString);
|
2015-05-23 15:01:13 -07:00
|
|
|
|
|
|
|
_logger.Info("Calling Environment.Exit");
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
|
2015-05-23 15:28:26 -07:00
|
|
|
private static string NormalizeCommandLineArgument(string arg)
|
|
|
|
{
|
|
|
|
if (arg.IndexOf(" ", StringComparison.OrdinalIgnoreCase) == -1)
|
|
|
|
{
|
2015-05-23 15:01:13 -07:00
|
|
|
return arg;
|
2015-05-23 15:28:26 -07:00
|
|
|
}
|
2015-05-23 15:01:13 -07:00
|
|
|
|
|
|
|
return "\"" + arg + "\"";
|
2015-05-23 15:28:26 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NoCheckCertificatePolicy : ICertificatePolicy
|
|
|
|
{
|
|
|
|
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 14:06:21 -07:00
|
|
|
}
|