mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
完善网络框架,增加服务器断开连接回调
This commit is contained in:
8
Assets/GameScripts/DotNet/Core/Modules/Session.meta
Normal file
8
Assets/GameScripts/DotNet/Core/Modules/Session.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62fb06b1efb2c3e4cb58e2bc0119688d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,63 @@
|
||||
#if TENGINE_UNITY
|
||||
using TEngine.Core;
|
||||
using TEngine.Core.Network;
|
||||
|
||||
namespace TEngine.Logic
|
||||
{
|
||||
public class SessionHeartbeatComponent : Entity
|
||||
{
|
||||
private long _timerId;
|
||||
private long _selfRunTimeId;
|
||||
private Session _session;
|
||||
private readonly PingRequest _pingRequest = new PingRequest();
|
||||
|
||||
public int Ping { get; private set; }
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Stop();
|
||||
Ping = 0;
|
||||
_session = null;
|
||||
_selfRunTimeId = 0;
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
public void Start(int interval)
|
||||
{
|
||||
_session = (Session)Parent;
|
||||
_selfRunTimeId = RuntimeId;
|
||||
_timerId = TimerScheduler.Instance.Unity.RepeatedTimer(interval, () => RepeatedSend().Coroutine());
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (_timerId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TimerScheduler.Instance?.Unity.RemoveByRef(ref _timerId);
|
||||
}
|
||||
|
||||
private async FTask RepeatedSend()
|
||||
{
|
||||
if (_selfRunTimeId != RuntimeId)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
var requestTime = TimeHelper.Now;
|
||||
var pingResponse = (PingResponse)await _session.Call(_pingRequest);
|
||||
|
||||
if (pingResponse.ErrorCode != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var responseTime = TimeHelper.Now;
|
||||
Ping = (int)(responseTime - requestTime) / 2;
|
||||
TimeHelper.TimeDiff = pingResponse.Now + Ping - responseTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a99afa40336b6ab4b8f1935689ff27c1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,58 @@
|
||||
#if TENGINE_NET
|
||||
using TEngine.Core.Network;
|
||||
|
||||
namespace TEngine.Core;
|
||||
|
||||
public class SessionIdleCheckerComponent: Entity
|
||||
{
|
||||
private long _timeOut;
|
||||
private long _timerId;
|
||||
private long _selfRuntimeId;
|
||||
private Session _session;
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
Stop();
|
||||
_timeOut = 0;
|
||||
_selfRuntimeId = 0;
|
||||
_session = null;
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
public void Start(int interval, int timeOut)
|
||||
{
|
||||
_timeOut = timeOut;
|
||||
_session = (Session)Parent;
|
||||
_selfRuntimeId = RuntimeId;
|
||||
_timerId = TimerScheduler.Instance.Core.RepeatedTimer(interval, Check);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
if (_timerId == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TimerScheduler.Instance.Core.RemoveByRef(ref _timerId);
|
||||
}
|
||||
|
||||
private void Check()
|
||||
{
|
||||
if (_selfRuntimeId != RuntimeId)
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
var timeNow = TimeHelper.Now;
|
||||
|
||||
if (timeNow - _session.LastReceiveTime < _timeOut)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Warning($"session timeout id:{Id}");
|
||||
_session.Dispose();
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8ecddd8867e88f409286a99647a236c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user