zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Unity/Assets/Hotfix/Logic/Model/Module/Message/PingComponentSystem.cs

70 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
namespace ET
{
[ObjectSystem]
public class PingComponentAwakeSystem: AwakeSystem<PingComponent>
{
public override void Awake(PingComponent self)
{
self.C2G_Ping = new C2G_Ping();
PingAsync(self).Coroutine();
}
private static async ETVoid PingAsync(PingComponent self)
{
Session session = self.GetParent<Session>();
long instanceId = self.InstanceId;
while (true)
{
if (self.InstanceId != instanceId)
{
return;
}
long time1 = TimeHelper.ClientNow();
try
{
G2C_Ping response = await session.Call(self.C2G_Ping) as G2C_Ping;
if (self.InstanceId != instanceId)
{
return;
}
long time2 = TimeHelper.ClientNow();
self.Ping = time2 - time1;
Game.EventSystem.Publish_Sync(new ET.EventType.ChangePing
{
ping = (int)self.Ping
});
Game.TimeInfo.ServerMinusClientTime = response.Time + (time2 - time1) / 2 - time2;
GlobalVariable.Token = response.Token;
await TimerComponent.Instance.WaitAsync(2000);
}
catch (RpcException e)
{
// session断开导致ping rpc报错记录一下即可不需要打成error
Log.Info($"ping error: {self.Id} {e.Error}");
return;
}
catch (Exception e)
{
Log.Error($"ping error: \n{e}");
}
}
}
}
[ObjectSystem]
public class PingComponentDestroySystem: DestroySystem<PingComponent>
{
public override void Destroy(PingComponent self)
{
self.Ping = default;
}
}
}