com.alicizax.unity.network/Runtime/Network/NetworkComponent.cs

62 lines
1.9 KiB
C#
Raw Normal View History

2025-02-24 17:35:48 +08:00
using AlicizaX.Runtime;
using Cysharp.Threading.Tasks;
using Fantasy.Network;
2025-03-19 13:02:15 +08:00
using Fantasy.Network.Interface;
2025-02-24 17:35:48 +08:00
using UnityEngine;
using UnityEngine.Scripting;
namespace AlicizaX.Network.Runtime
{
[DisallowMultipleComponent]
[AddComponentMenu("Game Framework/Network")]
[Preserve]
public sealed class NetworkComponent : GameFrameworkComponent
{
private INetworkManager _networkManager;
2025-03-18 19:41:26 +08:00
[SerializeField] private int m_HeartInterval = 2000;
2025-03-19 13:02:15 +08:00
public EConnectState ConnectState => _networkManager.ConnectState;
2025-02-24 17:35:48 +08:00
protected override void Awake()
{
ImplementationComponentType = Utility.Assembly.GetType(componentType);
InterfaceComponentType = typeof(INetworkManager);
base.Awake();
_networkManager = SysModuleCenter.GetModule<INetworkManager>();
if (_networkManager == null)
{
Log.Fatal("Network Manager is invalid.");
return;
}
2025-03-18 19:41:26 +08:00
_networkManager.SetHeartInterval(m_HeartInterval);
2025-02-24 17:35:48 +08:00
}
public async UniTask Initialize(params System.Reflection.Assembly[] assemblies)
{
await _networkManager.Initialize(assemblies);
}
public void Connect(string remoteAddress, NetworkProtocolType networkProtocolType, bool isHttps)
{
_networkManager.Connect(remoteAddress, networkProtocolType, isHttps);
}
2025-03-19 13:02:15 +08:00
public async UniTask<T> Call<T>(IRequest request, long routeId = 0) where T : IResponse
{
return await _networkManager.Call<T>(request, routeId);
}
public void Send(IMessage message, uint rpcId = 0, long routeId = 0)
{
_networkManager.Send(message, rpcId, routeId);
}
public void Send(IRouteMessage routeMessage, uint rpcId = 0, long routeId = 0)
{
_networkManager.Send(routeMessage, rpcId, routeId);
}
2025-02-24 17:35:48 +08:00
}
}