123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- 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
- {
-
-
-
- internal class SessionManager
- {
- private static Logger log = LoggerFactory.GetLogger("SessionManager");
- private static Dictionary<string, ISession> sessionMap = new Dictionary<string, ISession>();
- private static Dictionary<string, ISession> sessionMapByID = new Dictionary<string, ISession>();
- private static IceServerSessionExpireTask sessionExpireTask;
-
-
-
-
-
- public static ISession getSession(string sessionId)
- {
- ISession ret;
- if (sessionMapByID.TryGetValue(sessionId, out ret ))
- {
- return ret;
- }
- return null;
- }
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- 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<ISession> getAllSession()
- {
- return sessionMap.Values.ToList();
- }
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- private static string getSessionStrKey(String ip, int port, int recvPort)
- {
- return ip + ":" + recvPort;
-
- }
-
-
-
- public static void StartSessionExpireTask()
- {
- sessionExpireTask = new IceServerSessionExpireTask();
- sessionExpireTask.Start();
- }
-
-
-
- public static void EndSessionExpireTask()
- {
- sessionExpireTask.End();
- }
- }
- }
|