修复插槽清理后的bug

This commit is contained in:
2026-01-18 20:20:33 +08:00
parent 58911b2ef4
commit 4dea9d9da7
8 changed files with 170 additions and 164 deletions

View File

@@ -1,21 +1,21 @@
using Fantasy.Entitas; // using Fantasy.Entitas;
//
namespace NB; // namespace NB;
//
public class EntityTimeOutComponent : Entity // public class EntityTimeOutComponent : Entity
{ // {
/// <summary> // /// <summary>
/// 主要是用于检测每次请求的间隔,这里存放是下一次能正常通信时间 // /// 主要是用于检测每次请求的间隔,这里存放是下一次能正常通信时间
/// </summary> // /// </summary>
public long NextTime; // public long NextTime;
//
/// <summary> // /// <summary>
/// 用来设置检查的间隔时间 // /// 用来设置检查的间隔时间
/// </summary> // /// </summary>
public int Interval; // public int Interval;
//
/// <summary> // /// <summary>
/// 用于记录时间计划任务的ID后面可以通过这个ID随时取消这个任务 // /// 用于记录时间计划任务的ID后面可以通过这个ID随时取消这个任务
/// </summary> // /// </summary>
public long TimerId; // public long TimerId;
} // }

View File

@@ -13,14 +13,14 @@ public class C2A_LoginRequestHandler : MessageRPC<C2A_LoginRequest, A2C_LoginRes
protected override async FTask Run(Session session, C2A_LoginRequest request, A2C_LoginResponse response, protected override async FTask Run(Session session, C2A_LoginRequest request, A2C_LoginResponse response,
Action reply) Action reply)
{ {
if (!session.CheckInterval(20000)) // if (!session.CheckInterval(20000))
{ // {
// 返回这个3代表操作过于频繁。 // // 返回这个3代表操作过于频繁。
response.ErrorCode = ErrorCode.ErrFrequent; // response.ErrorCode = ErrorCode.ErrFrequent;
return; // return;
} // }
session.SetTimeout(3000); // session.SetTimeout(3000);
var scene = session.Scene; var scene = session.Scene;
Log.Info($"登录服场景 {scene.Id} {scene.RuntimeId} {scene.SceneConfigId}"); Log.Info($"登录服场景 {scene.Id} {scene.RuntimeId} {scene.SceneConfigId}");
var result = await AuthenticationHelper.Login(scene, request.Username, request.Password); var result = await AuthenticationHelper.Login(scene, request.Username, request.Password);

View File

@@ -1,48 +1,48 @@
using System; // using System;
using NB.Authentication; // using NB.Authentication;
using Fantasy; // using Fantasy;
using Fantasy.Async; // using Fantasy.Async;
using Fantasy.Entitas; // using Fantasy.Entitas;
using Fantasy.Network; // using Fantasy.Network;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract // // ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
//
namespace NB; // namespace NB;
//
public static class EntityHelper // public static class EntityHelper
{ // {
public static bool CheckInterval(this Entity entity, int interval) // public static bool CheckInterval(this Entity entity, int interval)
{ // {
var sessionTimeOutComponent = entity.GetComponent<EntityTimeOutComponent>(); // var sessionTimeOutComponent = entity.GetComponent<EntityTimeOutComponent>();
//
if (sessionTimeOutComponent == null) // if (sessionTimeOutComponent == null)
{ // {
sessionTimeOutComponent = entity.AddComponent<EntityTimeOutComponent>(); // sessionTimeOutComponent = entity.AddComponent<EntityTimeOutComponent>();
sessionTimeOutComponent.SetInterval(interval); // sessionTimeOutComponent.SetInterval(interval);
return true; // return true;
} // }
//
return sessionTimeOutComponent.CheckInterval(); // return sessionTimeOutComponent.CheckInterval();
} // }
//
public static void SetTimeout(this Entity entity, int timeout = 3000, Func<FTask>? task = null) // public static void SetTimeout(this Entity entity, int timeout = 3000, Func<FTask>? task = null)
{ // {
var sessionTimeOutComponent = entity.GetComponent<EntityTimeOutComponent>(); // var sessionTimeOutComponent = entity.GetComponent<EntityTimeOutComponent>();
//
if (sessionTimeOutComponent == null) // if (sessionTimeOutComponent == null)
{ // {
sessionTimeOutComponent = entity.AddComponent<EntityTimeOutComponent>(); // sessionTimeOutComponent = entity.AddComponent<EntityTimeOutComponent>();
} // }
//
sessionTimeOutComponent.TimeOut(timeout, task); // sessionTimeOutComponent.TimeOut(timeout, task);
} // }
//
public static bool IsTimeOutComponent(this Entity entity) // public static bool IsTimeOutComponent(this Entity entity)
{ // {
return entity.GetComponent<EntityTimeOutComponent>() != null; // return entity.GetComponent<EntityTimeOutComponent>() != null;
} // }
//
public static void CancelTimeout(this Entity entity) // public static void CancelTimeout(this Entity entity)
{ // {
entity.RemoveComponent<EntityTimeOutComponent>(); // entity.RemoveComponent<EntityTimeOutComponent>();
} // }
} // }

View File

@@ -1,79 +1,79 @@
using System; // using System;
using Fantasy; // using Fantasy;
using Fantasy.Async; // using Fantasy.Async;
using Fantasy.Entitas.Interface; // using Fantasy.Entitas.Interface;
using Fantasy.Helper; // using Fantasy.Helper;
//
namespace NB.Authentication; // namespace NB.Authentication;
//
public sealed class EntityTimeOutComponentDestroySystem : DestroySystem<EntityTimeOutComponent> // public sealed class EntityTimeOutComponentDestroySystem : DestroySystem<EntityTimeOutComponent>
{ // {
protected override void Destroy(EntityTimeOutComponent self) // protected override void Destroy(EntityTimeOutComponent self)
{ // {
if (self.TimerId != 0) // if (self.TimerId != 0)
{ // {
self.Scene.TimerComponent.Net.Remove(ref self.TimerId); // self.Scene.TimerComponent.Net.Remove(ref self.TimerId);
} // }
//
self.NextTime = 0; // self.NextTime = 0;
self.Interval = 0; // self.Interval = 0;
} // }
} // }
//
public static class EntityTimeOutComponentSystem // public static class EntityTimeOutComponentSystem
{ // {
public static void SetInterval(this EntityTimeOutComponent self, int interval) // public static void SetInterval(this EntityTimeOutComponent self, int interval)
{ // {
if (interval <= 0) // if (interval <= 0)
{ // {
throw new ArgumentException("interval must be greater than 0", nameof(interval)); // throw new ArgumentException("interval must be greater than 0", nameof(interval));
} // }
//
self.Interval = interval; // self.Interval = interval;
self.NextTime = TimeHelper.Now + interval; // self.NextTime = TimeHelper.Now + interval;
} // }
//
public static bool CheckInterval(this EntityTimeOutComponent self) // public static bool CheckInterval(this EntityTimeOutComponent self)
{ // {
if (self.NextTime > TimeHelper.Now) // if (self.NextTime > TimeHelper.Now)
{ // {
Log.Warning("当前连接请求的间隔过小"); // Log.Warning("当前连接请求的间隔过小");
return false; // return false;
} // }
//
self.NextTime = TimeHelper.Now + self.Interval; // self.NextTime = TimeHelper.Now + self.Interval;
return true; // return true;
} // }
//
public static void TimeOut(this EntityTimeOutComponent self, int timeout, Func<FTask>? task = null) // public static void TimeOut(this EntityTimeOutComponent self, int timeout, Func<FTask>? task = null)
{ // {
var scene = self.Scene; // var scene = self.Scene;
var parentRunTimeId = self.Parent.RuntimeId; // var parentRunTimeId = self.Parent.RuntimeId;
//
if (self.TimerId != 0) // if (self.TimerId != 0)
{ // {
self.Scene.TimerComponent.Net.Remove(ref self.TimerId); // self.Scene.TimerComponent.Net.Remove(ref self.TimerId);
} // }
//
self.TimerId = // self.TimerId =
scene.TimerComponent.Net.OnceTimer(timeout, () => { self.Handler(parentRunTimeId, task).Coroutine(); }); // scene.TimerComponent.Net.OnceTimer(timeout, () => { self.Handler(parentRunTimeId, task).Coroutine(); });
} // }
//
private static async FTask Handler(this EntityTimeOutComponent self, long parentRunTimeId, Func<FTask>? task = null) // private static async FTask Handler(this EntityTimeOutComponent self, long parentRunTimeId, Func<FTask>? task = null)
{ // {
var selfParent = self.Parent; // var selfParent = self.Parent;
//
if (selfParent == null || parentRunTimeId != selfParent.RuntimeId) // if (selfParent == null || parentRunTimeId != selfParent.RuntimeId)
{ // {
return; // return;
} // }
//
if (task != null) // if (task != null)
{ // {
await task(); // await task();
} // }
//
self.TimerId = 0; // self.TimerId = 0;
selfParent.Dispose(); // selfParent.Dispose();
} // }
} // }

View File

@@ -13,7 +13,7 @@ public class C2Game_GetItemsRequestHandler : AddressRPC<Player, C2Game_GetItemsR
var itemContainer = entity.GetComponent<PlayerItemContainerComponent>(); var itemContainer = entity.GetComponent<PlayerItemContainerComponent>();
response.Items = itemContainer.GetItemInfos(); response.Items = itemContainer.GetItemInfos();
response.Rigs = itemContainer.GetRigInfos(); response.Rigs = itemContainer.GetRigInfos();
response.Slots = itemContainer.Slots; response.Slots = itemContainer.GetSlots();
await FTask.CompletedTask; await FTask.CompletedTask;
} }
} }

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Fantasy; using Fantasy;
using Fantasy.Async; using Fantasy.Async;
using Fantasy.Entitas.Interface; using Fantasy.Entitas.Interface;
@@ -161,5 +162,10 @@ public static class PlayerItemContainerComponentSystem
return ret; return ret;
} }
public static List<long> GetSlots(this PlayerItemContainerComponent self)
{
return self.Slots.ToList();
}
#endregion #endregion
} }

