2025-07-17 18:26:28 +08:00

65 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
namespace ET.Client
{
[EntitySystemOf(typeof(PingComponent))]
public static partial class PingComponentSystem
{
[EntitySystem]
private static void Awake(this PingComponent self)
{
self.PingAsync().Coroutine();
}
[EntitySystem]
private static void Destroy(this PingComponent self)
{
self.Ping = default;
}
private static async ETTask PingAsync(this PingComponent self)
{
Session session = self.GetParent<Session>();
long instanceId = self.InstanceId;
Fiber fiber = self.Fiber();
while (true)
{
try
{
await fiber.Root.GetComponent<TimerComponent>().WaitAsync(2000);
if (self.InstanceId != instanceId)
{
return;
}
long time1 = TimeInfo.Instance.ClientNow();
// C2G_Ping不需要调用disposeCall中会判断如果用了对象池会自动回收
C2G_Ping c2GPing = C2G_Ping.Create(true);
// 这里response要用using才能回收到池默认不回收
using G2C_Ping response = await session.Call(c2GPing) as G2C_Ping;
if (self.InstanceId != instanceId)
{
return;
}
long time2 = TimeInfo.Instance.ClientNow();
self.Ping = time2 - time1;
TimeInfo.Instance.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
}
catch (RpcException e)
{
// session断开导致ping rpc报错记录一下即可不需要打成error
Log.Debug($"session disconnect, ping error: {self.Id} {e.Error}");
return;
}
catch (Exception e)
{
Log.Debug($"ping error: \n{e}");
}
}
}
}
}