mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Implement file polling for disk session cache
For use in case FileSystemWatcher does not work, see #684
This commit is contained in:
parent
5e11348e6f
commit
2ebc8eded5
3 changed files with 86 additions and 9 deletions
|
|
@ -97,6 +97,7 @@
|
|||
<Compile Include="Commands\Script.cs" />
|
||||
<Compile Include="Commands\Send.cs" />
|
||||
<Compile Include="Commands\Set.cs" />
|
||||
<Compile Include="Protocol\Session\SessionFileMonitor.cs" />
|
||||
<Compile Include="WinAPI\ConsoleIcon.cs" />
|
||||
<Compile Include="ConsoleIO.cs" />
|
||||
<Compile Include="Crypto\Streams\BouncyAes\AesFastEngine.cs" />
|
||||
|
|
|
|||
|
|
@ -23,8 +23,8 @@ namespace MinecraftClient.Protocol.Session
|
|||
"launcher_profiles.json"
|
||||
);
|
||||
|
||||
private static SessionFileMonitor cachemonitor;
|
||||
private static Dictionary<string, SessionToken> sessions = new Dictionary<string, SessionToken>();
|
||||
private static FileSystemWatcher cachemonitor = new FileSystemWatcher();
|
||||
private static Timer updatetimer = new Timer(100);
|
||||
private static List<KeyValuePair<string, SessionToken>> pendingadds = new List<KeyValuePair<string, SessionToken>>();
|
||||
private static BinaryFormatter formatter = new BinaryFormatter();
|
||||
|
|
@ -81,15 +81,8 @@ namespace MinecraftClient.Protocol.Session
|
|||
/// <returns>TRUE if session tokens are seeded from file</returns>
|
||||
public static bool InitializeDiskCache()
|
||||
{
|
||||
cachemonitor.Path = AppDomain.CurrentDomain.BaseDirectory;
|
||||
cachemonitor.IncludeSubdirectories = false;
|
||||
cachemonitor.Filter = SessionCacheFilePlaintext;
|
||||
cachemonitor.NotifyFilter = NotifyFilters.LastWrite;
|
||||
cachemonitor.Changed += new FileSystemEventHandler(OnChanged);
|
||||
cachemonitor.EnableRaisingEvents = true;
|
||||
|
||||
cachemonitor = new SessionFileMonitor(AppDomain.CurrentDomain.BaseDirectory, SessionCacheFilePlaintext, new FileSystemEventHandler(OnChanged));
|
||||
updatetimer.Elapsed += HandlePending;
|
||||
|
||||
return LoadFromDisk();
|
||||
}
|
||||
|
||||
|
|
|
|||
83
MinecraftClient/Protocol/Session/SessionFileMonitor.cs
Normal file
83
MinecraftClient/Protocol/Session/SessionFileMonitor.cs
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient.Protocol.Session
|
||||
{
|
||||
/// <summary>
|
||||
/// Monitor session file changes on disk
|
||||
/// </summary>
|
||||
class SessionFileMonitor
|
||||
{
|
||||
private FileSystemWatcher monitor;
|
||||
private Thread polling;
|
||||
|
||||
/// <summary>
|
||||
/// Create a new SessionFileMonitor and start monitoring
|
||||
/// </summary>
|
||||
/// <param name="folder">Folder to monitor</param>
|
||||
/// <param name="filename">Filename inside folder</param>
|
||||
/// <param name="handler">Callback for file changes</param>
|
||||
public SessionFileMonitor(string folder, string filename, FileSystemEventHandler handler)
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted("§8Initializing disk session cache using FileSystemWatcher");
|
||||
|
||||
try
|
||||
{
|
||||
monitor = new FileSystemWatcher();
|
||||
monitor.Path = folder;
|
||||
monitor.IncludeSubdirectories = false;
|
||||
monitor.Filter = filename;
|
||||
monitor.NotifyFilter = NotifyFilters.LastWrite;
|
||||
monitor.Changed += handler;
|
||||
monitor.EnableRaisingEvents = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (Settings.DebugMessages)
|
||||
ConsoleIO.WriteLineFormatted("§8Failed to initialize FileSystemWatcher, retrying using Polling");
|
||||
|
||||
polling = new Thread(() => PollingThread(folder, filename, handler));
|
||||
polling.Start();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fallback polling thread for use when operating system does not support FileSystemWatcher
|
||||
/// </summary>
|
||||
/// <param name="folder">Folder to monitor</param>
|
||||
/// <param name="filename">File name to monitor</param>
|
||||
/// <param name="handler">Callback when file changes</param>
|
||||
private void PollingThread(string folder, string filename, FileSystemEventHandler handler)
|
||||
{
|
||||
string filePath = String.Concat(folder, Path.DirectorySeparatorChar, filename);
|
||||
DateTime lastWrite = GetLastWrite(filePath);
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(5000);
|
||||
DateTime lastWriteNew = GetLastWrite(filePath);
|
||||
if (lastWriteNew != lastWrite)
|
||||
{
|
||||
lastWrite = lastWriteNew;
|
||||
handler(this, new FileSystemEventArgs(WatcherChangeTypes.Changed, folder, filename));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get last write for a given file
|
||||
/// </summary>
|
||||
/// <param name="path">File path to get last write from</param>
|
||||
/// <returns>Last write time, or DateTime.MinValue if the file does not exist</returns>
|
||||
private DateTime GetLastWrite(string path)
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(path);
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
return fileInfo.LastWriteTime;
|
||||
}
|
||||
else return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue