TcpSessionManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using CommonLang.Log;
  2. using CommonServer.Server;
  3. using CommonServer_ICE.Server;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. namespace CommonServer_ICE.Session
  9. {
  10. /// <summary>
  11. /// TCP通讯方式会话管理器
  12. /// </summary>
  13. internal class TcpSessionManager : Glacier2.SessionManagerDisp_
  14. {
  15. private static Logger log = LoggerFactory.GetLogger("TcpSessionManager");
  16. // 所有会话保存对象
  17. private Dictionary<string, ISession> sessionIdKeyMap = new Dictionary<string, ISession>();
  18. // TCP服务器对象
  19. private IceTcpServer tcpServer;
  20. public TcpSessionManager(IceTcpServer tcpServer)
  21. {
  22. this.tcpServer = tcpServer;
  23. tcpServer.TcpSessionManager = this;
  24. }
  25. /// <summary>
  26. /// 创建会话
  27. /// </summary>
  28. /// <param name="userId">用户名(忽略不用)</param>
  29. /// <param name="control">会话控制器</param>
  30. /// <param name="current">上下文</param>
  31. /// <returns>会话</returns>
  32. public override Glacier2.SessionPrx create(string userId, Glacier2.SessionControlPrx control, Ice.Current current)
  33. {
  34. Ice.TCPConnectionInfo tcpConInfo = (Ice.TCPConnectionInfo)current.con.getInfo();
  35. IceServerIoSession session = new IceServerIoSession(tcpServer);
  36. session.TcpSessionManager = this;
  37. session.RemoteIp = tcpConInfo.remoteAddress;
  38. session.ClientSentDataPort = tcpConInfo.remotePort;
  39. sessionIdKeyMap.Add(session.ID, session);
  40. // 通知新会话建立
  41. try
  42. {
  43. tcpServer.OnNewSessionConnected(session);
  44. }
  45. catch (Exception e)
  46. {
  47. log.Error("建立新会话发生异常:" + e.Message);
  48. }
  49. Ice.ObjectPrx objectPrx = current.adapter.addWithUUID(session);
  50. session.ClientIdentity = objectPrx.ice_getIdentity();
  51. return Glacier2.SessionPrxHelper.uncheckedCast(current.adapter.addWithUUID(session));
  52. }
  53. /// <summary>
  54. /// 清除会话
  55. /// </summary>
  56. /// <param name="session">会话</param>
  57. public void RemoveSession(IceServerIoSession session)
  58. {
  59. sessionIdKeyMap.Remove(session.ID);
  60. }
  61. /// <summary>
  62. /// 获取会话键
  63. /// </summary>
  64. /// <param name="ip">IP</param>
  65. /// <param name="port">端口</param>
  66. /// <returns>会话键</returns>
  67. private string getSessionKey(string ip, int port)
  68. {
  69. return ip + ":" + port;
  70. }
  71. /// <summary>
  72. /// 获取会话数量
  73. /// </summary>
  74. /// <returns></returns>
  75. public int getSessionCount()
  76. {
  77. return sessionIdKeyMap.Count;
  78. }
  79. /// <summary>
  80. /// 获取所有会话
  81. /// </summary>
  82. /// <returns>会话一览</returns>
  83. public List<ISession> getAllSession()
  84. {
  85. return sessionIdKeyMap.Values.ToList();
  86. }
  87. /// <summary>
  88. /// 判断序列号为键的会话是否存在
  89. /// </summary>
  90. /// <param name="sessionId">序列号键</param>
  91. /// <returns></returns>
  92. public ISession getSession(string sessionId)
  93. {
  94. ISession ret;
  95. if (sessionIdKeyMap.TryGetValue(sessionId, out ret))
  96. {
  97. return ret;
  98. }
  99. return null;
  100. }
  101. /// <summary>
  102. /// 根据IP地址和端口判断是否存在某个会话
  103. /// </summary>
  104. /// <param name="ip"></param>
  105. /// <param name="port"></param>
  106. /// <returns></returns>
  107. public ISession getSession(String ip, int port)
  108. {
  109. return null;
  110. }
  111. /// <summary>
  112. /// 判断指定序列号的会话是否存在
  113. /// </summary>
  114. /// <param name="sessionId"></param>
  115. /// <returns></returns>
  116. public bool hasSession(string sessionId)
  117. {
  118. if (sessionIdKeyMap.ContainsKey(sessionId))
  119. {
  120. return true;
  121. }
  122. return false;
  123. }
  124. /// <summary>
  125. /// 判断传入会话对象是否存在
  126. /// </summary>
  127. /// <param name="session"></param>
  128. /// <returns></returns>
  129. public bool hasSession(ISession session)
  130. {
  131. return hasSession(session.ID);
  132. }
  133. /// <summary>
  134. /// IP地址和端口为键获取会话
  135. /// </summary>
  136. /// <param name="ip"></param>
  137. /// <param name="port"></param>
  138. /// <returns></returns>
  139. public bool hasSession(String ip, int port)
  140. {
  141. return false;
  142. }
  143. }
  144. }