feat: initial commit - HJHA game server full source
6 major server modules (PdkFriendServer/GlobalSever/ServerCore/GameModule/GameNetModule) + game logic (GameFix/GameDAL/ServerData) + network layer (NetWorkMessage) + data layer (ObjectModel) + utilities (MrWu/Core/Config/CloudAPI/dll) + adapters (zyxAdapter/base) .NET 8.0 C# solution, 16 projects, 958 source files
This commit is contained in:
68
GameNetModule/Manager/GameSessionManager.cs
Normal file
68
GameNetModule/Manager/GameSessionManager.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using System.Collections.Concurrent;
|
||||
using MrWu.Debug;
|
||||
using Server.Core;
|
||||
using Server.Net;
|
||||
|
||||
namespace Server
|
||||
{
|
||||
public class GameSessionManager : SingleInstance<GameSessionManager>
|
||||
{
|
||||
private readonly ConcurrentDictionary<int, IUserSession> _sessions = new ConcurrentDictionary<int, IUserSession>();
|
||||
|
||||
/// <summary>
|
||||
/// 如果已经存在,则不能添加
|
||||
/// </summary>
|
||||
/// <param name="userid"></param>
|
||||
/// <param name="session"></param>
|
||||
/// <returns></returns>
|
||||
public bool AddPlayerSession(int userid, IUserSession session)
|
||||
{
|
||||
Debug.Info($"GameSessionManager 添加玩家:{userid}");
|
||||
return _sessions.TryAdd(userid, session);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 如果Session 不是同一个不能移除
|
||||
/// </summary>
|
||||
/// <param name="userid"></param>
|
||||
/// <param name="session"></param>
|
||||
/// <returns></returns>
|
||||
public bool RemovePlayerSession(int userid, IUserSession session)
|
||||
{
|
||||
Debug.Log($"移除玩家Session:{userid}");
|
||||
// 尝试获取 userid 对应的 Session
|
||||
if (_sessions.TryGetValue(userid, out IUserSession existingSession))
|
||||
{
|
||||
Debug.Log($"找到玩家的Session! {existingSession.GetType()} {session.GetType()}");
|
||||
// 检查获取到的 Session 是否与传入的 session 相同
|
||||
if (existingSession == session)
|
||||
{
|
||||
// 如果相同,则尝试移除
|
||||
Debug.Info($"GameSessionManager 移除玩家:{userid}");
|
||||
return _sessions.TryRemove(userid, out _);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果 Session 不相同或 userid 不存在,则返回 false
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找Session
|
||||
/// </summary>
|
||||
/// <param name="userid"></param>
|
||||
/// <returns></returns>
|
||||
public IUserSession FindSession(int userid)
|
||||
{
|
||||
// 尝试获取 userid 对应的 Session
|
||||
if (_sessions.TryGetValue(userid, out IUserSession session))
|
||||
{
|
||||
// 如果找到,返回该 Session
|
||||
return session;
|
||||
}
|
||||
|
||||
// 如果未找到,返回 null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user