// note: we may have multiple connections with a host, so // possibly multiple entries per host (each with a different // id), so we do not use the host as the hashtable key static public void Add(string host, byte[] id) { lock (locker) { string uid = BitConverter.ToString(id); ClientSessionInfo si = (ClientSessionInfo)cache[uid]; if (si == null) { cache.Add(uid, new ClientSessionInfo(host, id)); } else if (si.HostName == host) { // we already have this and it's still valid // on the server, so we'll keep it a little longer si.KeepAlive(); } else { // it's very unlikely but the same session id // could be used by more than one host. In this // case we replace the older one with the new one si.Dispose(); cache.Remove(uid); cache.Add(uid, new ClientSessionInfo(host, id)); } } }
static public bool SetContextFromCache(Context context) { lock (locker) { ClientSessionInfo csi = FromContext(context, true); if (csi == null) { return(false); } csi.SetContext(context); csi.KeepAlive(); return(true); } }
static public bool SetContextInCache(Context context) { lock (locker) { // Don't check the validity because the masterKey of the ClientSessionInfo // can still be null when this is called the first time ClientSessionInfo csi = FromContext(context, false); if (csi == null) { return(false); } csi.GetContext(context); csi.KeepAlive(); return(true); } }
// only called inside the lock static private ClientSessionInfo FromContext(Context context, bool checkValidity) { if (context == null) { return(null); } byte[] id = context.SessionId; if ((id == null) || (id.Length == 0)) { return(null); } // do we have a session cached for this host ? string uid = BitConverter.ToString(id); ClientSessionInfo si = (ClientSessionInfo)cache[uid]; if (si == null) { return(null); } // In the unlikely case of multiple hosts using the same // session id, we just act like we do not know about it if (context.ClientSettings.TargetHost != si.HostName) { return(null); } // yes, so what's its status ? if (checkValidity && !si.Valid) { si.Dispose(); cache.Remove(uid); return(null); } // ok, it make sense return(si); }