public FSPSession CreateSession() { Debuger.Log(); uint sid = NewSessionID(); FSPSession session = new FSPSession(sid, HandleSessionSend); m_mapSession.Add(sid, session); return(session); }
public FSPSession GetSession(uint sid) { FSPSession session = null; lock (m_mapSession) { session = m_mapSession[sid]; } return(session); }
public void Release() { Debuger.Log(); if (m_session != null) { m_session.SetReceiveListener(null); m_session.Active(false); m_session = null; } }
//--------------------------------------------------------- public FSPPlayer(uint playerId, int timeout, FSPSession session, Action <FSPPlayer, FSPVKey> listener) { m_id = playerId; m_Timeout = timeout; m_Session = session; m_Session.SetReceiveListener(OnSessionReceive); m_RecvListener = listener; WaitForExit = false; m_FrameCache = new Queue <FSPFrame>(); }
//================================================================= public void Create(uint id, int authId, FSPSession session, Action <FSPPlayer, FSPMessage> listener) { Debuger.Log("id:{0}, authId:{1}, sid:{2}", id, authId, session.id); m_id = id; m_authId = authId; m_RecvListener = listener; m_session = session; m_session.SetReceiveListener(OnRecvFromSession); m_FrameCache = new Queue <FSPFrame>(); }
//--------------------------------------------------------- #region Session管理 private FSPSession GetSession(uint sid) { FSPSession s = null; lock (m_ListSession) { for (int i = 0; i < m_ListSession.Count; i++) { if (m_ListSession[i].Id == sid) { return(m_ListSession[i]); } } } return(null); }
//------------------------------------------------------------ private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint) { FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(buffer); FSPSession session = GetSession(data.sid); if (session == null) { Debuger.LogWarning(LOG_TAG_RECV, "DoReceive() 收到一个未知的SID = " + data.sid); //没有这个玩家,不理它的数据 return; } this.Log("DoReceive() Receive Buffer, SID={0}, IP={1}, Size={2}", session.Id, remotePoint, buffer.Length); session.EndPoint = remotePoint; session.Receive(data); }
//------------------------------------------------------------ private void OnReceive(byte[] buffer, int size, IPEndPoint remotePoint) { FSPDataC2S data = PBSerializer.NDeserialize <FSPDataC2S>(buffer); FSPSession session = GetSession(data.sid); if (session == null) { MyLogger.LogWarning(LOG_TAG_RECV, "DoReceive()", "unknown SID = " + data.sid); //player does not exist,reply nothing return; } this.Log("DoReceive() Receive Buffer, SID={0}, IP={1}, Size={2}", session.Id, remotePoint, buffer.Length); session.EndPoint = remotePoint; session.Receive(data); }
internal FSPSession AddSession(uint sid) { FSPSession s = GetSession(sid); if (s != null) { Debuger.LogWarning(LOG_TAG_MAIN, "AddSession() SID已经存在 = " + sid); return(s); } Debuger.Log(LOG_TAG_MAIN, "AddSession() SID = " + sid); s = new FSPSession(sid, m_GameSocket); lock (m_ListSession) { m_ListSession.Add(s); } return(s); }
internal FSPSession AddSession(uint sid) { FSPSession s = GetSession(sid); if (s != null) { MyLogger.LogWarning(LOG_TAG_MAIN, "AddSession()", " SID used = " + sid); return(s); } MyLogger.Log(LOG_TAG_MAIN, "AddSession() SID = " + sid); s = new FSPSession(sid, mGameSocket); lock (m_ListSession) { m_ListSession.Add(s); } return(s); }
//--------------------------------------------------------- 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); }
//=================================================================== public FSPPlayer AddPlayer(uint playerId, FSPSession session) { Debuger.Log("playerId:{0}", playerId); if (m_State != FSPGameState.Create) { Debuger.LogError("当前状态下无法AddPlayer! State = {0}", m_State); return(null); } FSPPlayer player = null; for (int i = 0; i < m_ListPlayer.Count; i++) { player = m_ListPlayer[i]; if (player.id == playerId) { Debuger.LogWarning("PlayerId已经存在!用新的替代旧的! PlayerId = " + playerId); m_ListPlayer.RemoveAt(i); player.Release(); break; } } if (m_ListPlayer.Count >= MaxPlayerNum) { Debuger.LogError("已经达到最大玩家数了! MaxPlayerNum = {0}", MaxPlayerNum); return(null); } player = new FSPPlayer(); player.Create(playerId, m_authId, session, OnRecvFromPlayer); m_ListPlayer.Add(player); return(player); }
private void DoReceiveInThread() { EndPoint remotePoint = IPUtils.GetIPEndPointAny(AddressFamily.InterNetwork, 0); int cnt = m_SystemSocket.ReceiveFrom(m_RecvBufferTemp, m_RecvBufferTemp.Length, SocketFlags.None, ref remotePoint); if (cnt > 0) { m_RecvBufferTempReader.Attach(m_RecvBufferTemp, cnt); byte[] m_32b = new byte[4]; m_RecvBufferTempReader.ReadBytes(m_32b, 0, 4); uint sid = BitConverter.ToUInt32(m_32b, 0); lock (m_mapSession) { FSPSession session = null; if (sid == 0) { Debuger.LogError("基于KCP的Sid为0,该包需要被丢掉"); } else { session = m_mapSession[sid]; } if (session != null) { session.Active(remotePoint as IPEndPoint); session.DoReceiveInGateway(m_RecvBufferTemp, cnt); } else { Debuger.LogWarning("无效的包! sid:{0}", sid); } } } }
public void Dispose() { m_Session = null; m_FrameCache.Clear(); }