public void Dispose() { SetGameState(FSPGameState.None); for (int i = 0; i < m_ListPlayer.Count; i++) { FSPPlayer player = m_ListPlayer[i]; FSPServer.Instance.DelSession(player.Sid); player.Dispose(); } m_ListPlayer.Clear(); m_ListObjectsForDelayGC.Clear(); GC.Collect(); onGameExit = null; onGameEnd = null; Debuger.Log(LOG_TAG, "Dispose()"); }
//--------------------------------------------------------- public bool AddPlayer(uint playerId, uint sid) { Debuger.Log(LOG_TAG, "AddPlayer() playerId:{0}, sid:{1}", playerId, sid); if (m_State != FSPGameState.Create) { Debuger.LogError(LOG_TAG, "AddPlayer() 当前状态下无法AddPlayer! State = {0}", m_State); return(false); } FSPPlayer player = null; for (int i = 0; i < m_ListPlayer.Count; i++) { player = m_ListPlayer[i]; if (player.Id == playerId) { Debuger.LogWarning(LOG_TAG, "AddPlayer() PlayerId已经存在!用新的替代旧的! PlayerId = " + playerId); m_ListPlayer.RemoveAt(i); FSPServer.Instance.DelSession(player.Sid); player.Dispose(); break; } } if (m_ListPlayer.Count >= MaxPlayerNum) { Debuger.LogError(LOG_TAG, "AddPlayer() 已经达到最大玩家数了! MaxPlayerNum = {0}", MaxPlayerNum); return(false); } FSPSession session = FSPServer.Instance.AddSession(sid); player = new FSPPlayer(playerId, m_FSPParam.serverTimeout, session, OnPlayerReceive); m_ListPlayer.Add(player); return(true); }
//--------------------------------------------------------- public bool AddPlayer(uint playerId, uint sid) { MyLogger.Log(LOG_TAG, "AddPlayer() playerId:{0}, sid:{1}", playerId.ToString(), sid); if (mState != FSPGameState.Create) { MyLogger.LogError(LOG_TAG, "AddPlayer() cannot create player in current state! State = {0}", mState.ToString()); return(false); } FSPPlayer player = null; for (int i = 0; i < mPlayerList.Count; i++) { player = mPlayerList[i]; if (player.Id == playerId) { MyLogger.LogWarning(LOG_TAG, "AddPlayer()", " PlayerId used!replace with new id! PlayerId = " + playerId); mPlayerList.RemoveAt(i); FSPServer.Instance.DelSession(player.Sid); player.Dispose(); break; } } if (mPlayerList.Count >= MaxPlayerNum) { MyLogger.LogError(LOG_TAG, "AddPlayer() maximum player reached! MaxPlayerNum = {0}", MaxPlayerNum.ToString()); return(false); } FSPSession session = FSPServer.Instance.AddSession(sid); player = new FSPPlayer(playerId, mFSPParam.serverTimeout, session, OnPlayerReceive); mPlayerList.Add(player); return(true); }
//--------------------------------------------------------- /// <summary> /// enter frame /// </summary> public void EnterFrame() { //delete exited players for (int i = 0; i < mPlayersExitOnNextFrameList.Count; i++) { FSPPlayer player = mPlayersExitOnNextFrameList[i]; FSPServer.Instance.DelSession(player.Sid); player.Dispose(); } mPlayersExitOnNextFrameList.Clear(); //hand game state HandleGameState(); //check if game state changes if (mState == FSPGameState.None) { return; } if (mLockedFrame.frameId != 0 || !mLockedFrame.IsEmpty()) { //send frame to players for (int i = 0; i < mPlayerList.Count; i++) { FSPPlayer player = mPlayerList[i]; player.SendToClient(mLockedFrame); if (player.WaitForExit) { mPlayersExitOnNextFrameList.Add(player); mPlayerList.RemoveAt(i); --i; } } } //clear frame 0 in each iteration if (mLockedFrame.frameId == 0) { mLockedFrame = new FSPFrame(); //delay GC if (UseDelayGC) { mListObjectsForDelayGC.Add(mLockedFrame); } } //add up frame id if (mState == FSPGameState.RoundBegin || mState == FSPGameState.ControlStart) { mCurFrameId++; mLockedFrame = new FSPFrame(); mLockedFrame.frameId = mCurFrameId; //防止GC if (UseDelayGC) { mListObjectsForDelayGC.Add(mLockedFrame); } } }