IceUdpServer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using CommonLang.Log;
  2. using CommonLang.Protocol;
  3. using CommonNetwork_ICE.Util;
  4. using CommonServer.Protocol;
  5. using CommonServer.Server;
  6. using CommonServer_ICE.handler;
  7. using CommonServer_ICE.Msg;
  8. using CommonServer_ICE.Session;
  9. using Slice;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. namespace CommonServer_ICE.Server
  15. {
  16. /// <summary>
  17. /// ICE的UDP消息委托处理实现类
  18. /// </summary>
  19. public sealed class IceUdpServer : IceConnectServer
  20. {
  21. private static Logger log = LoggerFactory.GetLogger("IceUdpServer");
  22. public IceUdpServer(IPackageCodec codec)
  23. : base(codec)
  24. {
  25. }
  26. private SenderDisp_ GetServerSendHandler()
  27. {
  28. // 服务器消息委托处理类
  29. ServerMsgHandler msgHandlerDelegate = new ServerMsgHandlerUdpDelegate(this);
  30. return new ServerUdpSendHandler(msgHandlerDelegate);
  31. }
  32. /// <summary>
  33. /// 获取传输类型
  34. /// </summary>
  35. /// <returns></returns>
  36. internal override int getCommType()
  37. {
  38. return Constants.COMM_TYPE_UDP;
  39. }
  40. /// <summary>
  41. /// 返回会话总数量
  42. /// </summary>
  43. public override int SessionCount
  44. {
  45. get { return SessionManager.getAllSession().Count; }
  46. }
  47. /// <summary>
  48. /// 启动ICE服务器
  49. /// </summary>
  50. /// <returns></returns>
  51. internal override void StartIceServer()
  52. {
  53. SenderDisp_ serverSendHandler = GetServerSendHandler();
  54. // 初始化消息接收
  55. bool initServerSuccessed = serverConnector.InitRecvServer(Constants.COMM_TYPE_UDP, serverSendHandler);
  56. if (!initServerSuccessed)
  57. {
  58. throw new Exception("初始化服务器失败!");
  59. }
  60. init();
  61. log.Info("ICE UDP服务器启动侦听,IP【" + serverConnector.GetConnectorConfig().LocalIp + "】端口【" + serverConnector.GetConnectorConfig().LocalPort + "】。");
  62. // 等待关闭
  63. serverConnector.RecvConnector.waitForShutdown();
  64. }
  65. /// <summary>
  66. /// 服务器启动成功后执行各项初始化
  67. /// </summary>
  68. void init()
  69. {
  70. // 启动服务器端关键包消息发送队列
  71. SeverSendMsgManager.StartSendMsgQueue();
  72. // 启动服务器端会话检测任务
  73. SessionManager.StartSessionExpireTask();
  74. }
  75. //广播消息
  76. public override void Broadcast(IMessage message)
  77. {
  78. List<ISession> sessionList = SessionManager.getAllSession();
  79. foreach (ISession session in sessionList)
  80. {
  81. session.Send(message);
  82. }
  83. }
  84. /// <summary>
  85. /// 判断某个Session是否存在
  86. /// </summary>
  87. /// <param name="session"></param>
  88. /// <returns></returns>
  89. public override bool HasSession(ISession session)
  90. {
  91. return SessionManager.hasSession(session);
  92. }
  93. /// <summary>
  94. /// 获取单个Session
  95. /// </summary>
  96. /// <param name="sessionID"></param>
  97. /// <returns></returns>
  98. public override ISession GetSessionByID(string sessionID)
  99. {
  100. return SessionManager.getSession(sessionID);
  101. }
  102. /// <summary>
  103. /// 获取所有Session
  104. /// </summary>
  105. /// <returns></returns>
  106. public override IEnumerable<ISession> GetSessions()
  107. {
  108. return SessionManager.getAllSession();
  109. }
  110. /// <summary>
  111. /// 释放服务器资源
  112. /// </summary>
  113. public override void Dispose()
  114. {
  115. log.Info("关闭服务器,释放各种资源");
  116. // 关闭所有Session消息接收通道
  117. List<ISession> sessionList = SessionManager.getAllSession();
  118. foreach (ISession session in sessionList)
  119. {
  120. session.Disconnect(true);
  121. }
  122. SeverSendMsgManager.EndSendMsgQueue();
  123. SessionManager.EndSessionExpireTask();
  124. if (serverConnector != null)
  125. {
  126. serverConnector.Destroy();
  127. }
  128. this.serverListener.OnDestory();
  129. }
  130. }
  131. }