コード例 #1
0
        /// <summary>
        /// 构造函数:创建一个未初始化的服务器实例。
        /// 来开始一个监听服务,
        /// 调用Init方法之后再Start方法
        /// </summary>
        /// <param name="server">它所关联的DNServer</param>
        /// <param name="bufferSize">给每个连接的IO操作的buffer大小</param>
        internal SocketListener(DNServer server, int bufferSize)
        {
            this._dnserver   = server;
            this._bufferSize = bufferSize;

            //这里可能无法限制最大连接数,需要在用户连接的回调中处理最大连接数
        }
コード例 #2
0
ファイル: DNServer.cs プロジェクト: KOLLSOFT/DNET
 /// <summary>
 /// 获得实例
 /// </summary>
 /// <returns></returns>
 public static DNServer GetInst()
 {
     if (_instance == null)
     {
         _instance = new DNServer();
     }
     return(_instance);
 }
コード例 #3
0
ファイル: Token.cs プロジェクト: KOLLSOFT/DNET
        /// <summary>
        /// 添加一条要发送的消息(未打包的数据),不会自动发送。
        /// 这一个方法会进行数据的预打包
        /// </summary>
        /// <param name="data">要发送的数据</param>
        /// <param name="index">数据起始</param>
        /// <param name="length">数据长度</param>
        public void AddSendData(byte[] data, int index, int length)
        {
            IPacket packet = DNServer.GetInstance().Packet;

            //进行预打包然后加入到队列
            if (!_sendQueue.EnqueueMaxLimit(packet.PrePack(data, index, length)))
            {
                DxDebug.LogWarning("Token.AddSendData():要发送的数据队列 丢弃了一段数据");
            }
        }
コード例 #4
0
 /// <summary>
 /// 服务器和客户端一起设置一个缓存文件夹
 /// </summary>
 /// <param name="path">缓存文件夹路径</param>
 public static bool SetCacheDir(string path)
 {
     if (!DNClient.GetInstance().SetDirCache(path))
     {
         return(false);
     }
     if (!DNServer.GetInstance().SetDirCache(path))
     {
         return(false);
     }
     return(true);
 }
コード例 #5
0
        /// <summary>
        /// 检查用户是否其实已经离线,在OnTimerTick函数里调用
        /// </summary>
        private void CheckOffLineAndSend()
        {
            _countHeartBeatCheckTime += KICK_TIME;
            if (_countHeartBeatCheckTime >= Config.HeartBeatCheckTime) //15秒*1
            {
                Interlocked.Exchange(ref _countHeartBeatCheckTime, 0); //这里要立马置零,防止后面的代码执行的过久,再次进入kick

                Token[] tokens = TokenManager.GetInstance().GetAllToken();
                if (tokens != null)
                {
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        Token token = tokens[i];
                        if (token.LastMsgReceTickTime < _checkTickTime) //如果从上次的进入这里的时间之后一直都没有收到消息
                        {
                            DxDebug.LogConsole("ServerTimer.CheckOffLine():一个用户长时间没有收到心跳包,被删除!");

                            TokenManager.GetInstance().DeleteToken(token.ID, TokenErrorType.HeartBeatTimeout);//删除这个用户
                        }
                    }
                }

                _checkTickTime = DateTime.Now.Ticks;
            }

            _countHeartBeatSendTime += KICK_TIME;
            if (_countHeartBeatSendTime >= Config.HeartBeatSendTime)                      //5秒进一次
            {
                Interlocked.Exchange(ref _countHeartBeatSendTime, 0);                     //这里要立马置零,防止后面的代码执行的过久,再次进入kick

                long subTimeTick = DateTime.Now.Ticks - 10000 * Config.HeartBeatSendTime; //计算得到的门限时间

                Token[] tokens = TokenManager.GetInstance().GetAllToken();
                if (tokens != null)
                {
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        Token token = tokens[i];
                        if (token.disposed == false && token.LastMsgSendTickTime < subTimeTick) //如果从上次的进入这里的时间之后一直都没有发消息
                        {
                            //应该发送一条心跳包
                            token.AddSendData(Config.HeartBeatData, 0, Config.HeartBeatData.Length);
                        }
                    }
                    DNServer.GetInstance().SendAll();//整体发送
                }
            }
        }
コード例 #6
0
ファイル: ServerStatus.cs プロジェクト: KOLLSOFT/DNET
 /// <summary>
 /// 构造函数要记录它的归属服务器
 /// </summary>
 /// <param name="server"></param>
 internal ServerStatus(DNServer server)
 {
     _dnServer = server;
 }