using CommonLang.Log; using CommonServer.Server; using CommonServer_ICE.Server; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CommonServer_ICE.Session { /// /// TCP通讯方式会话管理器 /// internal class TcpSessionManager : Glacier2.SessionManagerDisp_ { private static Logger log = LoggerFactory.GetLogger("TcpSessionManager"); // 所有会话保存对象 private Dictionary sessionIdKeyMap = new Dictionary(); // TCP服务器对象 private IceTcpServer tcpServer; public TcpSessionManager(IceTcpServer tcpServer) { this.tcpServer = tcpServer; tcpServer.TcpSessionManager = this; } /// /// 创建会话 /// /// 用户名(忽略不用) /// 会话控制器 /// 上下文 /// 会话 public override Glacier2.SessionPrx create(string userId, Glacier2.SessionControlPrx control, Ice.Current current) { Ice.TCPConnectionInfo tcpConInfo = (Ice.TCPConnectionInfo)current.con.getInfo(); IceServerIoSession session = new IceServerIoSession(tcpServer); session.TcpSessionManager = this; session.RemoteIp = tcpConInfo.remoteAddress; session.ClientSentDataPort = tcpConInfo.remotePort; sessionIdKeyMap.Add(session.ID, session); // 通知新会话建立 try { tcpServer.OnNewSessionConnected(session); } catch (Exception e) { log.Error("建立新会话发生异常:" + e.Message); } Ice.ObjectPrx objectPrx = current.adapter.addWithUUID(session); session.ClientIdentity = objectPrx.ice_getIdentity(); return Glacier2.SessionPrxHelper.uncheckedCast(current.adapter.addWithUUID(session)); } /// /// 清除会话 /// /// 会话 public void RemoveSession(IceServerIoSession session) { sessionIdKeyMap.Remove(session.ID); } /// /// 获取会话键 /// /// IP /// 端口 /// 会话键 private string getSessionKey(string ip, int port) { return ip + ":" + port; } /// /// 获取会话数量 /// /// public int getSessionCount() { return sessionIdKeyMap.Count; } /// /// 获取所有会话 /// /// 会话一览 public List getAllSession() { return sessionIdKeyMap.Values.ToList(); } /// /// 判断序列号为键的会话是否存在 /// /// 序列号键 /// public ISession getSession(string sessionId) { ISession ret; if (sessionIdKeyMap.TryGetValue(sessionId, out ret)) { return ret; } return null; } /// /// 根据IP地址和端口判断是否存在某个会话 /// /// /// /// public ISession getSession(String ip, int port) { return null; } /// /// 判断指定序列号的会话是否存在 /// /// /// public bool hasSession(string sessionId) { if (sessionIdKeyMap.ContainsKey(sessionId)) { return true; } return false; } /// /// 判断传入会话对象是否存在 /// /// /// public bool hasSession(ISession session) { return hasSession(session.ID); } /// /// IP地址和端口为键获取会话 /// /// /// /// public bool hasSession(String ip, int port) { return false; } } }