From 526baf45fa2135d0863baeb7239599c0409b3ef2 Mon Sep 17 00:00:00 2001 From: ALEXTANG <574809918@qq.com> Date: Tue, 18 Jul 2023 17:36:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BD=91=E7=BB=9C=E6=A1=86?= =?UTF-8?q?=E6=9E=B6=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/GameScripts/DotNet/Core/App.cs | 16 +-- Assets/GameScripts/DotNet/Core/AppDefine.cs | 11 ++ ...tMessageInfo.cs.meta => AppDefine.cs.meta} | 2 +- Assets/GameScripts/DotNet/Core/Define.cs | 110 +++++++++++++++++- Assets/GameScripts/DotNet/Core/Define.cs.meta | 2 +- .../DotNet/Core/Entitas/Scene/Scene.cs | 4 +- .../Core/Exporter/Excel/Base/ExcelDefine.cs | 75 ------------ .../Core/Exporter/Excel/ExcelExporter.cs | 40 +++---- .../DotNet/Core/Exporter/Exporter.cs | 94 +++++++-------- .../Core/Exporter/Interface/ICustomExport.cs | 12 +- .../Core/Exporter/ProtoBuf/ProtoBufDefine.cs | 28 ----- .../Exporter/ProtoBuf/ProtoBufExporter.cs | 36 +++--- Assets/GameScripts/DotNet/Core/Log/LogCore.cs | 4 +- Assets/GameScripts/DotNet/Core/Modules.meta | 8 ++ .../DotNet/Core/Modules/Session.meta | 8 ++ .../Session/SessionHeartbeatComponent.cs | 63 ++++++++++ .../SessionHeartbeatComponent.cs.meta} | 2 +- .../Session/SessionIdleCheckerComponent.cs | 58 +++++++++ .../SessionIdleCheckerComponent.cs.meta} | 2 +- .../Network/Base/Interface/AClientNetwork.cs | 15 ++- .../Core/Network/Base/Interface/ANetwork.cs | 46 ++------ .../Core/Network/Base/LastMessageInfo.cs | 19 --- .../DotNet/Core/Network/Base/Server/Server.cs | 2 +- .../Network/Entity/ClientNetworkComponent.cs | 7 +- .../Core/Network/Entity/Session/Session.cs | 10 +- .../Scheduler/ANetworkMessageScheduler.cs | 1 + .../Message/Protocols/CoreMessageProtocols.cs | 2 +- .../Scheduler/ClientMessageScheduler.cs | 1 + .../KCP/Client/KCPClientNetwork.cs | 24 ++-- .../KCP/Server/KCPServerNetwork.cs | 2 - .../KCP/Server/KCPServerNetworkChannel.cs | 2 +- .../TCP/Client/TCPClientNetwork.cs | 31 ++--- .../TCP/Server/TCPServerNetwork.cs | 2 +- .../Network/PacketParser/OuterPacketParser.cs | 4 + .../DotNet/Core/TEngineSettingsHelper.cs | 81 +++++++++++++ .../DotNet/Core/TEngineSettingsHelper.cs.meta | 11 ++ .../HotFix/GameLogic/DataCenter/GameClient.cs | 8 +- .../NetWorkDemo/Examples/Network/UIEntry.cs | 10 +- DotNet/App/TEngineSettings.json | 14 +++ 39 files changed, 559 insertions(+), 308 deletions(-) create mode 100644 Assets/GameScripts/DotNet/Core/AppDefine.cs rename Assets/GameScripts/DotNet/Core/{Network/Base/LastMessageInfo.cs.meta => AppDefine.cs.meta} (83%) delete mode 100644 Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs delete mode 100644 Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs create mode 100644 Assets/GameScripts/DotNet/Core/Modules.meta create mode 100644 Assets/GameScripts/DotNet/Core/Modules/Session.meta create mode 100644 Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs rename Assets/GameScripts/DotNet/Core/{Exporter/Excel/Base/ExcelDefine.cs.meta => Modules/Session/SessionHeartbeatComponent.cs.meta} (83%) create mode 100644 Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs rename Assets/GameScripts/DotNet/Core/{Exporter/ProtoBuf/ProtoBufDefine.cs.meta => Modules/Session/SessionIdleCheckerComponent.cs.meta} (83%) delete mode 100644 Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs create mode 100644 Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs create mode 100644 Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs.meta diff --git a/Assets/GameScripts/DotNet/Core/App.cs b/Assets/GameScripts/DotNet/Core/App.cs index 2d18606b..bbae9a5b 100644 --- a/Assets/GameScripts/DotNet/Core/App.cs +++ b/Assets/GameScripts/DotNet/Core/App.cs @@ -16,9 +16,11 @@ namespace TEngine // 解析命令行参数 Parser.Default.ParseArguments(Environment.GetCommandLineArgs()) .WithNotParsed(error => throw new Exception("Command line format error!")) - .WithParsed(option => Define.Options = option); + .WithParsed(option => AppDefine.Options = option); + // 加载框架配置 + TEngineSettingsHelper.Initialize(); // 检查启动参数 - switch (Define.Options.AppType) + switch (AppDefine.Options.AppType) { case AppType.Game: { @@ -31,12 +33,12 @@ namespace TEngine } default: { - throw new NotSupportedException($"AppType is {Define.Options.AppType} Unrecognized!"); + throw new NotSupportedException($"AppType is {AppDefine.Options.AppType} Unrecognized!"); } } // 根据不同的运行模式来选择日志的方式 - switch (Define.Options.Mode) + switch (AppDefine.Options.Mode) { case Mode.Develop: { @@ -71,7 +73,7 @@ namespace TEngine // 加载核心程序集 AssemblyManager.Initialize(); - Log.Info($"Start Server Param => {Parser.Default.FormatCommandLine(Define.Options)}"); + Log.Info($"Start Server Param => {Parser.Default.FormatCommandLine(AppDefine.Options)}"); } catch (Exception exception) { @@ -81,7 +83,7 @@ namespace TEngine public static async FTask Start() { - switch (Define.Options.Mode) + switch (AppDefine.Options.Mode) { case Mode.Develop: { @@ -99,7 +101,7 @@ namespace TEngine { // 发布模式只会启动启动参数传递的Server、也就是只会启动一个Server // 您可以做一个Server专门用于管理启动所有Server的工作 - await Server.Create(Define.Options.AppId); + await Server.Create(AppDefine.Options.AppId); return; } } diff --git a/Assets/GameScripts/DotNet/Core/AppDefine.cs b/Assets/GameScripts/DotNet/Core/AppDefine.cs new file mode 100644 index 00000000..a3bcadd0 --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/AppDefine.cs @@ -0,0 +1,11 @@ +#if TENGINE_NET +#pragma warning disable CS8618 +namespace TEngine +{ + public static class AppDefine + { + public static CommandLineOptions Options; + public static uint AppId => Options.AppId; + } +} +#endif \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs.meta b/Assets/GameScripts/DotNet/Core/AppDefine.cs.meta similarity index 83% rename from Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs.meta rename to Assets/GameScripts/DotNet/Core/AppDefine.cs.meta index 22f68fc9..1334d185 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs.meta +++ b/Assets/GameScripts/DotNet/Core/AppDefine.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: d9135b10424d287419b2b9b46caa803d +guid: e08a4791989b16843a924bf798cdaa34 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/GameScripts/DotNet/Core/Define.cs b/Assets/GameScripts/DotNet/Core/Define.cs index 49def5d1..99eb8d11 100644 --- a/Assets/GameScripts/DotNet/Core/Define.cs +++ b/Assets/GameScripts/DotNet/Core/Define.cs @@ -1,11 +1,111 @@ #if TENGINE_NET -#pragma warning disable CS8618 -namespace TEngine +namespace TEngine.Core; + +public static class Define { - public static class Define +#if TENGINE_NET + + #region ProtoBuf + + public static readonly char[] SplitChars = { ' ', '\t' }; + /// + /// ProtoBuf文件夹 + /// + public static string ProtoBufDirectory; + /// + /// 服务端生成文件夹 + /// + public static string ProtoBufServerDirectory; + /// + /// 客户端生成文件夹 + /// + public static string ProtoBufClientDirectory; + /// + /// 代码模板路径 + /// + public static string ProtoBufTemplatePath; + + #endregion + + #region Excel + + /// + /// 配置文件根目录 + /// + public static string ExcelProgramPath; + /// + /// 版本文件Excel + /// + public static string ExcelVersionFile; + /// + /// 服务器代码生成文件夹 + /// + public static string ExcelServerFileDirectory; + /// + /// 客户端代码生成文件夹 + /// + public static string ExcelClientFileDirectory; + /// + /// 服务器二进制数据文件夹 + /// + public static string ExcelServerBinaryDirectory; + /// + /// 客户端二进制数据文件夹 + /// + public static string ExcelClientBinaryDirectory; + /// + /// 服务器Json数据文件夹 + /// + public static string ExcelServerJsonDirectory; + /// + /// 客户端Json数据文件夹 + /// + public static string ExcelClientJsonDirectory; + /// + /// 服务器自定义导出代码 + /// + public static string ServerCustomExportDirectory; + /// + /// 客户端自定义导出代码 + /// + public static string ClientCustomExportDirectory; + /// + /// 自定义导出代码存放的程序集 + /// + public static string CustomExportAssembly; + /// + /// 导表支持的类型 + /// + public static readonly HashSet ColTypeSet = new HashSet() { - public static CommandLineOptions Options; - public static uint AppId => Options.AppId; + "", "0", "bool", "byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "string", "AttrConfig", + "short[]", "int[]", "long[]", "float[]", "string[]" + }; + /// + /// Excel生成代码模板的位置 + /// + public static string ExcelTemplatePath; + /// + /// 代码模板 + /// + public static string ExcelTemplate + { + get + { + return _template ??= File.ReadAllText(ExcelTemplatePath); + } } + private static string _template; + + #endregion + + #region Network + + public static int SessionIdleCheckerInterval; + public static int SessionIdleCheckerTimeout; + + #endregion + +#endif } #endif \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Define.cs.meta b/Assets/GameScripts/DotNet/Core/Define.cs.meta index 1334d185..f21f54ba 100644 --- a/Assets/GameScripts/DotNet/Core/Define.cs.meta +++ b/Assets/GameScripts/DotNet/Core/Define.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: e08a4791989b16843a924bf798cdaa34 +guid: 607b7ddd55f4bc3488ce863ef43f9613 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/GameScripts/DotNet/Core/Entitas/Scene/Scene.cs b/Assets/GameScripts/DotNet/Core/Entitas/Scene/Scene.cs index 00632a2c..ed565b93 100644 --- a/Assets/GameScripts/DotNet/Core/Entitas/Scene/Scene.cs +++ b/Assets/GameScripts/DotNet/Core/Entitas/Scene/Scene.cs @@ -67,12 +67,12 @@ namespace TEngine Scenes.Add(scene); return scene; } - public void CreateSession(string remoteAddress, NetworkProtocolType networkProtocolType, Action onConnectComplete, Action onConnectFail, int connectTimeout = 5000) + public void CreateSession(string remoteAddress, NetworkProtocolType networkProtocolType, Action onConnectComplete, Action onConnectFail,Action onConnectDisconnect, int connectTimeout = 5000) { var address = NetworkHelper.ToIPEndPoint(remoteAddress); var clientNetworkComponent = GetComponent() ?? AddComponent(); clientNetworkComponent.Initialize(networkProtocolType, NetworkTarget.Outer); - clientNetworkComponent.Connect(address, onConnectComplete, onConnectFail, connectTimeout); + clientNetworkComponent.Connect(address, onConnectComplete, onConnectFail,onConnectDisconnect, connectTimeout); Session = clientNetworkComponent.Session; } #else diff --git a/Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs b/Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs deleted file mode 100644 index 9a293460..00000000 --- a/Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs +++ /dev/null @@ -1,75 +0,0 @@ -#if TENGINE_NET -namespace TEngine.Core; - -public static class ExcelDefine -{ - /// - /// 项目跟目录路径 - /// - private const string ProjectPath = "../../.."; - /// - /// 配置文件根目录 - /// - public static string ProgramPath = $"{ProjectPath}/Config/Excel/"; - /// - /// 版本文件Excel - /// - public static string ExcelVersionFile = $"{ProgramPath}Version.txt"; - /// - /// 服务器代码生成文件夹 - /// - public static string ServerFileDirectory = $"{ProjectPath}/Server/TEngine.Model/Generate/ConfigTable/Entity/"; - /// - /// 客户端代码生成文件夹 - /// - public static string ClientFileDirectory = $"{ProjectPath}/Client/Unity/Assets/Scripts/TEngine/TEngine.Model/Generate/ConfigTable/Entity/"; - /// - /// 服务器二进制数据文件夹 - /// - public static string ServerBinaryDirectory = $"{ProjectPath}/Config/Binary/"; - /// - /// 客户端二进制数据文件夹 - /// - public static string ClientBinaryDirectory = $"{ProjectPath}/Client/Unity/Assets/Bundles/Config/"; - /// - /// 服务器Json数据文件夹 - /// - public static string ServerJsonDirectory = $"{ProjectPath}/Config/Json/Server/"; - /// - /// 客户端Json数据文件夹 - /// - public static string ClientJsonDirectory = $"{ProjectPath}/Config/Json/Client/"; - /// - /// 服务器自定义导出代码 - /// - public static string ServerCustomExportDirectory = $"{ProjectPath}/Server/TEngine.Model/Generate/CustomExport/"; - /// - /// 客户端自定义导出代码 - /// - public static string ClientCustomExportDirectory = $"{ProjectPath}/Client/Unity/Assets/Scripts/TEngine/TEngine.Model/Generate/CustomExport/"; - /// - /// 导表支持的类型 - /// - public static readonly HashSet ColTypeSet = new HashSet() - { - "", "0", "bool", "byte", "short", "ushort", "int", "uint", "long", "ulong", "float", "string", "AttrConfig", - "short[]", "int[]", "long[]", "float[]", "string[]" - }; - /// - /// Excel生成代码模板的位置 - /// - public static string ExcelTemplatePath = $"{ProjectPath}/Config/Template/ExcelTemplate.txt"; - /// - /// 代码模板 - /// - public static string ExcelTemplate - { - get - { - return _template ??= File.ReadAllText(Path.Combine(Environment.CurrentDirectory, $"{ProjectPath}/Config/Template/ExcelTemplate.txt")); - } - } - private static string _template; -} - -#endif \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Exporter/Excel/ExcelExporter.cs b/Assets/GameScripts/DotNet/Core/Exporter/Excel/ExcelExporter.cs index beb6013d..895e561d 100644 --- a/Assets/GameScripts/DotNet/Core/Exporter/Excel/ExcelExporter.cs +++ b/Assets/GameScripts/DotNet/Core/Exporter/Excel/ExcelExporter.cs @@ -32,7 +32,7 @@ public sealed class ExcelExporter public ExcelExporter(ExportType exportType) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; - var versionFilePath = ExcelDefine.ExcelVersionFile; + var versionFilePath = Define.ExcelVersionFile; switch (exportType) { @@ -47,8 +47,8 @@ public sealed class ExcelExporter File.Delete(versionFilePath); } - FileHelper.ClearDirectoryFile(ExcelDefine.ServerFileDirectory); - FileHelper.ClearDirectoryFile(ExcelDefine.ClientFileDirectory); + FileHelper.ClearDirectoryFile(Define.ExcelServerFileDirectory); + FileHelper.ClearDirectoryFile(Define.ExcelClientFileDirectory); break; } } @@ -63,12 +63,12 @@ public sealed class ExcelExporter private static void CustomExport() { // 清除文件夹 - FileHelper.ClearDirectoryFile(ExcelDefine.ServerCustomExportDirectory); - FileHelper.ClearDirectoryFile(ExcelDefine.ClientCustomExportDirectory); + FileHelper.ClearDirectoryFile(Define.ServerCustomExportDirectory); + FileHelper.ClearDirectoryFile(Define.ClientCustomExportDirectory); // 找到程序集 var assemblyLoadContext = new AssemblyLoadContext("ExporterDll", true); - var dllBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, "Logic.dll")); - var pdbBytes = File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, "Logic.pdb")); + var dllBytes = File.ReadAllBytes($"{Define.CustomExportAssembly}.dll"); + var pdbBytes = File.ReadAllBytes($"{Define.CustomExportAssembly}.pdb"); var assembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes)); // 加载程序集 AssemblyManager.LoadAssembly(int.MaxValue, assembly); @@ -95,7 +95,7 @@ public sealed class ExcelExporter /// private void Find() { - var versionFilePath = ExcelDefine.ExcelVersionFile; + var versionFilePath = Define.ExcelVersionFile; if(File.Exists(versionFilePath)) { @@ -107,7 +107,7 @@ public sealed class ExcelExporter _versionDic = new Dictionary(); } - var dir = new DirectoryInfo(ExcelDefine.ProgramPath); + var dir = new DirectoryInfo(Define.ExcelProgramPath); var excelFiles = dir.GetFiles("*", SearchOption.AllDirectories); if (excelFiles.Length <= 0) @@ -256,8 +256,8 @@ public sealed class ExcelExporter continue; } - if (!ExcelDefine.ColTypeSet.Contains(serverType) || - !ExcelDefine.ColTypeSet.Contains(clientType)) + if (!Define.ColTypeSet.Contains(serverType) || + !Define.ColTypeSet.Contains(clientType)) { Exporter.LogError($"配置表 {exportInfo.Name} {col} 列 [{colName}] 客户端类型 {clientType}, 服务端类型 {serverType} 不合法"); continue; @@ -297,12 +297,12 @@ public sealed class ExcelExporter writeToClassTask.Add(Task.Run(() => { - WriteToClass(excelTable.ServerColInfos, ExcelDefine.ServerFileDirectory, true); + WriteToClass(excelTable.ServerColInfos, Define.ExcelServerFileDirectory, true); })); writeToClassTask.Add(Task.Run(() => { - WriteToClass(excelTable.ClientColInfos, ExcelDefine.ClientFileDirectory, false); + WriteToClass(excelTable.ClientColInfos, Define.ExcelClientFileDirectory, false); })); Task.WaitAll(writeToClassTask.ToArray()); @@ -385,7 +385,7 @@ public sealed class ExcelExporter } } - var template = ExcelDefine.ExcelTemplate; + var template = Define.ExcelTemplate; if (fileBuilder.Length > 0) { @@ -407,8 +407,8 @@ public sealed class ExcelExporter private void ExportToBinary() { var exportToBinaryTasks = new List(); - var dynamicServerAssembly = DynamicAssembly.Load(ExcelDefine.ServerFileDirectory); - var dynamicClientAssembly = DynamicAssembly.Load(ExcelDefine.ClientFileDirectory); + var dynamicServerAssembly = DynamicAssembly.Load(Define.ExcelServerFileDirectory); + var dynamicClientAssembly = DynamicAssembly.Load(Define.ExcelClientFileDirectory); foreach (var (tableName, tableList) in _tables) { @@ -465,7 +465,7 @@ public sealed class ExcelExporter if (serverDynamicInfo?.ConfigData != null) { var bytes = ProtoBufHelper.ToBytes(serverDynamicInfo.ConfigData); - var serverBinaryDirectory = ExcelDefine.ServerBinaryDirectory; + var serverBinaryDirectory = Define.ExcelServerBinaryDirectory; if (!Directory.Exists(serverBinaryDirectory)) { @@ -476,7 +476,7 @@ public sealed class ExcelExporter if (serverDynamicInfo.Json.Length > 0) { - var serverJsonDirectory = ExcelDefine.ServerJsonDirectory; + var serverJsonDirectory = Define.ExcelServerJsonDirectory; using var sw = new StreamWriter(Path.Combine(serverJsonDirectory, $"{csName}Data.Json")); sw.WriteLine("{\"List\":["); sw.Write(serverDynamicInfo.Json.ToString()); @@ -487,7 +487,7 @@ public sealed class ExcelExporter if (clientDynamicInfo?.ConfigData != null) { var bytes = ProtoBufHelper.ToBytes(clientDynamicInfo.ConfigData); - var clientBinaryDirectory = ExcelDefine.ClientBinaryDirectory; + var clientBinaryDirectory = Define.ExcelClientBinaryDirectory; if (!Directory.Exists(clientBinaryDirectory)) { @@ -498,7 +498,7 @@ public sealed class ExcelExporter if (clientDynamicInfo.Json.Length > 0) { - var clientJsonDirectory = ExcelDefine.ClientJsonDirectory; + var clientJsonDirectory = Define.ExcelClientJsonDirectory; using var sw = new StreamWriter(Path.Combine(clientJsonDirectory, $"{csName}Data.Json")); sw.WriteLine("{\"List\":["); sw.Write(clientDynamicInfo.Json.ToString()); diff --git a/Assets/GameScripts/DotNet/Core/Exporter/Exporter.cs b/Assets/GameScripts/DotNet/Core/Exporter/Exporter.cs index e6a28bf4..335edb4d 100644 --- a/Assets/GameScripts/DotNet/Core/Exporter/Exporter.cs +++ b/Assets/GameScripts/DotNet/Core/Exporter/Exporter.cs @@ -16,7 +16,7 @@ public sealed class Exporter public void Start() { Console.OutputEncoding = Encoding.UTF8; - var exportType = Define.Options.ExportType; + var exportType = AppDefine.Options.ExportType; if (exportType != ExportType.None) { @@ -39,7 +39,7 @@ public sealed class Exporter LogInfo(""); exportType = (ExportType) key; - LoadConfig(); + // LoadConfig(); switch (exportType) { @@ -61,50 +61,52 @@ public sealed class Exporter Environment.Exit(0); } - private void LoadConfig() - { - const string settingsName = "TEngineSettings.json"; - var currentDirectory = Directory.GetCurrentDirectory(); - - if (!File.Exists(Path.Combine(currentDirectory, settingsName))) - { - throw new FileNotFoundException($"not found {settingsName} in OutputDirectory"); - } - - var configurationRoot = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile(settingsName) - .Build(); - // ProtoBuf文件所在的位置文件夹位置 - ProtoBufDefine.ProtoBufDirectory = configurationRoot["Export:ProtoBufDirectory:Value"]; - // ProtoBuf生成到服务端的文件夹位置 - ProtoBufDefine.ServerDirectory = configurationRoot["Export:ProtoBufServerDirectory:Value"]; - // ProtoBuf生成到客户端的文件夹位置 - ProtoBufDefine.ClientDirectory = configurationRoot["Export:ProtoBufClientDirectory:Value"]; - // ProtoBuf生成代码模板的位置 - ProtoBufDefine.ProtoBufTemplatePath = configurationRoot["Export:ProtoBufTemplatePath:Value"]; - // Excel配置文件根目录 - ExcelDefine.ProgramPath = configurationRoot["Export:ExcelProgramPath:Value"]; - // Excel版本文件的位置 - ExcelDefine.ExcelVersionFile = configurationRoot["Export:ExcelVersionFile:Value"]; - // Excel生成服务器代码的文件夹位置 - ExcelDefine.ServerFileDirectory = configurationRoot["Export:ExcelServerFileDirectory:Value"]; - // Excel生成客户端代码文件夹位置 - ExcelDefine.ClientFileDirectory = configurationRoot["Export:ExcelClientFileDirectory:Value"]; - // Excel生成服务器二进制数据文件夹位置 - ExcelDefine.ServerBinaryDirectory = configurationRoot["Export:ExcelServerBinaryDirectory:Value"]; - // Excel生成客户端二进制数据文件夹位置 - ExcelDefine.ClientBinaryDirectory = configurationRoot["Export:ExcelClientBinaryDirectory:Value"]; - // Excel生成服务器Json数据文件夹位置 - ExcelDefine.ServerJsonDirectory = configurationRoot["Export:ExcelServerJsonDirectory:Value"]; - // Excel生成客户端Json数据文件夹位置 - ExcelDefine.ClientJsonDirectory = configurationRoot["Export:ExcelClientJsonDirectory:Value"]; - // Excel生成代码模板的位置 - ExcelDefine.ExcelTemplatePath = configurationRoot["Export:ExcelTemplatePath:Value"]; - // 服务器自定义导出代码文件夹位置 - ExcelDefine.ServerCustomExportDirectory = configurationRoot["Export:ServerCustomExportDirectory:Value"]; - // 客户端自定义导出代码文件夹位置 - ExcelDefine.ClientCustomExportDirectory = configurationRoot["Export:ClientCustomExportDirectory:Value"]; - } +#if old + // private void LoadConfig() + // { + // const string settingsName = "TEngineSettings.json"; + // var currentDirectory = Directory.GetCurrentDirectory(); + // + // if (!File.Exists(Path.Combine(currentDirectory, settingsName))) + // { + // throw new FileNotFoundException($"not found {settingsName} in OutputDirectory"); + // } + // + // var configurationRoot = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) + // .AddJsonFile(settingsName) + // .Build(); + // // ProtoBuf文件所在的位置文件夹位置 + // ProtoBufDefine.ProtoBufDirectory = configurationRoot["Export:ProtoBufDirectory:Value"]; + // // ProtoBuf生成到服务端的文件夹位置 + // ProtoBufDefine.ServerDirectory = configurationRoot["Export:ProtoBufServerDirectory:Value"]; + // // ProtoBuf生成到客户端的文件夹位置 + // ProtoBufDefine.ClientDirectory = configurationRoot["Export:ProtoBufClientDirectory:Value"]; + // // ProtoBuf生成代码模板的位置 + // ProtoBufDefine.ProtoBufTemplatePath = configurationRoot["Export:ProtoBufTemplatePath:Value"]; + // // Excel配置文件根目录 + // ExcelDefine.ProgramPath = configurationRoot["Export:ExcelProgramPath:Value"]; + // // Excel版本文件的位置 + // ExcelDefine.ExcelVersionFile = configurationRoot["Export:ExcelVersionFile:Value"]; + // // Excel生成服务器代码的文件夹位置 + // ExcelDefine.ServerFileDirectory = configurationRoot["Export:ExcelServerFileDirectory:Value"]; + // // Excel生成客户端代码文件夹位置 + // ExcelDefine.ClientFileDirectory = configurationRoot["Export:ExcelClientFileDirectory:Value"]; + // // Excel生成服务器二进制数据文件夹位置 + // ExcelDefine.ServerBinaryDirectory = configurationRoot["Export:ExcelServerBinaryDirectory:Value"]; + // // Excel生成客户端二进制数据文件夹位置 + // ExcelDefine.ClientBinaryDirectory = configurationRoot["Export:ExcelClientBinaryDirectory:Value"]; + // // Excel生成服务器Json数据文件夹位置 + // ExcelDefine.ServerJsonDirectory = configurationRoot["Export:ExcelServerJsonDirectory:Value"]; + // // Excel生成客户端Json数据文件夹位置 + // ExcelDefine.ClientJsonDirectory = configurationRoot["Export:ExcelClientJsonDirectory:Value"]; + // // Excel生成代码模板的位置 + // ExcelDefine.ExcelTemplatePath = configurationRoot["Export:ExcelTemplatePath:Value"]; + // // 服务器自定义导出代码文件夹位置 + // ExcelDefine.ServerCustomExportDirectory = configurationRoot["Export:ServerCustomExportDirectory:Value"]; + // // 客户端自定义导出代码文件夹位置 + // ExcelDefine.ClientCustomExportDirectory = configurationRoot["Export:ClientCustomExportDirectory:Value"]; + // } +#endif public static void LogInfo(string msg) { diff --git a/Assets/GameScripts/DotNet/Core/Exporter/Interface/ICustomExport.cs b/Assets/GameScripts/DotNet/Core/Exporter/Interface/ICustomExport.cs index 0b171cf2..5801334f 100644 --- a/Assets/GameScripts/DotNet/Core/Exporter/Interface/ICustomExport.cs +++ b/Assets/GameScripts/DotNet/Core/Exporter/Interface/ICustomExport.cs @@ -20,22 +20,22 @@ public abstract class ACustomExport : ICustomExport { case CustomExportType.Client: { - if (!Directory.Exists(ExcelDefine.ClientCustomExportDirectory)) + if (!Directory.Exists(Define.ClientCustomExportDirectory)) { - Directory.CreateDirectory(ExcelDefine.ClientCustomExportDirectory); + Directory.CreateDirectory(Define.ClientCustomExportDirectory); } - File.WriteAllText($"{ExcelDefine.ClientCustomExportDirectory}/{fileName}", fileContent); + File.WriteAllText($"{Define.ClientCustomExportDirectory}/{fileName}", fileContent); return; } case CustomExportType.Server: { - if (!Directory.Exists(ExcelDefine.ServerCustomExportDirectory)) + if (!Directory.Exists(Define.ServerCustomExportDirectory)) { - Directory.CreateDirectory(ExcelDefine.ServerCustomExportDirectory); + Directory.CreateDirectory(Define.ServerCustomExportDirectory); } - File.WriteAllText($"{ExcelDefine.ServerCustomExportDirectory}/{fileName}", fileContent); + File.WriteAllText($"{Define.ServerCustomExportDirectory}/{fileName}", fileContent); return; } } diff --git a/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs b/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs deleted file mode 100644 index 20a7a31b..00000000 --- a/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs +++ /dev/null @@ -1,28 +0,0 @@ -#if TENGINE_NET -namespace TEngine.Core; - -public static class ProtoBufDefine -{ - public static readonly char[] SplitChars = {' ', '\t'}; - /// - /// 项目跟目录路径 - /// - private const string ProjectPath = "../../.."; - /// - /// ProtoBuf文件夹 - /// - public static string ProtoBufDirectory = $"{ProjectPath}/Config/ProtoBuf/"; - /// - /// 服务端生成文件夹 - /// - public static string ServerDirectory = $"{ProjectPath}/Server/TEngine.Model/Generate/NetworkProtocol/"; - /// - /// 客户端生成文件夹 - /// - public static string ClientDirectory = $"{ProjectPath}/Client/Unity/Assets/Scripts/TEngine/TEngine.Model/Generate/NetworkProtocol/"; - /// - /// 代码模板路径 - /// - public static string ProtoBufTemplatePath = $"{ProjectPath}/Config/Template/ProtoTemplate.txt"; -} -#endif diff --git a/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufExporter.cs b/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufExporter.cs index f863ed0c..37401411 100644 --- a/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufExporter.cs +++ b/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufExporter.cs @@ -39,14 +39,14 @@ public sealed class ProtoBufExporter { Console.OutputEncoding = Encoding.UTF8; - if (!Directory.Exists(ProtoBufDefine.ServerDirectory)) + if (!Directory.Exists(Define.ProtoBufServerDirectory)) { - Directory.CreateDirectory(ProtoBufDefine.ServerDirectory); + Directory.CreateDirectory(Define.ProtoBufServerDirectory); } - if (!Directory.Exists(ProtoBufDefine.ClientDirectory)) + if (!Directory.Exists(Define.ProtoBufClientDirectory)) { - Directory.CreateDirectory(ProtoBufDefine.ClientDirectory); + Directory.CreateDirectory(Define.ProtoBufClientDirectory); } var tasks = new Task[2]; @@ -86,9 +86,9 @@ public sealed class ProtoBufExporter _aRouteRequest = Opcode.OuterRouteRequest; _aRouteResponse = Opcode.OuterRouteResponse; opCodeName = "OuterOpcode"; - protoFile = $"{ProtoBufDefine.ProtoBufDirectory}OuterMessage.proto"; - saveDirectory.Add(ProtoBufDefine.ServerDirectory, _serverTemplate); - saveDirectory.Add(ProtoBufDefine.ClientDirectory, _clientTemplate); + protoFile = $"{Define.ProtoBufDirectory}OuterMessage.proto"; + saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate); + saveDirectory.Add(Define.ProtoBufClientDirectory, _clientTemplate); break; } case ProtoBufOpCodeType.Inner: @@ -101,8 +101,8 @@ public sealed class ProtoBufExporter _aRouteRequest = Opcode.InnerRouteRequest + 1000; _aRouteResponse = Opcode.InnerRouteResponse + 1000; opCodeName = "InnerOpcode"; - protoFile = $"{ProtoBufDefine.ProtoBufDirectory}InnerMessage.proto"; - saveDirectory.Add(ProtoBufDefine.ServerDirectory, _serverTemplate); + protoFile = $"{Define.ProtoBufDirectory}InnerMessage.proto"; + saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate); break; } case ProtoBufOpCodeType.InnerBson: @@ -115,8 +115,8 @@ public sealed class ProtoBufExporter _aRouteRequest = Opcode.InnerBsonRouteRequest + 1000; _aRouteResponse = Opcode.InnerBsonRouteResponse + 1000; opCodeName = "InnerBsonOpcode"; - protoFile = $"{ProtoBufDefine.ProtoBufDirectory}InnerBsonMessage.proto"; - saveDirectory.Add(ProtoBufDefine.ServerDirectory, _serverTemplate); + protoFile = $"{Define.ProtoBufDirectory}InnerBsonMessage.proto"; + saveDirectory.Add(Define.ProtoBufServerDirectory, _serverTemplate); break; } } @@ -143,7 +143,7 @@ public sealed class ProtoBufExporter isMsgHead = true; opcodeInfo = new OpcodeInfo(); file.AppendLine("\t[ProtoContract]"); - className = currentLine.Split(ProtoBufDefine.SplitChars, StringSplitOptions.RemoveEmptyEntries)[1]; + className = currentLine.Split(Define.SplitChars, StringSplitOptions.RemoveEmptyEntries)[1]; var splits = currentLine.Split(new[] {"//"}, StringSplitOptions.RemoveEmptyEntries); if (splits.Length > 1) @@ -337,7 +337,7 @@ public sealed class ProtoBufExporter private async Task RouteType() { - var routeTypeFile = $"{ProtoBufDefine.ProtoBufDirectory}RouteType.Config"; + var routeTypeFile = $"{Define.ProtoBufDirectory}RouteType.Config"; var protoFileText = await File.ReadAllTextAsync(routeTypeFile); var routeTypeFileSb = new StringBuilder(); routeTypeFileSb.AppendLine("namespace TEngine.Core.Network\n{"); @@ -369,8 +369,8 @@ public sealed class ProtoBufExporter routeTypeFileSb.AppendLine("\t}\n}"); var file = routeTypeFileSb.ToString(); - await File.WriteAllTextAsync($"{ProtoBufDefine.ServerDirectory}RouteType.cs", file); - await File.WriteAllTextAsync($"{ProtoBufDefine.ClientDirectory}RouteType.cs", file); + await File.WriteAllTextAsync($"{Define.ProtoBufServerDirectory}RouteType.cs", file); + await File.WriteAllTextAsync($"{Define.ProtoBufClientDirectory}RouteType.cs", file); } private void Repeated(StringBuilder file, string newline) @@ -379,7 +379,7 @@ public sealed class ProtoBufExporter { var index = newline.IndexOf(";", StringComparison.Ordinal); newline = newline.Remove(index); - var property = newline.Split(ProtoBufDefine.SplitChars, StringSplitOptions.RemoveEmptyEntries); + var property = newline.Split(Define.SplitChars, StringSplitOptions.RemoveEmptyEntries); var type = property[1]; var name = property[2]; var memberIndex = int.Parse(property[4]); @@ -400,7 +400,7 @@ public sealed class ProtoBufExporter { var index = currentLine.IndexOf(";", StringComparison.Ordinal); currentLine = currentLine.Remove(index); - var property = currentLine.Split(ProtoBufDefine.SplitChars, StringSplitOptions.RemoveEmptyEntries); + var property = currentLine.Split(Define.SplitChars, StringSplitOptions.RemoveEmptyEntries); var type = property[0]; var name = property[1]; var memberIndex = int.Parse(property[3]); @@ -451,7 +451,7 @@ public sealed class ProtoBufExporter private void LoadTemplate() { - string[] lines = File.ReadAllLines(ProtoBufDefine.ProtoBufTemplatePath, Encoding.UTF8); + string[] lines = File.ReadAllLines(Define.ProtoBufTemplatePath, Encoding.UTF8); StringBuilder serverSb = new StringBuilder(); StringBuilder clientSb = new StringBuilder(); diff --git a/Assets/GameScripts/DotNet/Core/Log/LogCore.cs b/Assets/GameScripts/DotNet/Core/Log/LogCore.cs index cfa9cd8e..61e7626a 100644 --- a/Assets/GameScripts/DotNet/Core/Log/LogCore.cs +++ b/Assets/GameScripts/DotNet/Core/Log/LogCore.cs @@ -34,12 +34,12 @@ namespace TEngine private bool CheckLogLevel(int level) { - if (Define.Options == null) + if (AppDefine.Options == null) { return true; } - return Define.Options.LogLevel <= level; + return AppDefine.Options.LogLevel <= level; } public void Trace(string msg) diff --git a/Assets/GameScripts/DotNet/Core/Modules.meta b/Assets/GameScripts/DotNet/Core/Modules.meta new file mode 100644 index 00000000..bcdfb1e4 --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/Modules.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 37d561d4e0b69c2489c4f54813f740e1 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/GameScripts/DotNet/Core/Modules/Session.meta b/Assets/GameScripts/DotNet/Core/Modules/Session.meta new file mode 100644 index 00000000..19e007fb --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/Modules/Session.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 62fb06b1efb2c3e4cb58e2bc0119688d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs new file mode 100644 index 00000000..33258891 --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs @@ -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 \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs.meta b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs.meta similarity index 83% rename from Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs.meta rename to Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs.meta index 2f4333bb..cfb73c22 100644 --- a/Assets/GameScripts/DotNet/Core/Exporter/Excel/Base/ExcelDefine.cs.meta +++ b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionHeartbeatComponent.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 0dac52ee40f938641a63d624ff19dd3f +guid: a99afa40336b6ab4b8f1935689ff27c1 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs new file mode 100644 index 00000000..0d59ec77 --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs @@ -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 \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs.meta b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs.meta similarity index 83% rename from Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs.meta rename to Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs.meta index 19f76f5e..ec64559f 100644 --- a/Assets/GameScripts/DotNet/Core/Exporter/ProtoBuf/ProtoBufDefine.cs.meta +++ b/Assets/GameScripts/DotNet/Core/Modules/Session/SessionIdleCheckerComponent.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: d418e7f4b6646504c8a6ca4b381992b0 +guid: d8ecddd8867e88f409286a99647a236c MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/GameScripts/DotNet/Core/Network/Base/Interface/AClientNetwork.cs b/Assets/GameScripts/DotNet/Core/Network/Base/Interface/AClientNetwork.cs index 9b941dfa..1f6ac681 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Base/Interface/AClientNetwork.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Base/Interface/AClientNetwork.cs @@ -4,17 +4,24 @@ using System.Net; namespace TEngine.Core.Network { - public abstract class AClientNetwork : ANetwork + public abstract class AClientNetwork: ANetwork { public uint ChannelId { get; protected set; } public abstract event Action OnDispose; + public abstract event Action OnConnectFail; + public abstract event Action OnConnectComplete; + public abstract event Action OnConnectDisconnect; public abstract event Action OnChangeChannelId; public abstract event Action OnReceiveMemoryStream; - protected AClientNetwork(Scene scene, NetworkType networkType, NetworkProtocolType networkProtocolType, NetworkTarget networkTarget) : base(scene, networkType, networkProtocolType, networkTarget) { } + protected AClientNetwork(Scene scene, NetworkType networkType, NetworkProtocolType networkProtocolType, NetworkTarget networkTarget): base( + scene, networkType, networkProtocolType, networkTarget) + { + } + + public abstract uint Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail, Action onConnectDisconnect, + int connectTimeout = 5000); - public abstract uint Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail, int connectTimeout = 5000); - public override void Dispose() { ChannelId = 0; diff --git a/Assets/GameScripts/DotNet/Core/Network/Base/Interface/ANetwork.cs b/Assets/GameScripts/DotNet/Core/Network/Base/Interface/ANetwork.cs index 88351875..8247cdb9 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Base/Interface/ANetwork.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Base/Interface/ANetwork.cs @@ -1,6 +1,7 @@ using System; using System.IO; using TEngine.Core; +// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract #pragma warning disable CS8625 #pragma warning disable CS8618 @@ -26,7 +27,6 @@ namespace TEngine.Core.Network public ANetworkMessageScheduler NetworkMessageScheduler { get; protected set; } protected readonly Func Pack; - private readonly LastMessageInfo _lastMessageInfo = new LastMessageInfo(); protected ANetwork(Scene scene, NetworkType networkType, NetworkProtocolType networkProtocolType, NetworkTarget networkTarget) { @@ -69,22 +69,9 @@ namespace TEngine.Core.Network return null; } #endif - if (memoryStream != null) - { - return InnerPacketParser.Pack(rpcId, routeId, memoryStream); - } - - // 只针对服务器做缓存消息优化(例如群发消息等)、避免多次序列化 - if (ReferenceEquals(_lastMessageInfo.Message, message)) - { - _lastMessageInfo.MemoryStream.Seek(0, SeekOrigin.Begin); - return _lastMessageInfo.MemoryStream; - } - - memoryStream = InnerPacketParser.Pack(rpcId, routeId, message); - _lastMessageInfo.MemoryStream = memoryStream; - _lastMessageInfo.Message = message; - return memoryStream; + return memoryStream == null + ? InnerPacketParser.Pack(rpcId, routeId, message) + : InnerPacketParser.Pack(rpcId, routeId, memoryStream); } #endif private MemoryStream OuterPack(uint rpcId, long routeTypeOpCode, long routeId, MemoryStream memoryStream, object message) @@ -96,26 +83,9 @@ namespace TEngine.Core.Network return null; } #endif - if (memoryStream != null) - { - return OuterPacketParser.Pack(rpcId, routeTypeOpCode, memoryStream); - } - - // 只针对服务器做缓存消息优化(例如群发消息等)、避免多次序列化 - // 客户端没有群发消息的功能、一般客户端都是自己缓存消息、如果这里做了缓存反而不好了 -#if TENGINE_NET - if (ReferenceEquals(_lastMessageInfo.Message, message)) - { - _lastMessageInfo.MemoryStream.Seek(0, SeekOrigin.Begin); - return _lastMessageInfo.MemoryStream; - } -#endif - memoryStream = OuterPacketParser.Pack(rpcId, routeTypeOpCode, message); -#if TENGINE_NET - _lastMessageInfo.MemoryStream = memoryStream; - _lastMessageInfo.Message = message; -#endif - return memoryStream; + return memoryStream == null + ? OuterPacketParser.Pack(rpcId, routeTypeOpCode, message) + : OuterPacketParser.Pack(rpcId, routeTypeOpCode, memoryStream); } public abstract void Send(uint channelId, uint rpcId, long routeTypeOpCode, long routeId, object message); @@ -132,8 +102,6 @@ namespace TEngine.Core.Network NetworkType = NetworkType.None; NetworkTarget = NetworkTarget.None; NetworkProtocolType = NetworkProtocolType.None; - - _lastMessageInfo.Dispose(); } } } \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs b/Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs deleted file mode 100644 index 2b6e745f..00000000 --- a/Assets/GameScripts/DotNet/Core/Network/Base/LastMessageInfo.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.IO; -#pragma warning disable CS8625 -#pragma warning disable CS8618 - -namespace TEngine.Core.Network -{ - public class LastMessageInfo : IDisposable - { - public object Message; - public MemoryStream MemoryStream; - - public void Dispose() - { - Message = null; - MemoryStream = null; - } - } -} \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/Network/Base/Server/Server.cs b/Assets/GameScripts/DotNet/Core/Network/Base/Server/Server.cs index 3ebc02ba..b246a9aa 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Base/Server/Server.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Base/Server/Server.cs @@ -84,7 +84,7 @@ namespace TEngine clientNetworkComponent.Connect(ipEndPoint,null, () => { Log.Error($"Unable to connect to the target server sourceServerId:{Id} targetServerId:{targetServerId}"); - }); + },null); _sessions.Add(targetServerId, new ConnectInfo(clientNetworkComponent.Session, clientNetworkComponent)); return clientNetworkComponent.Session; } diff --git a/Assets/GameScripts/DotNet/Core/Network/Entity/ClientNetworkComponent.cs b/Assets/GameScripts/DotNet/Core/Network/Entity/ClientNetworkComponent.cs index 6f5adb13..7a2e6000 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Entity/ClientNetworkComponent.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Entity/ClientNetworkComponent.cs @@ -9,6 +9,7 @@ namespace TEngine.Core.Network { private AClientNetwork Network { get; set; } public Session Session { get; private set; } + private Action _onConnectDisconnect; public void Initialize(NetworkProtocolType networkProtocolType, NetworkTarget networkTarget) { @@ -31,14 +32,15 @@ namespace TEngine.Core.Network } } - public void Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail, int connectTimeout = 5000) + public void Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail,Action onConnectDisconnect, int connectTimeout = 5000) { if (Network == null || Network.IsDisposed) { throw new NotSupportedException("Network is null or isDisposed"); } - Network.Connect(remoteEndPoint, onConnectComplete, onConnectFail, connectTimeout); + _onConnectDisconnect = onConnectDisconnect; + Network.Connect(remoteEndPoint, onConnectComplete, onConnectFail, onConnectDisconnect, connectTimeout); Session = Session.Create(Network); } @@ -51,6 +53,7 @@ namespace TEngine.Core.Network } Session = null; + _onConnectDisconnect?.Invoke(); base.Dispose(); } } diff --git a/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs b/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs index 18438ba6..44c40be1 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Entity/Session/Session.cs @@ -19,7 +19,7 @@ namespace TEngine.Core.Network private static readonly Dictionary Sessions = new (); public readonly Dictionary> RequestCallback = new(); - public static void Create(ANetworkMessageScheduler networkMessageScheduler, ANetworkChannel channel) + public static void Create(ANetworkMessageScheduler networkMessageScheduler, ANetworkChannel channel, NetworkTarget networkTarget) { #if TENGINE_DEVELOP if (ThreadSynchronizationContext.Main.ThreadId != Thread.CurrentThread.ManagedThreadId) @@ -33,6 +33,14 @@ namespace TEngine.Core.Network session.NetworkMessageScheduler = networkMessageScheduler; channel.OnDispose += session.Dispose; channel.OnReceiveMemoryStream += session.OnReceive; +#if TENGINE_NET + if (networkTarget == NetworkTarget.Outer) + { + var interval = Define.SessionIdleCheckerInterval; + var timeOut = Define.SessionIdleCheckerTimeout; + session.AddComponent().Start(interval, timeOut); + } +#endif Sessions.Add(session.Id, session); } 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 d18d7ca4..46bccd54 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Message/Interface/Scheduler/ANetworkMessageScheduler.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Message/Interface/Scheduler/ANetworkMessageScheduler.cs @@ -37,6 +37,7 @@ namespace TEngine.Core.Network switch (packInfo.ProtocolCode) { + case Opcode.PingResponse: case >= Opcode.OuterRouteMessage: { await Handler(session, messageType, packInfo); diff --git a/Assets/GameScripts/DotNet/Core/Network/Message/Protocols/CoreMessageProtocols.cs b/Assets/GameScripts/DotNet/Core/Network/Message/Protocols/CoreMessageProtocols.cs index b6ff44ba..c6ae78d0 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Message/Protocols/CoreMessageProtocols.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Message/Protocols/CoreMessageProtocols.cs @@ -37,7 +37,7 @@ namespace TEngine [ProtoIgnore] public PingResponse ResponseType { get; set; } [ProtoMember(90)] public long RpcId { get; set; } } - + [ProtoContract] public class PingResponse : AProto, IResponse { public uint OpCode() diff --git a/Assets/GameScripts/DotNet/Core/Network/Message/Scheduler/ClientMessageScheduler.cs b/Assets/GameScripts/DotNet/Core/Network/Message/Scheduler/ClientMessageScheduler.cs index c8e881b5..0c04cf7d 100644 --- a/Assets/GameScripts/DotNet/Core/Network/Message/Scheduler/ClientMessageScheduler.cs +++ b/Assets/GameScripts/DotNet/Core/Network/Message/Scheduler/ClientMessageScheduler.cs @@ -17,6 +17,7 @@ namespace TEngine.Core.Network { throw new NotSupportedException($"Received unsupported message protocolCode:{packInfo.ProtocolCode} messageType:{messageType}"); } + case Opcode.PingResponse: case > Opcode.OuterRouteResponse: { // 这个一般是客户端Session.Call发送时使用的、目前这个逻辑只有Unity客户端时使用 diff --git a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Client/KCPClientNetwork.cs b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Client/KCPClientNetwork.cs index 6c057661..3f6498f7 100644 --- a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Client/KCPClientNetwork.cs +++ b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Client/KCPClientNetwork.cs @@ -19,10 +19,11 @@ namespace TEngine.Core.Network #region 逻辑线程 private bool _isInit; - private Action _onConnectFail; - private Action _onConnectComplete; private long _connectTimeoutId; public override event Action OnDispose; + public override event Action OnConnectFail; + public override event Action OnConnectComplete; + public override event Action OnConnectDisconnect; public override event Action OnChangeChannelId; public override event Action OnReceiveMemoryStream; @@ -50,6 +51,11 @@ namespace TEngine.Core.Network if (_socket.Connected) { + if (OnConnectDisconnect != null) + { + ThreadSynchronizationContext.Main.Post(OnConnectDisconnect); + } + _socket.Disconnect(false); _socket.Close(); } @@ -58,7 +64,6 @@ namespace TEngine.Core.Network _updateMinTime = 0; _sendAction = null; - _onConnectFail = null; _rawSendBuffer = null; _rawReceiveBuffer = null; @@ -89,7 +94,7 @@ namespace TEngine.Core.Network }); } - public override uint Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail, int connectTimeout = 5000) + public override uint Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail, Action onConnectDisconnect, int connectTimeout = 5000) { if (_isInit) { @@ -97,8 +102,9 @@ namespace TEngine.Core.Network } _isInit = true; - _onConnectFail = onConnectFail; - _onConnectComplete = onConnectComplete; + OnConnectFail = onConnectFail; + OnConnectComplete = onConnectComplete; + OnConnectDisconnect = onConnectDisconnect; ChannelId = CreateChannelId(); _kcpSettings = KCPSettings.Create(NetworkTarget); _maxSndWnd = _kcpSettings.MaxSendWindowSize; @@ -119,11 +125,11 @@ namespace TEngine.Core.Network _connectTimeoutId = TimerScheduler.Instance.Core.OnceTimer(connectTimeout, () => { - if (_onConnectFail == null) + if (OnConnectFail == null) { return; } - _onConnectFail(); + OnConnectFail(); Dispose(); }); @@ -247,7 +253,7 @@ namespace TEngine.Core.Network ThreadSynchronizationContext.Main.Post(() => { OnChangeChannelId(ChannelId); - _onConnectComplete?.Invoke(); + OnConnectComplete?.Invoke(); }); // 到这里正确创建上连接了、可以正常发送消息了 break; diff --git a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetwork.cs b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetwork.cs index 3523e140..0439e4d0 100644 --- a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetwork.cs +++ b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetwork.cs @@ -170,7 +170,6 @@ namespace TEngine.Core.Network try { var receiveLength = _socket.ReceiveFrom(_rawReceiveBuffer, ref _clientEndPoint); - // Log.Debug($"_socket.ReceiveFrom receiveLength:{receiveLength}"); if (receiveLength < 1) { continue; @@ -223,7 +222,6 @@ namespace TEngine.Core.Network } case KcpHeader.ConfirmConnection: { - // Log.Debug("KcpHeader.ConfirmConnection"); if (receiveLength != 5) { break; diff --git a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetworkChannel.cs b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetworkChannel.cs index 350af147..1e411dbd 100644 --- a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetworkChannel.cs +++ b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/KCP/Server/KCPServerNetworkChannel.cs @@ -97,7 +97,7 @@ namespace TEngine.Core.Network return; } - Session.Create(networkMessageScheduler, this); + Session.Create(networkMessageScheduler, this, networkTarget); }); } diff --git a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Client/TCPClientNetwork.cs b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Client/TCPClientNetwork.cs index 5e20da07..b88c2c9d 100644 --- a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Client/TCPClientNetwork.cs +++ b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Client/TCPClientNetwork.cs @@ -18,10 +18,11 @@ namespace TEngine.Core.Network #region 逻辑线程 private bool _isInit; - private Action _onConnectFail; - private Action _onConnectComplete; private long _connectTimeoutId; public override event Action OnDispose; + public override event Action OnConnectFail; + public override event Action OnConnectComplete; + public override event Action OnConnectDisconnect; public override event Action OnChangeChannelId = channelId => { }; public override event Action OnReceiveMemoryStream; @@ -30,7 +31,7 @@ namespace TEngine.Core.Network NetworkThread.Instance.AddNetwork(this); } - public override uint Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail, int connectTimeout = 5000) + public override uint Connect(IPEndPoint remoteEndPoint, Action onConnectComplete, Action onConnectFail,Action onConnectDisconnect,int connectTimeout = 5000) { if (_isInit) { @@ -38,8 +39,8 @@ namespace TEngine.Core.Network } _isInit = true; - _onConnectFail = onConnectFail; - _onConnectComplete = onConnectComplete; + OnConnectFail = onConnectFail; + OnConnectComplete = onConnectComplete; ChannelId = 0xC0000000 | (uint) new Random().Next(); _sendAction = (rpcId, routeTypeOpCode, routeId, memoryStream, message) => @@ -66,7 +67,7 @@ namespace TEngine.Core.Network _connectTimeoutId = TimerScheduler.Instance.Core.OnceTimer(connectTimeout, () => { - _onConnectFail?.Invoke(); + OnConnectFail?.Invoke(); Dispose(); }); @@ -87,7 +88,7 @@ namespace TEngine.Core.Network return; } - OnConnectComplete(outArgs); + OnNetworkConnectComplete(outArgs); }); return ChannelId; @@ -106,6 +107,10 @@ namespace TEngine.Core.Network { if (_socket.Connected) { + if (OnConnectDisconnect != null) + { + ThreadSynchronizationContext.Main.Post(OnConnectDisconnect); + } _socket.Disconnect(true); _socket.Close(); } @@ -145,7 +150,7 @@ namespace TEngine.Core.Network private readonly SocketAsyncEventArgs _innArgs = new SocketAsyncEventArgs(); private Queue _messageCache = new Queue(); - private void OnConnectComplete(SocketAsyncEventArgs asyncEventArgs) + private void OnNetworkConnectComplete(SocketAsyncEventArgs asyncEventArgs) { #if TENGINE_DEVELOP if (NetworkThread.Instance.ManagedThreadId != Thread.CurrentThread.ManagedThreadId) @@ -163,9 +168,9 @@ namespace TEngine.Core.Network { Log.Error($"Unable to connect to the target server asyncEventArgs:{asyncEventArgs.SocketError}"); - if (_onConnectFail != null) + if (OnConnectFail != null) { - ThreadSynchronizationContext.Main.Post(_onConnectFail); + ThreadSynchronizationContext.Main.Post(OnConnectFail); } Dispose(); @@ -199,9 +204,9 @@ namespace TEngine.Core.Network _messageCache.Clear(); _messageCache = null; - if (_onConnectComplete != null) + if (OnConnectComplete != null) { - ThreadSynchronizationContext.Main.Post(_onConnectComplete); + ThreadSynchronizationContext.Main.Post(OnConnectComplete); } } @@ -509,7 +514,7 @@ namespace TEngine.Core.Network { case SocketAsyncOperation.Connect: { - NetworkThread.Instance.SynchronizationContext.Post(() => OnConnectComplete(asyncEventArgs)); + NetworkThread.Instance.SynchronizationContext.Post(() => OnNetworkConnectComplete(asyncEventArgs)); break; } case SocketAsyncOperation.Receive: diff --git a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Server/TCPServerNetwork.cs b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Server/TCPServerNetwork.cs index a0e61362..c6f4b35f 100644 --- a/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Server/TCPServerNetwork.cs +++ b/Assets/GameScripts/DotNet/Core/Network/NetworkProtocol/TCP/Server/TCPServerNetwork.cs @@ -173,7 +173,7 @@ namespace TEngine.Core.Network return; } - Session.Create(NetworkMessageScheduler, channel); + Session.Create(NetworkMessageScheduler, channel, this.NetworkTarget); }); _connectionChannel.Add(channelId, channel); diff --git a/Assets/GameScripts/DotNet/Core/Network/PacketParser/OuterPacketParser.cs b/Assets/GameScripts/DotNet/Core/Network/PacketParser/OuterPacketParser.cs index 745f62df..31030c0c 100644 --- a/Assets/GameScripts/DotNet/Core/Network/PacketParser/OuterPacketParser.cs +++ b/Assets/GameScripts/DotNet/Core/Network/PacketParser/OuterPacketParser.cs @@ -141,6 +141,10 @@ namespace TEngine.Core.Network } #endif packInfo = OuterPackInfo.Create(); + if (packInfo == null) + { + return null; + } packInfo.ProtocolCode = BitConverter.ToUInt32(_messageHead, Packet.PacketLength); packInfo.RpcId = BitConverter.ToUInt32(_messageHead, Packet.OuterPacketRpcIdLocation); packInfo.RouteTypeCode = BitConverter.ToUInt16(_messageHead, Packet.OuterPacketRouteTypeOpCodeLocation); diff --git a/Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs b/Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs new file mode 100644 index 00000000..13714034 --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs @@ -0,0 +1,81 @@ +#if TENGINE_NET +using Microsoft.Extensions.Configuration; +#pragma warning disable CS8604 +#pragma warning disable CS8601 +#pragma warning disable CS8618 + +namespace TEngine.Core; + +public static class TEngineSettingsHelper +{ + public static void Initialize() + { + const string settingsName = "TEngineSettings.json"; + var currentDirectory = Directory.GetCurrentDirectory(); + + if (!File.Exists(Path.Combine(currentDirectory, settingsName))) + { + throw new FileNotFoundException($"not found {settingsName} in OutputDirectory"); + } + + var configurationRoot = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(settingsName).Build(); + // 加载网络配置 + LoadNetworkConfig(configurationRoot); + // 加载ProtoBuf配置 + LoadProtoConfig(configurationRoot); + // 加载Excel配置 + LoadExcelConfig(configurationRoot); + } + + private static void LoadNetworkConfig(IConfigurationRoot root) + { + Define.SessionIdleCheckerInterval = Convert.ToInt32(root["Network:SessionIdleCheckerInterval:Value"]); + Define.SessionIdleCheckerTimeout = Convert.ToInt32(root["Network:SessionIdleCheckerTimeout:Value"]); + } + + private static void LoadProtoConfig(IConfigurationRoot root) + { + // ProtoBuf文件所在的位置文件夹位置 + Define.ProtoBufDirectory = root["Export:ProtoBufDirectory:Value"].GetFullPath(); + // ProtoBuf生成到服务端的文件夹位置 + Define.ProtoBufServerDirectory = root["Export:ProtoBufServerDirectory:Value"].GetFullPath(); + // ProtoBuf生成到客户端的文件夹位置 + Define.ProtoBufClientDirectory = root["Export:ProtoBufClientDirectory:Value"].GetFullPath(); + // ProtoBuf生成代码模板的位置 + Define.ProtoBufTemplatePath = root["Export:ProtoBufTemplatePath:Value"].GetFullPath(); + } + + private static void LoadExcelConfig(IConfigurationRoot root) + { + // Excel配置文件根目录 + Define.ExcelProgramPath = root["Export:ExcelProgramPath:Value"].GetFullPath(); + // Excel版本文件的位置 + Define.ExcelVersionFile = root["Export:ExcelVersionFile:Value"].GetFullPath(); + // Excel生成服务器代码的文件夹位置 + Define.ExcelServerFileDirectory = root["Export:ExcelServerFileDirectory:Value"].GetFullPath(); + // Excel生成客户端代码文件夹位置 + Define.ExcelClientFileDirectory = root["Export:ExcelClientFileDirectory:Value"].GetFullPath(); + // Excel生成服务器二进制数据文件夹位置 + Define.ExcelServerBinaryDirectory = root["Export:ExcelServerBinaryDirectory:Value"].GetFullPath(); + // Excel生成客户端二进制数据文件夹位置 + Define.ExcelClientBinaryDirectory = root["Export:ExcelClientBinaryDirectory:Value"].GetFullPath(); + // Excel生成服务器Json数据文件夹位置 + Define.ExcelServerJsonDirectory = root["Export:ExcelServerJsonDirectory:Value"].GetFullPath(); + // Excel生成客户端Json数据文件夹位置 + Define.ExcelClientJsonDirectory = root["Export:ExcelClientJsonDirectory:Value"].GetFullPath(); + // Excel生成代码模板的位置 + Define.ExcelTemplatePath = root["Export:ExcelTemplatePath:Value"].GetFullPath(); + // 服务器自定义导出代码文件夹位置 + Define.ServerCustomExportDirectory = root["Export:ServerCustomExportDirectory:Value"].GetFullPath(); + // 客户端自定义导出代码 + Define.ClientCustomExportDirectory = root["Export:ClientCustomExportDirectory:Value"].GetFullPath(); + // 自定义导出代码存放的程序集 + Define.CustomExportAssembly = root["Export:CustomExportAssembly:Value"].GetFullPath(); + } + + private static string GetFullPath(this string relativePath) + { + return Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePath)); + } +} +#endif \ No newline at end of file diff --git a/Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs.meta b/Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs.meta new file mode 100644 index 00000000..b25e9c01 --- /dev/null +++ b/Assets/GameScripts/DotNet/Core/TEngineSettingsHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4070f130fd645b9459fe147291285436 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs b/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs index bbb8447c..4d5f9e8c 100644 --- a/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs +++ b/Assets/GameScripts/HotFix/GameLogic/DataCenter/GameClient.cs @@ -79,7 +79,7 @@ namespace GameLogic if (Scene.Session == null || Scene.Session.IsDisposed) { - Scene.CreateSession(address, ProtocolType, OnConnectComplete, OnConnectFail); + Scene.CreateSession(address, ProtocolType, OnConnectComplete, OnConnectFail, OnConnectDisconnect); } } @@ -95,6 +95,12 @@ namespace GameLogic Log.Warning("Could not connect to server"); } + private void OnConnectDisconnect() + { + Status = GameClientStatus.StatusClose; + Log.Warning("OnConnectDisconnect server"); + } + public virtual void Send(object message, uint rpcId = 0, long routeId = 0) { if (Scene.Session == null) diff --git a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/Examples/Network/UIEntry.cs b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/Examples/Network/UIEntry.cs index 40f375bd..be264e19 100644 --- a/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/Examples/Network/UIEntry.cs +++ b/Assets/GameScripts/HotFix/GameLogic/NetWorkDemo/Examples/Network/UIEntry.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using TEngine.Core.Network; using TEngine.Core; +using TEngine.Logic; using UnityEngine; using UnityEngine.UI; @@ -62,7 +63,7 @@ namespace TEngine.Demo // onConnectComplete:当跟服务器建立连接后的回调 // onConnectFail:当网络无法连接或出错时的回调 // connectTimeout:连接超时时间、默认是5000毫秒 - Scene.CreateSession("127.0.0.1:20000", NetworkProtocolType.KCP, OnConnectComplete, OnConnectFail); + Scene.CreateSession("127.0.0.1:20000", NetworkProtocolType.KCP, OnConnectComplete, OnConnectFail, OnConnectDisconnect); // 注意:框架使用的ProtoBuf协议、文件定义的位置在Demo下Config/ProtoBuf/里 // 生成协议是在服务器工程生成 // ProtoBuf有三个文件: @@ -74,6 +75,7 @@ namespace TEngine.Demo private void OnConnectComplete() { IsConnect = true; + Scene.Session.AddComponent().Start(15); LogDebug("已连接到服务器"); } @@ -82,6 +84,12 @@ namespace TEngine.Demo IsConnect = false; LogError("无法连接到服务器"); } + + private void OnConnectDisconnect() + { + IsConnect = false; + LogError("服务器主动断开了连接"); + } private void OnSendButtonClick() { diff --git a/DotNet/App/TEngineSettings.json b/DotNet/App/TEngineSettings.json index 2bab0b6b..24c9ccf0 100644 --- a/DotNet/App/TEngineSettings.json +++ b/DotNet/App/TEngineSettings.json @@ -59,6 +59,20 @@ "ClientCustomExportDirectory": { "Value": "../../Client/Unity/Assets/Scripts/Hotfix/Generate/CustomExport/", "Comment": "客户端自定义导出代码文件夹位置" + }, + "CustomExportAssembly": { + "Value": "Logic", + "Comment": "自定义导出代码存放的程序集" + } + }, + "Network": { + "SessionIdleCheckerInterval": { + "Value": 10000, + "Comment": "每隔多久检查一个Session的对话时间" + }, + "SessionIdleCheckerTimeout": { + "Value": 30000, + "Comment": "距上一次接收对话的时间如果超过设定的时间会自定断开Session" } } } \ No newline at end of file