/// <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():要发送的数据队列 丢弃了一段数据"); } }
/// <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); }
/// <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();//整体发送 } } }