mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using System;
|
|
using System.Net;
|
|
|
|
namespace ET.Client
|
|
{
|
|
[EntitySystemOf(typeof(RouterCheckComponent))]
|
|
public static partial class RouterCheckComponentSystem
|
|
{
|
|
[EntitySystem]
|
|
private static void Awake(this RouterCheckComponent self)
|
|
{
|
|
self.CheckAsync().Coroutine();
|
|
}
|
|
|
|
private static async ETTask CheckAsync(this RouterCheckComponent self)
|
|
{
|
|
Session session = self.GetParent<Session>();
|
|
long instanceId = self.InstanceId;
|
|
Fiber fiber = self.Fiber();
|
|
Scene root = fiber.Root;
|
|
while (true)
|
|
{
|
|
if (self.InstanceId != instanceId)
|
|
{
|
|
return;
|
|
}
|
|
|
|
await fiber.TimerComponent.WaitAsync(1000);
|
|
|
|
if (self.InstanceId != instanceId)
|
|
{
|
|
return;
|
|
}
|
|
|
|
long time = self.Fiber().TimeInfo.ClientFrameTime();
|
|
|
|
if (time - session.LastRecvTime < 7 * 1000)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
long sessionId = session.Id;
|
|
|
|
(uint localConn, uint remoteConn) = session.AService.GetChannelConn(sessionId);
|
|
|
|
IPEndPoint realAddress = self.GetParent<Session>().RemoteAddress;
|
|
Log.Info($"get recvLocalConn start: {root.Id} {realAddress} {localConn} {remoteConn}");
|
|
|
|
(uint recvLocalConn, IPEndPoint routerAddress) = await RouterHelper.GetRouterAddress(root, realAddress, localConn, remoteConn);
|
|
if (recvLocalConn == 0)
|
|
{
|
|
Log.Error($"get recvLocalConn fail: {root.Id} {routerAddress} {realAddress} {localConn} {remoteConn}");
|
|
continue;
|
|
}
|
|
|
|
Log.Info($"get recvLocalConn ok: {root.Id} {routerAddress} {realAddress} {recvLocalConn} {localConn} {remoteConn}");
|
|
|
|
session.LastRecvTime = self.Fiber().TimeInfo.ClientNow();
|
|
|
|
session.AService.ChangeAddress(sessionId, routerAddress);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |