From 3863fae1f7ecc0021fcfc549e800d18792ac881c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Wed, 19 Mar 2025 13:02:15 +0800 Subject: [PATCH] modify --- Runtime/Network/EConnectState.cs | 9 +++++++ Runtime/Network/EConnectState.cs.meta | 3 +++ Runtime/Network/INetworkManager.cs | 8 ++++++ Runtime/Network/NetworkComponent.cs | 18 +++++++++++++ Runtime/Network/NetworkManager.cs | 37 +++++++++++++++++++-------- 5 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 Runtime/Network/EConnectState.cs create mode 100644 Runtime/Network/EConnectState.cs.meta diff --git a/Runtime/Network/EConnectState.cs b/Runtime/Network/EConnectState.cs new file mode 100644 index 0000000..d535e57 --- /dev/null +++ b/Runtime/Network/EConnectState.cs @@ -0,0 +1,9 @@ +namespace AlicizaX.Network.Runtime +{ + public enum EConnectState:byte + { + None, + Connect, + Disconnect, + } +} diff --git a/Runtime/Network/EConnectState.cs.meta b/Runtime/Network/EConnectState.cs.meta new file mode 100644 index 0000000..cdbef35 --- /dev/null +++ b/Runtime/Network/EConnectState.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c53aae23b1a94f598e335310d9614ecb +timeCreated: 1742355596 \ No newline at end of file diff --git a/Runtime/Network/INetworkManager.cs b/Runtime/Network/INetworkManager.cs index 7d865ff..48dfbbc 100644 --- a/Runtime/Network/INetworkManager.cs +++ b/Runtime/Network/INetworkManager.cs @@ -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 Call(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); } } diff --git a/Runtime/Network/NetworkComponent.cs b/Runtime/Network/NetworkComponent.cs index 4c0eb3c..d5f4e99 100644 --- a/Runtime/Network/NetworkComponent.cs +++ b/Runtime/Network/NetworkComponent.cs @@ -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 Call(IRequest request, long routeId = 0) where T : IResponse + { + return await _networkManager.Call(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); + } } } diff --git a/Runtime/Network/NetworkManager.cs b/Runtime/Network/NetworkManager.cs index 0fef68f..8bfca4d 100644 --- a/Runtime/Network/NetworkManager.cs +++ b/Runtime/Network/NetworkManager.cs @@ -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 Call(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().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"); } } }