aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-05-02 01:49:16 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-05-02 01:49:16 -0400
commitf6ef72732178db79176d238206ca83150f6c80d4 (patch)
tree9e0a32286a6a5ff014f1fa777bb17cebc4097abe /MediaBrowser.ServerApplication
parent780d5b914cc22bdf88477761e60c5de64b20504d (diff)
parentcd02373e554df232d88063f41b8aee391f3e7667 (diff)
Merge branch 'dev'
Diffstat (limited to 'MediaBrowser.ServerApplication')
-rw-r--r--MediaBrowser.ServerApplication/MainStartup.cs19
-rw-r--r--MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj25
-rw-r--r--MediaBrowser.ServerApplication/Native/SqliteExtensions.cs62
-rw-r--r--MediaBrowser.ServerApplication/Native/Standby.cs40
-rw-r--r--MediaBrowser.ServerApplication/Native/WindowsApp.cs14
-rw-r--r--MediaBrowser.ServerApplication/ServerNotifyIcon.cs7
-rw-r--r--MediaBrowser.ServerApplication/packages.config2
7 files changed, 138 insertions, 31 deletions
diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs
index cbf7d298c9..dc61dcda8e 100644
--- a/MediaBrowser.ServerApplication/MainStartup.cs
+++ b/MediaBrowser.ServerApplication/MainStartup.cs
@@ -273,11 +273,13 @@ namespace MediaBrowser.ServerApplication
}
private static ServerNotifyIcon _serverNotifyIcon;
+ private static TaskScheduler _mainTaskScheduler;
private static void ShowTrayIcon()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
_serverNotifyIcon = new ServerNotifyIcon(_appHost.LogManager, _appHost, _appHost.ServerConfigurationManager, _appHost.LocalizationManager);
+ _mainTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Application.Run();
}
@@ -321,6 +323,18 @@ namespace MediaBrowser.ServerApplication
}
}
+ public static void Invoke(Action action)
+ {
+ if (_isRunningAsService)
+ {
+ action();
+ }
+ else
+ {
+ Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, _mainTaskScheduler ?? TaskScheduler.Current);
+ }
+ }
+
/// <summary>
/// Starts the service.
/// </summary>
@@ -555,9 +569,10 @@ namespace MediaBrowser.ServerApplication
private static void ShutdownWindowsApplication()
{
- _logger.Info("Calling Application.Exit");
- Application.Exit();
+ //_logger.Info("Calling Application.Exit");
+ //Application.Exit();
+ _logger.Info("Calling Environment.Exit");
Environment.Exit(0);
_logger.Info("Calling ApplicationTaskCompletionSource.SetResult");
diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj
index 291e63dac7..f544a149d8 100644
--- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj
+++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj
@@ -13,6 +13,8 @@
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<TargetFrameworkProfile />
+ <NuGetPackageImportStamp>
+ </NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -81,9 +83,9 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
- <Reference Include="System.Data.SQLite, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
- <SpecificVersion>False</SpecificVersion>
- <HintPath>..\packages\System.Data.SQLite.Core.1.0.94.0\lib\net45\System.Data.SQLite.dll</HintPath>
+ <Reference Include="System.Data.SQLite, Version=1.0.101.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
+ <HintPath>..\packages\System.Data.SQLite.Core.1.0.101.0\lib\net46\System.Data.SQLite.dll</HintPath>
+ <Private>True</Private>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.ServiceProcess" />
@@ -112,6 +114,7 @@
</Compile>
<Compile Include="MainStartup.cs" />
<Compile Include="Native\LnkShortcutHandler.cs" />
+ <Compile Include="Native\SqliteExtensions.cs" />
<Compile Include="Native\Standby.cs" />
<Compile Include="Native\ServerAuthorization.cs" />
<Compile Include="Native\WindowsApp.cs" />
@@ -156,14 +159,6 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
- <Content Include="..\packages\System.Data.SQLite.Core.1.0.94.0\build\net45\x64\SQLite.Interop.dll">
- <Link>x64\SQLite.Interop.dll</Link>
- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
- </Content>
- <Content Include="..\packages\System.Data.SQLite.Core.1.0.94.0\build\net45\x86\SQLite.Interop.dll">
- <Link>x86\SQLite.Interop.dll</Link>
- <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
- </Content>
<Content Include="..\Tools\Installation\MediaBrowser.InstallUtil.dll">
<Link>MediaBrowser.InstallUtil.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -1116,6 +1111,13 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
+ <Import Project="..\packages\System.Data.SQLite.Core.1.0.101.0\build\net46\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.101.0\build\net46\System.Data.SQLite.Core.targets')" />
+ <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+ <PropertyGroup>
+ <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
+ </PropertyGroup>
+ <Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.101.0\build\net46\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.101.0\build\net46\System.Data.SQLite.Core.targets'))" />
+ </Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
@@ -1123,5 +1125,4 @@
<Target Name="AfterBuild">
</Target>
-->
- <Import Project="..\packages\System.Data.SQLite.Core.1.0.94.0\build\net45\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.94.0\build\net45\System.Data.SQLite.Core.targets')" />
</Project> \ No newline at end of file
diff --git a/MediaBrowser.ServerApplication/Native/SqliteExtensions.cs b/MediaBrowser.ServerApplication/Native/SqliteExtensions.cs
new file mode 100644
index 0000000000..1cde2ea139
--- /dev/null
+++ b/MediaBrowser.ServerApplication/Native/SqliteExtensions.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Data;
+using System.Data.SQLite;
+using System.Threading.Tasks;
+using MediaBrowser.Model.Logging;
+using MediaBrowser.Server.Implementations.Persistence;
+
+namespace MediaBrowser.ServerApplication.Native
+{
+ /// <summary>
+ /// Class SQLiteExtensions
+ /// </summary>
+ static class SqliteExtensions
+ {
+ /// <summary>
+ /// Connects to db.
+ /// </summary>
+ /// <param name="dbPath">The db path.</param>
+ /// <param name="logger">The logger.</param>
+ /// <returns>Task{IDbConnection}.</returns>
+ /// <exception cref="System.ArgumentNullException">dbPath</exception>
+ public static async Task<IDbConnection> ConnectToDb(string dbPath, ILogger logger)
+ {
+ if (string.IsNullOrEmpty(dbPath))
+ {
+ throw new ArgumentNullException("dbPath");
+ }
+
+ logger.Info("Sqlite {0} opening {1}", SQLiteConnection.SQLiteVersion, dbPath);
+
+ var connectionstr = new SQLiteConnectionStringBuilder
+ {
+ PageSize = 4096,
+ CacheSize = 2000,
+ SyncMode = SynchronizationModes.Full,
+ DataSource = dbPath,
+ JournalMode = SQLiteJournalModeEnum.Wal
+ };
+
+ var connection = new SQLiteConnection(connectionstr.ConnectionString);
+
+ await connection.OpenAsync().ConfigureAwait(false);
+
+ return connection;
+ }
+ }
+
+ public class DbConnector : IDbConnector
+ {
+ private readonly ILogger _logger;
+
+ public DbConnector(ILogger logger)
+ {
+ _logger = logger;
+ }
+
+ public Task<IDbConnection> Connect(string dbPath)
+ {
+ return SqliteExtensions.ConnectToDb(dbPath, _logger);
+ }
+ }
+} \ No newline at end of file
diff --git a/MediaBrowser.ServerApplication/Native/Standby.cs b/MediaBrowser.ServerApplication/Native/Standby.cs
index 274c72b254..919709538f 100644
--- a/MediaBrowser.ServerApplication/Native/Standby.cs
+++ b/MediaBrowser.ServerApplication/Native/Standby.cs
@@ -1,4 +1,5 @@
-using System.Runtime.InteropServices;
+using System;
+using System.Runtime.InteropServices;
namespace MediaBrowser.ServerApplication.Native
{
@@ -7,11 +8,33 @@ namespace MediaBrowser.ServerApplication.Native
/// </summary>
public static class Standby
{
- public static void PreventSystemStandby()
+ public static void PreventSleepAndMonitorOff()
{
- SystemHelper.ResetStandbyTimer();
+ NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED | NativeMethods.ES_DISPLAY_REQUIRED);
}
+ public static void PreventSleep()
+ {
+ NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
+ }
+
+ // Clear EXECUTION_STATE flags to allow the system to sleep and turn off monitor normally
+ public static void AllowSleep()
+ {
+ NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS);
+ }
+
+ internal static class NativeMethods
+ {
+ // Import SetThreadExecutionState Win32 API and necessary flags
+ [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
+ public static extern uint SetThreadExecutionState(uint esFlags);
+ public const uint ES_CONTINUOUS = 0x80000000;
+ public const uint ES_SYSTEM_REQUIRED = 0x00000001;
+ public const uint ES_DISPLAY_REQUIRED = 0x00000002;
+ }
+
+ [Flags]
internal enum EXECUTION_STATE : uint
{
ES_NONE = 0,
@@ -21,16 +44,5 @@ namespace MediaBrowser.ServerApplication.Native
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000
}
-
- public class SystemHelper
- {
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
-
- public static void ResetStandbyTimer()
- {
- EXECUTION_STATE es = SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED);
- }
- }
}
}
diff --git a/MediaBrowser.ServerApplication/Native/WindowsApp.cs b/MediaBrowser.ServerApplication/Native/WindowsApp.cs
index 34f4315deb..808c7558ec 100644
--- a/MediaBrowser.ServerApplication/Native/WindowsApp.cs
+++ b/MediaBrowser.ServerApplication/Native/WindowsApp.cs
@@ -7,8 +7,10 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
+using System.Windows.Forms;
using CommonIO;
using MediaBrowser.Controller.Power;
+using MediaBrowser.Server.Implementations.Persistence;
using MediaBrowser.Server.Startup.Common.FFMpeg;
using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
@@ -137,7 +139,12 @@ namespace MediaBrowser.ServerApplication.Native
public void PreventSystemStandby()
{
- Standby.PreventSystemStandby();
+ MainStartup.Invoke(Standby.PreventSleep);
+ }
+
+ public void AllowSystemStandby()
+ {
+ MainStartup.Invoke(Standby.AllowSleep);
}
public IPowerManagement GetPowerManagement()
@@ -185,6 +192,11 @@ namespace MediaBrowser.ServerApplication.Native
}
}
+ public IDbConnector GetDbConnector()
+ {
+ return new DbConnector(_logger);
+ }
+
/// <summary>
/// Processes the exited.
/// </summary>
diff --git a/MediaBrowser.ServerApplication/ServerNotifyIcon.cs b/MediaBrowser.ServerApplication/ServerNotifyIcon.cs
index fbb93b6602..27816db5a4 100644
--- a/MediaBrowser.ServerApplication/ServerNotifyIcon.cs
+++ b/MediaBrowser.ServerApplication/ServerNotifyIcon.cs
@@ -36,10 +36,15 @@ namespace MediaBrowser.ServerApplication
set
{
Action act = () => notifyIcon1.Visible = false;
- contextMenuStrip1.Invoke(act);
+ Invoke(act);
}
}
+ public void Invoke(Action action)
+ {
+ contextMenuStrip1.Invoke(action);
+ }
+
public ServerNotifyIcon(ILogManager logManager,
IServerApplicationHost appHost,
IServerConfigurationManager configurationManager,
diff --git a/MediaBrowser.ServerApplication/packages.config b/MediaBrowser.ServerApplication/packages.config
index 1c86a02ae6..2d4baae256 100644
--- a/MediaBrowser.ServerApplication/packages.config
+++ b/MediaBrowser.ServerApplication/packages.config
@@ -3,5 +3,5 @@
<package id="CommonIO" version="1.0.0.9" targetFramework="net45" />
<package id="ImageMagickSharp" version="1.0.0.18" targetFramework="net45" />
<package id="Patterns.Logging" version="1.0.0.2" targetFramework="net45" />
- <package id="System.Data.SQLite.Core" version="1.0.94.0" targetFramework="net45" requireReinstallation="true" />
+ <package id="System.Data.SQLite.Core" version="1.0.101.0" targetFramework="net46" />
</packages> \ No newline at end of file