public void ProcessRequest(HttpContext context) { lock (m_containers) { string roomIdStr = context.Request.QueryString["roomId"]; string identity = context.Request.QueryString["identity"]; string cmd = context.Request.QueryString["cmd"]; Guid roomId; if (Guid.TryParse(roomIdStr, out roomId)) { m_roomId = roomId; if (!string.IsNullOrEmpty(cmd)) { Guid id; if (!Guid.TryParse(identity, out id) || id != ServerContainer.ServerIdentity) { context.Response.StatusCode = 404; return; } if (cmd == "create") { if (!m_containers.ContainsKey(roomId)) { MatchServerContainer container = new MatchServerContainer(); container.Run(); m_containers.Add(roomId, container); m_lastCheckedTime.Add(roomId, DateTime.UtcNow); } } } if (!m_containers.ContainsKey(roomId)) { context.Response.StatusCode = 404; return; } } else { context.Response.StatusCode = 404; return; } } if (context.IsWebSocketRequest) { context.AcceptWebSocketRequest(WebSocketRequestHandler); } }
private static void DoGC(int timeout) { lock (m_containers) { List <Guid> removeContainers = null; foreach (KeyValuePair <Guid, DateTime> kvp in m_lastCheckedTime) { DateTime checkedTime = kvp.Value; if (DateTime.UtcNow > checkedTime.AddSeconds(timeout)) { if (removeContainers == null) { removeContainers = new List <Guid>(); } removeContainers.Add(kvp.Key); } } if (removeContainers != null) { for (int i = 0; i < removeContainers.Count; ++i) { Guid roomId = removeContainers[i]; MatchServerContainer container = m_containers[roomId]; if (container.ConnectionsCount == 0) { container.Stop(); m_containers.Remove(roomId); m_lastCheckedTime.Remove(roomId); } else { m_lastCheckedTime[roomId] = DateTime.UtcNow; } } } } }