View File

@@ -79,7 +79,7 @@ public static class PlayerManageComponentSystem
{ {
Log.Debug("检测到当前账号已经在缓存中了"); Log.Debug("检测到当前账号已经在缓存中了");
// 如果有延迟下线的计划任务,那就先取消一下。 // 如果有延迟下线的计划任务,那就先取消一下。
account.CancelTimeout(); // account.CancelTimeout();
// 如果在Gate的缓存中已经存在了该账号那只能以下几种可能: // 如果在Gate的缓存中已经存在了该账号那只能以下几种可能:
// 1、同一客户端发送了重复登录的请求数据。 // 1、同一客户端发送了重复登录的请求数据。
// 2、客户端经历的断线然后又重新连接到这个服务器上了断线重连 // 2、客户端经历的断线然后又重新连接到这个服务器上了断线重连

View File

@@ -92,12 +92,12 @@ public static class PlayerHelper
return; return;
} }
// 如果不存在定时任务的组件,那就添加并设置定时任务 // // 如果不存在定时任务的组件,那就添加并设置定时任务
if (account.IsTimeOutComponent()) // if (account.IsTimeOutComponent())
{ // {
// 如果已经存在了,那就表示当然已经有一个延时断开的任务了,那就不需要重复添加了 // // 如果已经存在了,那就表示当然已经有一个延时断开的任务了,那就不需要重复添加了
return; // return;
} // }
// 立即下线处理 // 立即下线处理
if (timeOut <= 0) if (timeOut <= 0)
@@ -108,7 +108,7 @@ public static class PlayerHelper
} }
// 设置延迟下线 // 设置延迟下线
account.SetTimeout(timeOut, account.Disconnect); // account.SetTimeout(timeOut, account.Disconnect);
} }
#endregion #endregion