`
This commit is contained in:
parent
de844d7274
commit
d0ff0dbfda
@ -13,7 +13,6 @@ namespace XNet.Business
|
||||
private const int TARGET_FPS = 30;
|
||||
private const int FRAME_TIME_MS = 1000 / TARGET_FPS;
|
||||
private readonly List<string> _instanceIds = new List<string>();
|
||||
|
||||
public GameLoopService(NavMeshManager navMeshManager, SceneAgent sceneAgent, WsConnectionManager wsManager)
|
||||
{
|
||||
_navMeshManager = navMeshManager;
|
||||
|
||||
@ -15,10 +15,10 @@ namespace XNet.Business
|
||||
{
|
||||
public string InstanceId { get; }
|
||||
public DtCrowd Crowd { get; set; }
|
||||
public object SyncRoot { get; } = new object(); // 线程锁
|
||||
//public object SyncRoot { get; } = new object(); // 线程锁
|
||||
|
||||
// 新增:存储该实例下所有 Agent 的索引(关键修复)
|
||||
public List<int> AgentIndices { get; } = new List<int>();
|
||||
public ConcurrentDictionary<int, bool> AgentIndices { get; } = new ConcurrentDictionary<int, bool>();
|
||||
|
||||
// 构造函数
|
||||
public CrowdInstance(string instanceId, DtCrowd crowd)
|
||||
@ -30,22 +30,22 @@ namespace XNet.Business
|
||||
// 辅助:添加 Agent 时自动记录索引
|
||||
public void AddAgentIndex(int agentIdx)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
if (!AgentIndices.Contains(agentIdx))
|
||||
{
|
||||
AgentIndices.Add(agentIdx);
|
||||
}
|
||||
}
|
||||
//lock (SyncRoot)
|
||||
//{
|
||||
//if (!AgentIndices.ContainsKey(agentIdx))
|
||||
//{
|
||||
AgentIndices.TryAdd(agentIdx, true);
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
// 辅助:移除 Agent 时清理索引
|
||||
public void RemoveAgentIndex(int agentIdx)
|
||||
{
|
||||
lock (SyncRoot)
|
||||
{
|
||||
AgentIndices.Remove(agentIdx);
|
||||
}
|
||||
//lock (SyncRoot)
|
||||
//{
|
||||
AgentIndices.TryRemove(agentIdx, out _);
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,11 +84,12 @@ namespace XNet.Business
|
||||
return syncList;
|
||||
}
|
||||
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
// 遍历实例内所有Agent索引(修复AgentIndices报错)
|
||||
foreach (var agentIdx in ci.AgentIndices)
|
||||
foreach (var kv in ci.AgentIndices)
|
||||
{
|
||||
int agentIdx = kv.Key;
|
||||
var agent = ci.Crowd.GetAgent(agentIdx);
|
||||
if (agent == null || agent.state == DtCrowdAgentState.DT_CROWDAGENT_STATE_INVALID)
|
||||
{
|
||||
@ -133,14 +134,14 @@ namespace XNet.Business
|
||||
lastStates.TryAdd(agentIdx, new AgentState { Position = currPos, Rotation = currRot });
|
||||
}
|
||||
}
|
||||
}
|
||||
//}
|
||||
|
||||
return syncList;
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// ===============================================
|
||||
// 修复2:重写 GetAgentRotation(通过速度计算朝向)
|
||||
// ==========================================
|
||||
// ===============================================
|
||||
public float GetAgentRotation(string instanceId, int agentIdx)
|
||||
{
|
||||
if (!_crowdInstances.TryGetValue(instanceId, out var ci))
|
||||
@ -148,8 +149,8 @@ namespace XNet.Business
|
||||
return 0f;
|
||||
}
|
||||
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
var agent = ci.Crowd.GetAgent(agentIdx);
|
||||
if (agent == null || agent.state == DtCrowdAgentState.DT_CROWDAGENT_STATE_INVALID)
|
||||
{
|
||||
@ -169,7 +170,7 @@ namespace XNet.Business
|
||||
}
|
||||
|
||||
return rotation;
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
|
||||
@ -214,8 +215,8 @@ namespace XNet.Business
|
||||
throw new ArgumentException($"实例 {instanceId} 不存在");
|
||||
}
|
||||
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
// 创建Agent(原有逻辑)
|
||||
var agentParams = new DtCrowdAgentParams();
|
||||
agentParams.radius = radius;
|
||||
@ -237,22 +238,22 @@ namespace XNet.Business
|
||||
.TryAdd(agentIdx, new AgentState { Position = position, Rotation = 0f });
|
||||
|
||||
return agentIdx;
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
public void RemoveAgent(string instanceId, int agentIdx)
|
||||
{
|
||||
if (!_crowdInstances.TryGetValue(instanceId, out var ci)) return;
|
||||
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
var agent = ci.Crowd.GetAgent(agentIdx);
|
||||
// 【修复】判断 Agent 是否有效,需要检查 state
|
||||
if (agent != null && agent.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_INVALID)
|
||||
{
|
||||
ci.Crowd.RemoveAgent(agent);
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
public bool AgentGoto(string instanceId, int agentIdx, Vector3 destination)
|
||||
@ -267,14 +268,14 @@ namespace XNet.Business
|
||||
|
||||
if (targetRef == 0) return false;
|
||||
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
var agent = ci.Crowd.GetAgent(agentIdx);
|
||||
if (agent != null && agent.state != DtCrowdAgentState.DT_CROWDAGENT_STATE_INVALID)
|
||||
{
|
||||
return ci.Crowd.RequestMoveTarget(agent, targetRef, realTargetPos);
|
||||
}
|
||||
}
|
||||
//}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -288,8 +289,8 @@ namespace XNet.Business
|
||||
return Vector3.Zero;
|
||||
}
|
||||
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
var agent = ci.Crowd.GetAgent(agentIdx);
|
||||
if (agent == null || agent.state == DtCrowdAgentState.DT_CROWDAGENT_STATE_INVALID)
|
||||
{
|
||||
@ -298,7 +299,7 @@ namespace XNet.Business
|
||||
|
||||
// 坐标转换(和AddAgent时一致:Z轴取反)
|
||||
return new Vector3(agent.npos.X, agent.npos.Y, -agent.npos.Z);
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
public void UpdateAll(float deltaTime)
|
||||
@ -308,10 +309,10 @@ namespace XNet.Business
|
||||
MaxDegreeOfParallelism = Environment.ProcessorCount // 限制并行数=CPU核心数,避免过载
|
||||
}, ci =>
|
||||
{
|
||||
lock (ci.SyncRoot)
|
||||
{
|
||||
//lock (ci.SyncRoot)
|
||||
//{
|
||||
ci.Crowd.Update(deltaTime, null);
|
||||
}
|
||||
//}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ namespace XNet.Business
|
||||
{
|
||||
// ========== 原有核心字段 ==========
|
||||
private readonly ConcurrentDictionary<string, WebSocket> _connections = new();
|
||||
private readonly ConcurrentDictionary<string, HashSet<string>> _instanceSubscribers = new();
|
||||
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, bool>> _instanceSubscribers = new();
|
||||
private readonly JsonSerializerOptions _jsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
|
||||
|
||||
// ========== 新增:对象池配置 ==========
|
||||
@ -48,10 +48,7 @@ namespace XNet.Business
|
||||
{
|
||||
foreach (var instanceId in _instanceSubscribers.Keys)
|
||||
{
|
||||
lock (_instanceSubscribers[instanceId])
|
||||
{
|
||||
_instanceSubscribers[instanceId].Remove(connId);
|
||||
}
|
||||
_instanceSubscribers[instanceId].TryRemove(connId, out _);
|
||||
}
|
||||
Console.WriteLine($"[WS .NET 10] 连接断开:{connId},当前连接数:{_connections.Count}");
|
||||
}
|
||||
@ -65,17 +62,15 @@ namespace XNet.Business
|
||||
return false;
|
||||
}
|
||||
|
||||
_instanceSubscribers.GetOrAdd(instanceId, _ => new HashSet<string>());
|
||||
lock (_instanceSubscribers[instanceId])
|
||||
{
|
||||
_instanceSubscribers[instanceId].Add(connId);
|
||||
}
|
||||
_instanceSubscribers.GetOrAdd(instanceId, _ => new ConcurrentDictionary<string, bool>());
|
||||
|
||||
_instanceSubscribers[instanceId].TryAdd(connId, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task SendAgentPositionBatchAsync(List<AgentPositionSyncMsg> syncMsgs)
|
||||
{
|
||||
if(syncMsgs.Count == 0) return;
|
||||
if (syncMsgs.Count == 0) return;
|
||||
|
||||
foreach (var group in syncMsgs.GroupBy(m => m.InstanceId))
|
||||
{
|
||||
@ -109,7 +104,7 @@ namespace XNet.Business
|
||||
}
|
||||
else
|
||||
{
|
||||
msgBytes = tempMs.ToArray();
|
||||
msgBytes = tempMs.ToArray();// 超出池化数组长度,使用新分配的数组
|
||||
needReturnPooledBytes = false;
|
||||
}
|
||||
}
|
||||
@ -130,21 +125,19 @@ namespace XNet.Business
|
||||
var deadConnIds = _deadConnListPool.Get();
|
||||
try
|
||||
{
|
||||
lock (subscriberConnIds)
|
||||
foreach (var connKv in subscriberConnIds)
|
||||
{
|
||||
foreach (var connId in subscriberConnIds)
|
||||
if (_connections.TryGetValue(connKv.Key, out var socket))
|
||||
{
|
||||
if (_connections.TryGetValue(connId, out var socket))
|
||||
{
|
||||
// 3. 发送时传递有效长度
|
||||
if (needReturnPooledBytes)
|
||||
{
|
||||
_ = SendToSingleConnAsync(socket, msgBytes, msgBytesLength, connId, deadConnIds);
|
||||
// 3. 发送时传递有效长度
|
||||
_ = SendToSingleConnAsync(socket, msgBytes, msgBytesLength, connKv.Key, deadConnIds);
|
||||
}
|
||||
else
|
||||
{
|
||||
_ = SendToSingleConnAsync(socket, msgBytes, msgBytes.Length, connId, deadConnIds);
|
||||
}
|
||||
// 3. 发送整串字节数组
|
||||
_ = SendToSingleConnAsync(socket, msgBytes, msgBytes.Length, connKv.Key, deadConnIds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<OutputType>Library</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user