diff --git a/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs b/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs index aa9780c0..f7fde407 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs @@ -19,21 +19,6 @@ namespace TEngine.Core.Network private static readonly Dictionary Sessions = new (); public readonly Dictionary> RequestCallback = new(); -#if TENGINE_UNITY - public delegate void CsMsgDelegate(IResponse msg); - - public readonly Dictionary MsgHandles = new Dictionary(); - - public void RegisterMsgHandler(uint protocolCode,CsMsgDelegate ctx) - { - if (MsgHandles.ContainsKey(protocolCode)) - { - return; - } - MsgHandles.Add(protocolCode,ctx); - } -#endif - public static void Create(ANetworkMessageScheduler networkMessageScheduler, ANetworkChannel channel) { #if TENGINE_DEVELOP diff --git a/Assets/GameScripts/DotNet/Core/Network/Message/Dispatcher/MessageDispatcherSystem.cs b/Assets/GameScripts/DotNet/Core/Network/Message/Dispatcher/MessageDispatcherSystem.cs index 7ced506a..4f8eff04 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Message/Dispatcher/MessageDispatcherSystem.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Message/Dispatcher/MessageDispatcherSystem.cs @@ -34,6 +34,28 @@ namespace TEngine.Core.Network #endif private static readonly CoroutineLockQueueType ReceiveRouteMessageLock = new CoroutineLockQueueType("ReceiveRouteMessageLock"); +#if TENGINE_UNITY + + public readonly Dictionary>> MsgHandles = new Dictionary>>(); + + public void RegisterMsgHandler(uint protocolCode,Action ctx) + { + if (!MsgHandles.ContainsKey(protocolCode)) + { + MsgHandles[protocolCode] = new List>(); + } + MsgHandles[protocolCode].Add(ctx); + } + + public void UnRegisterMsgHandler(uint protocolCode,Action ctx) + { + if (MsgHandles.TryGetValue(protocolCode, out var handle)) + { + handle.Remove(ctx); + } + } +#endif + protected override void OnLoad(int assemblyName) { foreach (var type in AssemblyManager.ForEach(assemblyName, typeof(IMessage))) diff --git a/Assets/GameScripts/DotNet/Core/Network/Message/Interface/Scheduler/ANetworkMessageScheduler.cs b/Assets/GameScripts/DotNet/Core/Network/Message/Interface/Scheduler/ANetworkMessageScheduler.cs index 5142a227..d18d7ca4 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Message/Interface/Scheduler/ANetworkMessageScheduler.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Message/Interface/Scheduler/ANetworkMessageScheduler.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using Codice.Client.GameUI.Explorer; using TEngine.Core; #pragma warning disable CS8600 @@ -58,9 +57,12 @@ namespace TEngine.Core.Network #else #if TENGINE_UNITY - if (session.MsgHandles.TryGetValue(packInfo.ProtocolCode,out var msgDelegate)) + if (MessageDispatcherSystem.Instance.MsgHandles.TryGetValue(packInfo.ProtocolCode,out var msgDelegates)) { - msgDelegate.Invoke(aResponse); + foreach (var msgDelegate in msgDelegates) + { + msgDelegate.Invoke(aResponse); + } return; } #endif diff --git a/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs b/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs index d3d5d741..9f09fb6b 100644 --- a/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs +++ b/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using GameBase; using TEngine; using TEngine.Core.Network; @@ -54,6 +55,10 @@ namespace GameLogic public void Connect(string address, bool reconnect = false) { + if (Status == GameClientStatus.StatusConnected || Status == GameClientStatus.StatusLogin || Status == GameClientStatus.StatusEnter) + { + return; + } if (!reconnect) { // SetWatchReconnect(false); @@ -71,8 +76,11 @@ namespace GameLogic _lastAddress = address; Status = reconnect ? GameClientStatus.StatusReconnect : GameClientStatus.StatusInit; - - Scene.CreateSession(address, ProtocolType, OnConnectComplete, OnConnectFail); + + if (Scene.Session == null || Scene.Session.IsDisposed) + { + Scene.CreateSession(address, ProtocolType, OnConnectComplete, OnConnectFail); + } } private void OnConnectComplete() @@ -116,5 +124,15 @@ namespace GameLogic } Scene.Session.Send(memoryStream,rpcId,routeTypeOpCode,routeId); } + + public void RegisterMsgHandler(uint protocolCode,Action ctx) + { + MessageDispatcherSystem.Instance.RegisterMsgHandler(protocolCode,ctx); + } + + public void UnRegisterMsgHandler(uint protocolCode,Action ctx) + { + MessageDispatcherSystem.Instance.UnRegisterMsgHandler(protocolCode,ctx); + } } } \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs b/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs new file mode 100644 index 00000000..d378fcea --- /dev/null +++ b/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs @@ -0,0 +1,22 @@ +using TEngine.Core.Network; + +namespace GameLogic +{ + public class NetworkUtils + { + public static bool CheckError(IResponse response) + { + bool hasError = false; + if (response == null) + { + var networkError = "NetWork Response Error"; + hasError = true; + } + else + { + hasError = response.ErrorCode != 0; + } + return hasError; + } + } +} \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs.meta b/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs.meta new file mode 100644 index 00000000..343e949f --- /dev/null +++ b/Assets/GameScripts/HotFix/GameLogic/DataCenter/NetworkUtils.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4c373208f2ca4f28b35a28434a3b1103 +timeCreated: 1689262967 \ No newline at end of file diff --git a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo.meta b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo.meta new file mode 100644 index 00000000..23f2f84b --- /dev/null +++ b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fd38f6cec6fb1864596745f8fc7067d8 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs index 4e21629e..8fd40331 100644 --- a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs +++ b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/NetWorkDemoUI.cs @@ -58,12 +58,7 @@ class NetWorkDemoUI : UIWindow Log.Info("请输入账号和密码"); return; } - - GameClient.Instance.Send(new H_C2G_LoginRequest() - { - UserName = m_inputName.text, - Password = m_inputPassWord.text - }); + PlayerNetSys.Instance.DoLoginReq(m_inputName.text,m_inputPassWord.text); } private void OnClickRegisterBtn() diff --git a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs index 6fbd4061..5f259d14 100644 --- a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs +++ b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/PlayerNetSys.cs @@ -13,17 +13,35 @@ namespace GameLogic public override void Init() { base.Init(); + GameClient.Instance.RegisterMsgHandler(OuterOpcode.H_G2C_LoginResponse,OnLoginRes); } - - - } - public class H_C2G_LoginRequestHandler : MessageRPC - { - protected override async FTask Run(Session session, H_C2G_LoginRequest request, H_G2C_LoginResponse response, Action reply) + + public void OnLoginRes(IResponse response) { - Log.Debug($"收到请求登录的消息 request:{request.ToJson()}"); - response.Text = "登录成功"; - await FTask.CompletedTask; + if (NetworkUtils.CheckError(response)) + { + GameClient.Instance.Status = GameClientStatus.StatusConnected; + return; + } + H_G2C_LoginResponse ret = (H_G2C_LoginResponse)response; + Log.Debug(ret.ToJson()); + GameClient.Instance.Status = GameClientStatus.StatusEnter; + } + + public void DoLoginReq(string userName,string passWord) + { + if (GameClient.Instance.Status == GameClientStatus.StatusEnter) + { + Log.Info("当前已经登录成功。"); + return; + } + H_C2G_LoginRequest loginRequest =new H_C2G_LoginRequest() + { + UserName = userName, + Password = passWord + }; + GameClient.Instance.Send(loginRequest); + GameClient.Instance.Status = GameClientStatus.StatusLogin; } } } \ No newline at end of file