namespace Framework.Session
{
using Framework.Json;
using Framework.Server;
using Microsoft.AspNetCore.Http;
using System;
internal static class UtilSession
{
///
/// Serialize session state.
///
public static void Serialize(AppJson appJson, out string jsonClient)
{
appJson.RequestJson = null;
UtilStopwatch.TimeStart("Serialize");
UtilJson.Serialize(appJson, out string jsonSession, out jsonClient);
UtilStopwatch.TimeStop("Serialize");
if (!appJson.IsPageNotFound) // Do not serialize custom error page.
{
UtilServer.Session.SetString("AppInternal", jsonSession);
}
UtilStopwatch.Log(string.Format("JsonSession.Length={0:n0}; JsonClient.Length={1:n0};", jsonSession.Length, jsonClient.Length));
}
///
/// Deserialize session state.
///
public static AppJson Deserialize()
{
AppJson result = null;
string json = UtilServer.Session.GetString("AppInternal");
if (!string.IsNullOrEmpty(json)) // Not session expired.
{
UtilStopwatch.TimeStart("Deserialize");
result = (AppJson)UtilJson.Deserialize(json);
UtilStopwatch.TimeStop("Deserialize");
}
return result;
}
///
/// Returns true, if expected command has been sent by client.
///
public static bool Request(AppJson appJson, CommandEnum command, out CommandJson commandJson, out T componentJson) where T : ComponentJson
{
bool result = false;
commandJson = appJson.RequestJson.CommandGet();
componentJson = (T)null;
if (command == commandJson.CommandEnum)
{
result = true;
componentJson = (T)appJson.Root.RootComponentJsonList[commandJson.ComponentId];
}
return result;
}
}
}