This commit is contained in:
陈思海 2025-03-19 13:02:15 +08:00
parent 20ab04db1f
commit 3863fae1f7
5 changed files with 65 additions and 10 deletions

View File

@ -0,0 +1,9 @@
namespace AlicizaX.Network.Runtime
{
public enum EConnectState:byte
{
None,
Connect,
Disconnect,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c53aae23b1a94f598e335310d9614ecb
timeCreated: 1742355596

View File

@ -1,13 +1,21 @@
using AlicizaX.Runtime;
using Cysharp.Threading.Tasks;
using Fantasy.Network;
using Fantasy.Network.Interface;
namespace AlicizaX.Network.Runtime
{
public interface INetworkManager : IModule
{
public EConnectState ConnectState { get; }
void SetHeartInterval(int heartInterval);
UniTask Initialize(params System.Reflection.Assembly[] assemblies);
void Connect(string remoteAddress, NetworkProtocolType networkProtocolType, bool isHttps);
UniTask<T> Call<T>(IRequest request, long routeId = 0) where T : IResponse;
void Send(IMessage message, uint rpcId = 0, long routeId = 0);
void Send(IRouteMessage routeMessage, uint rpcId = 0, long routeId = 0);
}
}

View File

@ -1,6 +1,7 @@
using AlicizaX.Runtime;
using Cysharp.Threading.Tasks;
using Fantasy.Network;
using Fantasy.Network.Interface;
using UnityEngine;
using UnityEngine.Scripting;
@ -15,6 +16,8 @@ namespace AlicizaX.Network.Runtime
[SerializeField] private int m_HeartInterval = 2000;
public EConnectState ConnectState => _networkManager.ConnectState;
protected override void Awake()
{
ImplementationComponentType = Utility.Assembly.GetType(componentType);
@ -39,5 +42,20 @@ namespace AlicizaX.Network.Runtime
{
_networkManager.Connect(remoteAddress, networkProtocolType, isHttps);
}
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);
}
}
}

View File

@ -3,6 +3,7 @@ using AlicizaX.Runtime;
using Cysharp.Threading.Tasks;
using Fantasy;
using Fantasy.Network;
using Fantasy.Network.Interface;
using Log = AlicizaX.Runtime.Log;
namespace AlicizaX.Network.Runtime
@ -13,8 +14,11 @@ namespace AlicizaX.Network.Runtime
private Scene _scene;
private Session _session;
private int _heartInterval;
private bool isConnect;
public EConnectState ConnectState => _connectState;
private EConnectState _connectState;
void IModule.Dispose()
{
_scene?.Dispose();
@ -42,29 +46,42 @@ namespace AlicizaX.Network.Runtime
public void Connect(string remoteAddress, NetworkProtocolType networkProtocolType, bool isHttps)
{
if (isConnect) return;
_session = _scene.Connect(remoteAddress, networkProtocolType, OnConnectComplete, OnConnectFail, OnConnectDisconnect, isHttps);
}
public async UniTask<T> Call<T>(IRequest request, long routeId = 0) where T : IResponse
{
return (T)await _session.Call(request, routeId);
}
public void Send(IMessage message, uint rpcId = 0, long routeId = 0)
{
_session.Send(message, rpcId, routeId);
}
public void Send(IRouteMessage routeMessage, uint rpcId = 0, long routeId = 0)
{
_session.Send(routeMessage, rpcId, routeId);
}
private void OnConnectComplete()
{
isConnect = true;
Log.Debug("连接成功");
// 添加心跳组件给Session。
// Start(2000)就是2000毫秒。
_connectState = EConnectState.Connect;
_session.AddComponent<SessionHeartbeatComponent>().Start(_heartInterval);
Log.Debug("Connect Success");
}
private void OnConnectFail()
{
isConnect = false;
Log.Debug("连接失败");
_connectState = EConnectState.None;
Log.Debug("Connect Fail");
}
private void OnConnectDisconnect()
{
isConnect = false;
Log.Debug("连接断开");
_connectState = EConnectState.Disconnect;
Log.Debug("Disconnect");
}
}
}