123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- using CommonLang.Log;
- using CommonLang.Protocol;
- using CommonNetwork_ICE.Util;
- using CommonServer.Protocol;
- using CommonServer.Server;
- using CommonServer_ICE.handler;
- using CommonServer_ICE.Msg;
- using CommonServer_ICE.Session;
- using Slice;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace CommonServer_ICE.Server
- {
- /// <summary>
- /// ICE的UDP消息委托处理实现类
- /// </summary>
- public sealed class IceUdpServer : IceConnectServer
- {
- private static Logger log = LoggerFactory.GetLogger("IceUdpServer");
- public IceUdpServer(IPackageCodec codec)
- : base(codec)
- {
- }
- private SenderDisp_ GetServerSendHandler()
- {
- // 服务器消息委托处理类
- ServerMsgHandler msgHandlerDelegate = new ServerMsgHandlerUdpDelegate(this);
- return new ServerUdpSendHandler(msgHandlerDelegate);
- }
- /// <summary>
- /// 获取传输类型
- /// </summary>
- /// <returns></returns>
- internal override int getCommType()
- {
- return Constants.COMM_TYPE_UDP;
- }
- /// <summary>
- /// 返回会话总数量
- /// </summary>
- public override int SessionCount
- {
- get { return SessionManager.getAllSession().Count; }
- }
- /// <summary>
- /// 启动ICE服务器
- /// </summary>
- /// <returns></returns>
- internal override void StartIceServer()
- {
- SenderDisp_ serverSendHandler = GetServerSendHandler();
- // 初始化消息接收
- bool initServerSuccessed = serverConnector.InitRecvServer(Constants.COMM_TYPE_UDP, serverSendHandler);
- if (!initServerSuccessed)
- {
- throw new Exception("初始化服务器失败!");
- }
- init();
- log.Info("ICE UDP服务器启动侦听,IP【" + serverConnector.GetConnectorConfig().LocalIp + "】端口【" + serverConnector.GetConnectorConfig().LocalPort + "】。");
- // 等待关闭
- serverConnector.RecvConnector.waitForShutdown();
- }
- /// <summary>
- /// 服务器启动成功后执行各项初始化
- /// </summary>
- void init()
- {
- // 启动服务器端关键包消息发送队列
- SeverSendMsgManager.StartSendMsgQueue();
- // 启动服务器端会话检测任务
- SessionManager.StartSessionExpireTask();
- }
- //广播消息
- public override void Broadcast(IMessage message)
- {
- List<ISession> sessionList = SessionManager.getAllSession();
- foreach (ISession session in sessionList)
- {
- session.Send(message);
- }
- }
- /// <summary>
- /// 判断某个Session是否存在
- /// </summary>
- /// <param name="session"></param>
- /// <returns></returns>
- public override bool HasSession(ISession session)
- {
- return SessionManager.hasSession(session);
- }
- /// <summary>
- /// 获取单个Session
- /// </summary>
- /// <param name="sessionID"></param>
- /// <returns></returns>
- public override ISession GetSessionByID(string sessionID)
- {
- return SessionManager.getSession(sessionID);
- }
- /// <summary>
- /// 获取所有Session
- /// </summary>
- /// <returns></returns>
- public override IEnumerable<ISession> GetSessions()
- {
- return SessionManager.getAllSession();
- }
- /// <summary>
- /// 释放服务器资源
- /// </summary>
- public override void Dispose()
- {
- log.Info("关闭服务器,释放各种资源");
- // 关闭所有Session消息接收通道
- List<ISession> sessionList = SessionManager.getAllSession();
- foreach (ISession session in sessionList)
- {
- session.Disconnect(true);
- }
- SeverSendMsgManager.EndSendMsgQueue();
- SessionManager.EndSessionExpireTask();
- if (serverConnector != null)
- {
- serverConnector.Destroy();
- }
- this.serverListener.OnDestory();
- }
- }
- }
|