Minecraft-Console-Client/MinecraftClient/Protocol/SessionCache/SessionCache.cs

190 lines
7 KiB
C#
Raw Normal View History

2016-03-02 18:16:19 -07:00
using MinecraftClient.Protocol;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Timers;
2016-03-02 18:16:19 -07:00
namespace MinecraftClient.Protocol.SessionCache
2016-03-02 18:16:19 -07:00
{
/// <summary>
/// Handle sessions caching and storage.
2016-03-02 18:16:19 -07:00
/// </summary>
public static class SessionCache
{
private const string SessionCacheFile = "SessionCache.db";
2016-03-02 18:16:19 -07:00
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>>();
2016-03-02 18:16:19 -07:00
private static BinaryFormatter formatter = new BinaryFormatter();
/// <summary>
/// Retrieve whether SessionCache contains a session for the given login.
/// </summary>
/// <param name="login">User login used with Minecraft.net</param>
2016-03-02 18:16:19 -07:00
/// <returns>TRUE if session is available</returns>
public static bool Contains(string login)
{
return sessions.ContainsKey(login);
}
/// <summary>
/// Store a session and save it to disk if required.
/// </summary>
/// <param name="login">User login used with Minecraft.net</param>
2016-03-02 18:16:19 -07:00
/// <param name="session">User session token used with Minecraft.net</param>
public static void Store(string login, SessionToken session)
{
if (Contains(login))
{
sessions[login] = session;
}
else
{
sessions.Add(login, session);
}
if (Settings.SessionCaching == CacheType.Disk && updatetimer.Enabled == true)
{
pendingadds.Add(new KeyValuePair<string, SessionToken>(login, session));
}
else if (Settings.SessionCaching == CacheType.Disk)
2016-03-02 18:16:19 -07:00
{
SaveToDisk();
}
}
/// <summary>
/// Retrieve a session token for the given login.
/// </summary>
/// <param name="login">User login used with Minecraft.net</param>
2016-03-02 18:16:19 -07:00
/// <returns>SessionToken for given login</returns>
public static SessionToken Get(string login)
{
return sessions[login];
}
/// <summary>
/// Initialize cache monitoring to keep cache updated with external changes.
/// </summary>
2016-03-02 18:16:19 -07:00
/// <returns>TRUE if session tokens are seeded from file</returns>
public static bool InitializeDiskCache()
{
cachemonitor.Path = AppDomain.CurrentDomain.BaseDirectory;
cachemonitor.IncludeSubdirectories = false;
cachemonitor.Filter = SessionCacheFile;
2016-03-02 18:16:19 -07:00
cachemonitor.NotifyFilter = NotifyFilters.LastWrite;
cachemonitor.Changed += new FileSystemEventHandler(OnChanged);
cachemonitor.EnableRaisingEvents = true;
updatetimer.Elapsed += HandlePending;
2016-03-02 18:16:19 -07:00
return LoadFromDisk();
}
/// <summary>
/// Reloads cache on external cache file change.
/// </summary>
/// <param name="sender">Sender</param>
2016-03-02 18:16:19 -07:00
/// <param name="e">Event data</param>
private static void OnChanged(object sender, FileSystemEventArgs e)
{
updatetimer.Stop();
updatetimer.Start();
}
/// <summary>
/// Called after timer elapsed. Reads disk cache and adds new/modified sessions back.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event data</param>
private static void HandlePending(object sender, ElapsedEventArgs e)
2016-03-02 18:16:19 -07:00
{
updatetimer.Stop();
2016-03-02 18:16:19 -07:00
LoadFromDisk();
foreach(KeyValuePair<string, SessionToken> pending in pendingadds.ToArray())
{
Store(pending.Key, pending.Value);
pendingadds.Remove(pending);
}
2016-03-02 18:16:19 -07:00
}
/// <summary>
/// Reads cache file and loads SessionTokens into SessionCache.
/// </summary>
2016-03-02 18:16:19 -07:00
/// <returns>True if data is successfully loaded</returns>
private static bool LoadFromDisk()
{
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted("§8Updating session cache from disk");
if (File.Exists(SessionCacheFile))
2016-03-02 18:16:19 -07:00
{
try
{
using (FileStream fs = new FileStream(SessionCacheFile, FileMode.Open, FileAccess.Read, FileShare.Read))
2016-03-02 18:16:19 -07:00
{
sessions = (Dictionary<string, SessionToken>)formatter.Deserialize(fs);
return true;
}
}
catch (IOException ex)
{
ConsoleIO.WriteLineFormatted("§8Failed to read session cache from disk: " + ex.Message);
2016-03-02 18:16:19 -07:00
}
catch (SerializationException ex2)
2016-03-02 18:16:19 -07:00
{
ConsoleIO.WriteLineFormatted("§8Got malformed data while reading session cache from disk: " + ex2.Message);
2016-03-02 18:16:19 -07:00
}
}
return false;
}
/// <summary>
/// Saves SessionToken's from SessionCache into cache file.
2016-03-02 18:16:19 -07:00
/// </summary>
private static void SaveToDisk()
{
if (Settings.DebugMessages)
ConsoleIO.WriteLineFormatted("§8Saving session cache to disk");
bool fileexists = File.Exists(SessionCacheFile);
IOException lastEx = null;
int attempt = 1;
2016-03-02 18:16:19 -07:00
while (attempt < 4)
2016-03-02 18:16:19 -07:00
{
try
{
using (FileStream fs = new FileStream(SessionCacheFile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
cachemonitor.EnableRaisingEvents = false;
// delete existing file contents
if (fileexists)
{
fs.SetLength(0);
fs.Flush();
}
2016-03-02 18:16:19 -07:00
formatter.Serialize(fs, sessions);
cachemonitor.EnableRaisingEvents = true;
}
return;
}
catch (IOException ex)
2016-03-02 18:16:19 -07:00
{
lastEx = ex;
attempt++;
System.Threading.Thread.Sleep(new Random().Next(150, 350) * attempt); //CSMA/CD :)
2016-03-02 18:16:19 -07:00
}
}
ConsoleIO.WriteLineFormatted("§8Failed to write session cache to disk" + (lastEx != null ? ": " + lastEx.Message : ""));
2016-03-02 18:16:19 -07:00
}
}
}