using System.Collections.Generic;
using System.Threading;
using LeanCloud.Storage;
namespace LeanCloud.Engine {
///
/// LCEngineRequestContext provides the context of engine request.
///
public class LCEngineRequestContext {
public const string RemoteAddressKey = "__remoteAddressKey";
public const string SessionTokenKey = "__sessionToken";
public const string CurrentUserKey = "__currentUser";
private static ThreadLocal> requestContext = new ThreadLocal>();
public static void Init() {
if (requestContext.IsValueCreated) {
requestContext.Value.Clear();
}
requestContext.Value = new Dictionary();
}
public static void Set(string key, object value) {
if (!requestContext.IsValueCreated) {
requestContext.Value = new Dictionary();
}
requestContext.Value[key] = value;
}
public static object Get(string key) {
if (!requestContext.IsValueCreated) {
return null;
}
return requestContext.Value[key];
}
///
/// The remote address of this request.
///
public static string RemoteAddress {
get {
object remoteAddress = Get(RemoteAddressKey);
if (remoteAddress != null) {
return remoteAddress as string;
}
return null;
}
set {
Set(RemoteAddressKey, value);
}
}
///
/// The session token of this request.
///
public static string SessionToken {
get {
object sessionToken = Get(SessionTokenKey);
if (sessionToken != null) {
return sessionToken as string;
}
return null;
}
set {
Set(SessionTokenKey, value);
}
}
///
/// The user of this request.
///
public static LCUser CurrentUser {
get {
object currentUser = Get(CurrentUserKey);
if (currentUser != null) {
return currentUser as LCUser;
}
return null;
}
set {
Set(CurrentUserKey, value);
}
}
}
}