Quellcode durchsuchen

1.去掉NetThreadComponent
2.增加单线程网络模式,主要目的是方便大家做消息0GC处理,多线程不好做0GC处理
3.开关宏是SINGLE_THREAD,客户端默认使用单线程网络
4.单独的服务端仍然是多线程网络

tanghai vor 2 Jahren
Ursprung
Commit
ef48eddc15

+ 0 - 1
Unity/Assets/Resources/GlobalConfig.asset

@@ -12,5 +12,4 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 36527db572638af47b03c805671cba75, type: 3}
   m_Name: GlobalConfig
   m_EditorClassIdentifier: 
-  LoadMode: 1
   CodeMode: 3

+ 0 - 1
Unity/Assets/Scripts/Codes/Hotfix/Share/Demo/EntryEvent1_InitShare.cs

@@ -5,7 +5,6 @@ namespace ET
     {
         protected override async ETTask Run(Scene scene, EventType.EntryEvent1 args)
         {
-            Root.Instance.Scene.AddComponent<NetThreadComponent>();
             Root.Instance.Scene.AddComponent<OpcodeTypeComponent>();
             Root.Instance.Scene.AddComponent<MessageDispatcherComponent>();
             Root.Instance.Scene.AddComponent<NumericWatcherComponent>();

+ 0 - 58
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/NetThreadComponentSystem.cs

@@ -1,58 +0,0 @@
-using System;
-using System.Threading;
-
-namespace ET
-{
-    [FriendOf(typeof(NetThreadComponent))]
-    public static class NetThreadComponentSystem
-    {
-        [ObjectSystem]
-        public class AwakeSystem: AwakeSystem<NetThreadComponent>
-        {
-            protected override void Awake(NetThreadComponent self)
-            {
-                NetThreadComponent.Instance = self;
-
-                // 网络线程
-                self.thread = new Thread(self.NetThreadUpdate);
-                self.thread.Start();
-            }
-        }
-        
-        [ObjectSystem]
-        public class LateUpdateSystem: LateUpdateSystem<NetThreadComponent>
-        {
-            protected override void LateUpdate(NetThreadComponent self)
-            {
-                self.MainThreadUpdate();
-            }
-        }
-        
-        [ObjectSystem]
-        public class DestroySystem: DestroySystem<NetThreadComponent>
-        {
-            protected override void Destroy(NetThreadComponent self)
-            {
-                NetThreadComponent.Instance = null;
-                self.isStop = true;
-                self.thread.Join(1000);
-            }
-        }
-
-        // 主线程Update
-        private static void MainThreadUpdate(this NetThreadComponent self)
-        {
-            NetServices.Instance.UpdateInMainThread();
-        }
-
-        // 网络线程Update
-        private static void NetThreadUpdate(this NetThreadComponent self)
-        {
-            while (!self.isStop)
-            {
-                NetServices.Instance.UpdateInNetThread();
-                Thread.Sleep(1);
-            }
-        }
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Codes/Hotfix/Share/Module/Message/NetThreadComponentSystem.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 4b3919fcf71fdf3418d4ae252ddc4a9a
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 14
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/NetThreadComponent.cs

@@ -1,14 +0,0 @@
-using System.Threading;
-
-namespace ET
-{
-    [ComponentOf(typeof(Scene))]
-    public class NetThreadComponent: Entity, IAwake, ILateUpdate, IDestroy
-    {
-        [StaticField]
-        public static NetThreadComponent Instance;
-        
-        public Thread thread;
-        public bool isStop;
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Codes/Model/Share/Module/Message/NetThreadComponent.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: ba15d59c1a9268042a18fc3d9863b672
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 43 - 3
Unity/Assets/Scripts/Core/Module/Network/NetServices.cs

@@ -3,6 +3,7 @@ using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.IO;
 using System.Net;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace ET
@@ -37,7 +38,7 @@ namespace ET
         public object Object; // 参数
     }
 
-    public class NetServices: Singleton<NetServices>
+    public class NetServices: Singleton<NetServices>, ISingletonUpdate
     {
         private readonly ConcurrentQueue<NetOperator> netThreadOperators = new ConcurrentQueue<NetOperator>();
         private readonly ConcurrentQueue<NetOperator> mainThreadOperators = new ConcurrentQueue<NetOperator>();
@@ -61,6 +62,21 @@ namespace ET
 
                 this.typeOpcode.Add(type, messageAttribute.Opcode);
             }
+            
+#if !SINGLE_THREAD
+            // 网络线程
+            this.thread = new Thread(this.NetThreadUpdate);
+            this.thread.Start();
+#endif
+        }
+
+        public void Destroy()
+        {
+            
+#if !SINGLE_THREAD
+            this.isStop = true;            
+            this.thread.Join(1000);
+#endif
         }
 
 #region 线程安全
@@ -151,7 +167,7 @@ namespace ET
             this.errorCallback.Add(serviceId, action);
         }
         
-        public void UpdateInMainThread()
+        private void UpdateInMainThread()
         {
             while (true)
             {
@@ -210,6 +226,7 @@ namespace ET
         private readonly Dictionary<int, AService> services = new Dictionary<int, AService>();
         private readonly Queue<int> queue = new Queue<int>();
         
+        
         private void Add(AService aService)
         {
             this.services[aService.Id] = aService;
@@ -231,6 +248,22 @@ namespace ET
             }
         }
 
+#if !SINGLE_THREAD
+        
+        private bool isStop;
+        private readonly Thread thread;
+        
+        // 网络线程Update
+        private void NetThreadUpdate()
+        {
+            while (!this.isStop)
+            {
+                this.UpdateInNetThread();
+                Thread.Sleep(1);
+            }
+        }
+#endif
+
         private void RunNetThreadOperator()
         {
             while (true)
@@ -321,7 +354,7 @@ namespace ET
             }
         }
         
-        public void UpdateInNetThread()
+        private void UpdateInNetThread()
         {
             int count = this.queue.Count;
             while (count-- > 0)
@@ -379,5 +412,12 @@ namespace ET
 
 #endregion
 
+        public void Update()
+        {
+#if SINGLE_THREAD
+            UpdateInNetThread();
+#endif
+            UpdateInMainThread();
+        }
     }
 }

+ 6 - 6
Unity/ProjectSettings/ProjectSettings.asset

@@ -839,11 +839,11 @@ PlayerSettings:
   webGLDecompressionFallback: 0
   webGLPowerPreference: 2
   scriptingDefineSymbols:
-    Android: NETSTANDARD2_0;UNITY
-    Server: NETSTANDARD2_0;UNITY
-    Standalone: NETSTANDARD2_0;UNITY
-    WebGL: NETSTANDARD2_0;UNITY
-    iPhone: NETSTANDARD2_0;UNITY
+    Android: UNITY;SINGLE_THREAD
+    Server: UNITY
+    Standalone: UNITY;SINGLE_THREAD
+    WebGL: UNITY
+    iPhone: UNITY;SINGLE_THREAD
   additionalCompilerArguments: {}
   platformArchitecture:
     iPhone: 1
@@ -859,7 +859,7 @@ PlayerSettings:
   allowUnsafeCode: 1
   useDeterministicCompilation: 1
   enableRoslynAnalyzers: 1
-  selectedPlatform: 0
+  selectedPlatform: 3
   additionalIl2CppArgs: 
   scriptingRuntimeVersion: 1
   gcIncremental: 0