using CommonNetwork_ICE.Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using CommonNetwork_ICE.Util; using CommonServer.Server; using CommonServer.Protocol; using CommonLang.Log; using CommonServer_ICE.Server; namespace CommonServer_ICE.Session { /// /// 服务器端Session管理器 /// internal class SessionManager { private static Logger log = LoggerFactory.GetLogger("SessionManager"); private static Dictionary sessionMap = new Dictionary(); private static Dictionary sessionMapByID = new Dictionary(); private static IceServerSessionExpireTask sessionExpireTask; /// /// 判断序列号为键的会话是否存在 /// /// 序列号键 /// public static ISession getSession(string sessionId) { ISession ret; if (sessionMapByID.TryGetValue(sessionId, out ret )) { return ret; } return null; } /// /// 根据IP地址和端口判断是否存在某个会话 /// /// /// /// public static ISession getSession(String ip, int port, int recvPort) { string key = getSessionStrKey(ip, port, recvPort); ISession ret; if (sessionMap.TryGetValue(key, out ret)) { return ret; } return null; } /// /// IP地址和端口为键增加会话 /// /// /// /// private static void addSession(String ip, IceServerIoSession ioSession) { string key = getSessionStrKey(ip, ioSession.ClientSentDataPort, ioSession.ClientRecvDataPort); sessionMap.Add(key, ioSession); sessionMapByID.Add(ioSession.ID, ioSession); log.Info("新增会话,键:" + key); } /// /// 删除会话 /// /// public static void removeSession(ISession session) { IceServerIoSession ioSession = (IceServerIoSession)session; IceConfig config = ioSession.Connector.GetConnectorConfig(); string key = getSessionStrKey(config.RemoteIp, ioSession.ClientSentDataPort, ioSession.ClientRecvDataPort); if (sessionMap.ContainsKey(key)) { sessionMap.Remove(key); sessionMapByID.Remove(session.ID); } } /// /// 判断指定会话是否存在 /// /// /// public static bool hasSession(ISession session) { return hasSession(session.ID); } /// /// 判断指定序列号的会话是否存在 /// /// /// public static bool hasSession(string sessionId) { if (sessionMapByID.ContainsKey(sessionId)) { return true; } return false; } /// /// 判断传入会话对象是否存在 /// /// /// public static bool hasSession(IceServerIoSession session) { IceConfig config = session.Connector.GetConnectorConfig(); return hasSession(config.RemoteIp, session.ClientSentDataPort, session.ClientRecvDataPort); } /// /// IP地址和端口为键获取会话 /// /// /// /// public static bool hasSession(String ip, int port, int recvPort) { string key = getSessionStrKey(ip, port, recvPort); if (sessionMap.ContainsKey(key)) { return true; } return false; } /// /// 获取所有会话 /// /// public static List getAllSession() { return sessionMap.Values.ToList(); } /// /// 创建Session /// /// /// /// /// /// public static IceServerIoSession createSession(IceConnectServer server, String ip, int port, int recvPort) { IceServerIoSession session = new IceServerIoSession(server); session.ClientSentDataPort = port; session.ClientRecvDataPort = recvPort; addSession(ip, session); return session; } /// /// 生成基于IP地址和端口的会话存储键 /// /// 发送方侦听端口 /// private static string getSessionStrKey(String ip, int port, int recvPort) { return ip + ":" + recvPort; //return ip + ":" + port + ":" + recvPort; } /// /// 启动会话过期检测线程 /// public static void StartSessionExpireTask() { sessionExpireTask = new IceServerSessionExpireTask(); sessionExpireTask.Start(); } /// /// 关闭会话过期检测线程 /// public static void EndSessionExpireTask() { sessionExpireTask.End(); } } }