private bool ValidateSession(CHttpRequest arequest) { Cookie sessionCookie = arequest.Request.Cookies["sessionId"]; CHttpSession sessionObject = null; if ((sessionCookie == null) || ((sessionCookie != null) && (sessionCookie.Value == ""))) { sessionObject = arequest.Server.ServerSessions.GetSession(""); arequest.Response.Cookies.Add(new Cookie("sessionId", sessionObject.SessionId)); } else { sessionObject = arequest.Server.ServerSessions.GetSession(sessionCookie.Value); } if (sessionObject != null) { try { sessionObject.TouchTime = DateTime.Now; arequest.Session = sessionObject; CHttpResponse response = new CHttpTextResponse("Hello with session id " + sessionObject.SessionId + " touchTime " + sessionObject.TouchTime.ToString()); response.SendResponse(HttpStatusCode.OK, arequest); } finally { arequest.Session.IsUsed = false; } } else { arequest.Response.Cookies.Add(new Cookie("sessionId", "")); arequest.SendServerExpired(arequest); } return(true); }
public CHttpSession GetSession(string asessionId) { CHttpSession result = null; int repeats = 0; int waitResult = 0; bool alreadyChecked = false; bool running = !haltEvent.WaitOne(0, false); WaitHandle[] listLocks = new WaitHandle[] { lockMutex, haltEvent, checkStartedEvent }; WaitHandle[] flushLocks = new WaitHandle[] { haltEvent, checkFinishedEvent }; while (running && (repeats <= getSessionRepeats) && (result == null) && !alreadyChecked) { waitResult = WaitHandle.WaitAny(listLocks, getSessionTimeout, false); if (waitResult == 0) { try { if (asessionId == "") { result = new CHttpSession(true); result.IsUsed = true; list.Add(result); } else { result = list.Find(session => session.SessionId == asessionId); if (result != null) { if ((result.TouchTime.AddMilliseconds(expirationTime) <= DateTime.Now) && !result.IsUsed) { parentServer.ServerLog.LogInfo("Removing expired session " + result.SessionId); list.Remove(result); result = null; } else { result.IsUsed = true; } } } alreadyChecked = true; } finally { lockMutex.ReleaseMutex(); } } else if (waitResult == 2) { WaitHandle.WaitAny(flushLocks, 5000, false); } repeats++; running = !haltEvent.WaitOne(0, false); } return(result); }
private void CheckExpired() { bool haltPending = false; while (!haltPending) { if (!haltEvent.WaitOne(checkExpiredPeriod, false)) { if (lockMutex.WaitOne(forceExpiredTimeout, false)) { try { for (int counter = list.Count - 1; counter >= 0; --counter) { CHttpSession session = list[counter]; if (session.TouchTime.AddMilliseconds(expirationTime) <= DateTime.Now) { parentServer.ServerLog.LogInfo("Removing expired session " + session.SessionId); list.RemoveAt(counter); } } } finally { checkFinishedEvent.Set(); checkStartedEvent.Reset(); lockMutex.ReleaseMutex(); } } else { checkFinishedEvent.Reset(); checkStartedEvent.Set(); } } else { haltPending = true; } } }