Procházet zdrojové kódy

1.修复一个ThreadPoolScheduler的bug
2.Fiber的创建改成异步的,原因是创建的分发一定要在Fiber的线程执行,保证线程安全

tanghai před 2 roky
rodič
revize
b79be4f22b
26 změnil soubory, kde provedl 237 přidání a 186 odebrání
  1. 35 19
      Unity/Assets/Scripts/Core/Fiber/Fiber.cs
  2. 1 6
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockComponent.cs
  3. 4 1
      Unity/Assets/Scripts/Core/Fiber/Module/Timer/TimerComponent.cs
  4. 6 6
      Unity/Assets/Scripts/Core/World/Module/EventSystem/EventSystem.cs
  5. 24 19
      Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs
  6. 2 2
      Unity/Assets/Scripts/Core/World/Module/Fiber/MainThreadScheduler.cs
  7. 25 36
      Unity/Assets/Scripts/Core/World/Module/Fiber/ThreadPoolScheduler.cs
  8. 19 26
      Unity/Assets/Scripts/Core/World/Module/Fiber/ThreadScheduler.cs
  9. 12 1
      Unity/Assets/Scripts/Core/World/World.cs
  10. 6 1
      Unity/Assets/Scripts/Editor/ComponentViewEditor/TypeDrawer/EntityRefTypeDrawer.cs
  11. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs
  12. 6 3
      Unity/Assets/Scripts/Hotfix/Server/Demo/Gate/FiberInit_Gate.cs
  13. 10 6
      Unity/Assets/Scripts/Hotfix/Server/Demo/Realm/FiberInit_Realm.cs
  14. 7 3
      Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/FiberInit_Map.cs
  15. 1 1
      Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/Match2Map_GetRoomHandler.cs
  16. 7 3
      Unity/Assets/Scripts/Hotfix/Server/LockStep/Match/FiberInit_Match.cs
  17. 7 3
      Unity/Assets/Scripts/Hotfix/Server/LockStep/Room/FiberInit_RoomRoot.cs
  18. 7 3
      Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/FiberInit_Location.cs
  19. 22 21
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentSystem.cs
  20. 7 3
      Unity/Assets/Scripts/Hotfix/Server/Module/NetInner/FiberInit_Net.cs
  21. 7 3
      Unity/Assets/Scripts/Hotfix/Server/Module/Router/FiberInit_Router.cs
  22. 7 3
      Unity/Assets/Scripts/Hotfix/Server/Module/Router/FiberInit_RouterManager.cs
  23. 9 12
      Unity/Assets/Scripts/Hotfix/Share/FiberInit_Main.cs
  24. 1 1
      Unity/Assets/Scripts/Model/Server/Module/Message/NetInnerComponent.cs
  25. 3 2
      Unity/Assets/Scripts/Model/Share/Entry.cs
  26. 1 1
      Unity/ProjectSettings/ProjectSettings.asset

+ 35 - 19
Unity/Assets/Scripts/Core/Fiber/Fiber.cs

@@ -33,17 +33,15 @@ namespace ET
         
         public int Process { get; }
         
-        public EntitySystem EntitySystem { get; }
-        public TimeInfo TimeInfo { get; }
-        public IdGenerater IdGenerater { get; }
-        public Mailboxes Mailboxes { get; }
-        public ThreadSynchronizationContext ThreadSynchronizationContext { get; }
-        public TimerComponent TimerComponent { get; }
-        public CoroutineLockComponent CoroutineLockComponent { get; }
+        public EntitySystem EntitySystem { get; private set; }
+        public TimeInfo TimeInfo { get; private set; }
+        public IdGenerater IdGenerater { get; private set; }
+        public Mailboxes Mailboxes { get; private set; }
+        public ThreadSynchronizationContext ThreadSynchronizationContext { get; private set; }
+        public TimerComponent TimerComponent { get; private set; }
+        public CoroutineLockComponent CoroutineLockComponent { get; private set; }
 
         private readonly Queue<ETTask> frameFinishTasks = new();
-
-        public bool IsRuning;
         
         public Fiber(int id, int process, int zone, SceneType sceneType, string name)
         {
@@ -62,19 +60,31 @@ namespace ET
 
         public void Update()
         {
-            this.IsRuning = true;
-            this.ThreadSynchronizationContext.Update();
-            this.TimeInfo.Update();
-            this.EntitySystem.Update();
-            this.TimerComponent.Update();
-            this.CoroutineLockComponent.Update();
+            try
+            {
+                this.ThreadSynchronizationContext.Update();
+                this.TimeInfo.Update();
+                this.EntitySystem.Update();
+                this.TimerComponent.Update();
+                this.CoroutineLockComponent.Update();
+            }
+            catch (Exception e)
+            {
+                Log.Error(e);
+            }
         }
         
         public void LateUpdate()
         {
-            this.EntitySystem.LateUpdate();
-            FrameFinishUpdate();
-            this.IsRuning = false;
+            try
+            {
+                this.EntitySystem.LateUpdate();
+                FrameFinishUpdate();
+            }
+            catch (Exception e)
+            {
+                Log.Error(e);
+            }
         }
 
         public async ETTask WaitFrameFinish()
@@ -100,8 +110,14 @@ namespace ET
                 return;
             }
             this.IsDisposed = true;
-            this.Id = 0;
+            
             this.Root.Dispose();
+            //this.ThreadSynchronizationContext = null;
+            //this.CoroutineLockComponent = null;
+            //this.TimerComponent = null;
+            //this.IdGenerater = null;
+            //this.TimeInfo = null;
+            //this.EntitySystem = null;
         }
     }
 }

+ 1 - 6
Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockComponent.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class CoroutineLockComponent: IDisposable
+    public class CoroutineLockComponent
     {
         public readonly TimerComponent TimerComponent;
         private readonly Dictionary<int, CoroutineLockQueueType> dictionary = new();
@@ -14,11 +14,6 @@ namespace ET
             this.TimerComponent = timerComponent;
         }
 
-        public void Dispose()
-        {
-            this.nextFrameRun.Clear();
-        }
-
         public void Update()
         {
             // 循环过程中会有对象继续加入队列

+ 4 - 1
Unity/Assets/Scripts/Core/Fiber/Module/Timer/TimerComponent.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 
 namespace ET
 {
@@ -55,6 +56,8 @@ namespace ET
 
     public class TimerComponent
     {
+        private bool IsDisposed;
+        
         public readonly TimeInfo TimeInfo;
         /// <summary>
         /// key: time, value: timer id

+ 6 - 6
Unity/Assets/Scripts/Core/World/Module/EventSystem/EventSystem.cs

@@ -200,17 +200,17 @@ namespace ET
         {
             if (!this.allInvokes.TryGetValue(typeof(A), out var invokeHandlers))
             {
-                throw new Exception($"Invoke error: {typeof(A).Name}");
+                throw new Exception($"Invoke error1: {type} {typeof(A).FullName}");
             }
             if (!invokeHandlers.TryGetValue(type, out var invokeHandler))
             {
-                throw new Exception($"Invoke error: {typeof(A).Name} {type}");
+                throw new Exception($"Invoke error2: {type} {typeof(A).FullName}");
             }
 
             var aInvokeHandler = invokeHandler as AInvokeHandler<A>;
             if (aInvokeHandler == null)
             {
-                throw new Exception($"Invoke error, not AInvokeHandler: {typeof(A).Name} {type}");
+                throw new Exception($"Invoke error3, not AInvokeHandler: {type} {typeof(A).FullName}");
             }
             
             aInvokeHandler.Handle(args);
@@ -220,18 +220,18 @@ namespace ET
         {
             if (!this.allInvokes.TryGetValue(typeof(A), out var invokeHandlers))
             {
-                throw new Exception($"Invoke error: {typeof(A).Name}");
+                throw new Exception($"Invoke error4: {type} {typeof(A).FullName}");
             }
             
             if (!invokeHandlers.TryGetValue(type, out var invokeHandler))
             {
-                throw new Exception($"Invoke error: {typeof(A).Name} {type}");
+                throw new Exception($"Invoke error5: {type} {typeof(A).FullName}");
             }
 
             var aInvokeHandler = invokeHandler as AInvokeHandler<A, T>;
             if (aInvokeHandler == null)
             {
-                throw new Exception($"Invoke error, not AInvokeHandler: {typeof(T).Name} {type}");
+                throw new Exception($"Invoke error6, not AInvokeHandler: {type} {typeof(A).FullName} {typeof(T).FullName} ");
             }
             
             return aInvokeHandler.Handle(args);

+ 24 - 19
Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs

@@ -2,6 +2,7 @@
 using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Threading;
+using System.Threading.Tasks;
 
 namespace ET
 {
@@ -17,7 +18,7 @@ namespace ET
         private readonly IScheduler[] schedulers = new IScheduler[3];
         
         private int idGenerator = 10000000; // 10000000以下为保留的用于StartSceneConfig的fiber id, 1个区配置1000个纤程,可以配置10000个区
-        private readonly ConcurrentDictionary<int, Fiber> fibers = new();
+        private ConcurrentDictionary<int, Fiber> fibers = new();
 
         private MainThreadScheduler mainThreadScheduler;
         
@@ -56,31 +57,35 @@ namespace ET
             {
                 kv.Value.Dispose();
             }
+
+            this.fibers = null;
         }
 
-        public int Create(SchedulerType schedulerType, int fiberId, int zone, SceneType sceneType, string name)
+        public async Task<int> Create(SchedulerType schedulerType, int fiberId, int zone, SceneType sceneType, string name)
         {
             try
             {
                 Fiber fiber = new(fiberId, Options.Instance.Process, zone, sceneType, name);
-
-                fiber.Root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
-                fiber.Root.AddComponent<ActorSenderComponent>();
-                fiber.Root.AddComponent<ActorRecverComponent>();
-            
-                // 这里要换成Fiber的ThreadSynchronizationContext,因为在FiberInit中可能存在调用第三方的Task,需要回调到这个Fiber中来
-                SynchronizationContext old = SynchronizationContext.Current;
-                SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
-                
-                // 根据Fiber的SceneType分发Init
-                EventSystem.Instance.Invoke((long)sceneType, new FiberInit() {Fiber = fiber});
-                
-                SynchronizationContext.SetSynchronizationContext(old);
-                
                 this.fibers[fiber.Id] = fiber;
-                
                 this.schedulers[(int) schedulerType].Add(fiberId);
                 
+                TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
+
+                fiber.ThreadSynchronizationContext.Post(async () =>
+                {
+                    try
+                    {
+                        // 根据Fiber的SceneType分发Init,必须在Fiber线程中执行
+                        await EventSystem.Instance.Invoke<FiberInit, ETTask>((long)sceneType, new FiberInit() {Fiber = fiber});
+                        tcs.SetResult(true);
+                    }
+                    catch (Exception e)
+                    {
+                        Log.Error(e);
+                    }
+                });
+
+                await tcs.Task;
                 return fiberId;
             }
             catch (Exception e)
@@ -89,10 +94,10 @@ namespace ET
             }
         }
         
-        public int Create(SchedulerType schedulerType, int zone, SceneType sceneType, string name)
+        public async Task<int> Create(SchedulerType schedulerType, int zone, SceneType sceneType, string name)
         {
             int fiberId = Interlocked.Increment(ref this.idGenerator);
-            return this.Create(schedulerType, fiberId, zone, sceneType, name);
+            return await this.Create(schedulerType, fiberId, zone, sceneType, name);
         }
         
         public void Remove(int id)

+ 2 - 2
Unity/Assets/Scripts/Core/World/Module/Fiber/MainThreadScheduler.cs

@@ -42,10 +42,10 @@ namespace ET
                     continue;
                 }
 
-                this.idQueue.Enqueue(id);
-                
                 SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
                 fiber.Update();
+                
+                this.idQueue.Enqueue(id);
             }
         }
 

+ 25 - 36
Unity/Assets/Scripts/Core/World/Module/Fiber/ThreadPoolScheduler.cs

@@ -30,47 +30,36 @@ namespace ET
         {
             while (true)
             {
-                try
+                if (this.fiberManager.IsDisposed())
+                {
+                    return;
+                }
+                
+                if (!this.idQueue.TryDequeue(out int id))
                 {
-                    if (this.fiberManager.IsDisposed())
-                    {
-                        return;
-                    }
-                    
-                    if (!this.idQueue.TryDequeue(out int id))
-                    {
-                        Thread.Sleep(1);
-                        continue;
-                    }
-
-                    Fiber fiber = this.fiberManager.Get(id);
-                    if (fiber == null)
-                    {
-                        continue;
-                    }
-
-                    if (fiber.IsDisposed)
-                    {
-                        continue;
-                    }
-                    
-                    this.idQueue.Enqueue(id);
-
-                    // 正在执行的就不执行了
-                    if (!fiber.IsRuning)
-                    {
-                        SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
-                        fiber.Update();
-                        fiber.LateUpdate();
-                        SynchronizationContext.SetSynchronizationContext(null);
-                    }
-
                     Thread.Sleep(1);
+                    continue;
                 }
-                catch (Exception e)
+
+                Fiber fiber = this.fiberManager.Get(id);
+                if (fiber == null)
+                {
+                    continue;
+                }
+
+                if (fiber.IsDisposed)
                 {
-                    Log.Error(e);
+                    continue;
                 }
+                
+                SynchronizationContext.SetSynchronizationContext(fiber.ThreadSynchronizationContext);
+                fiber.Update();
+                fiber.LateUpdate();
+                SynchronizationContext.SetSynchronizationContext(null);
+
+                this.idQueue.Enqueue(id);
+
+                Thread.Sleep(1);
             }
         }
 

+ 19 - 26
Unity/Assets/Scripts/Core/World/Module/Fiber/ThreadScheduler.cs

@@ -5,7 +5,7 @@ using System.Threading;
 
 namespace ET
 {
-    // 一个Process一个固定的线程
+    // 一个Fiber一个固定的线程
     internal class ThreadScheduler: IScheduler
     {
         private readonly ConcurrentDictionary<int, Thread> dictionary = new();
@@ -24,35 +24,28 @@ namespace ET
             
             while (true)
             {
-                try
+                if (this.fiberManager.IsDisposed())
                 {
-                    if (this.fiberManager.IsDisposed())
-                    {
-                        return;
-                    }
-                    
-                    fiber = fiberManager.Get(fiberId);
-                    if (fiber == null)
-                    {
-                        this.dictionary.Remove(fiberId, out _);
-                        return;
-                    }
-                    
-                    if (fiber.IsDisposed)
-                    {
-                        this.dictionary.Remove(fiberId, out _);
-                        return;
-                    }
-                    
-                    fiber.Update();
-                    fiber.LateUpdate();
-
-                    Thread.Sleep(1);
+                    return;
+                }
+                
+                fiber = fiberManager.Get(fiberId);
+                if (fiber == null)
+                {
+                    this.dictionary.Remove(fiberId, out _);
+                    return;
                 }
-                catch (Exception e)
+                
+                if (fiber.IsDisposed)
                 {
-                    Log.Error(e);
+                    this.dictionary.Remove(fiberId, out _);
+                    return;
                 }
+                
+                fiber.Update();
+                fiber.LateUpdate();
+
+                Thread.Sleep(1);
             }
         }
 

+ 12 - 1
Unity/Assets/Scripts/Core/World/World.cs

@@ -1,11 +1,20 @@
 using System;
 using System.Collections.Generic;
+
 namespace ET
 {
     public class World: IDisposable
     {
         [StaticField]
-        public static World Instance = new();
+        private static World instance;
+
+        public static World Instance
+        {
+            get
+            {
+                return instance ??= new World();
+            }
+        }
 
         private readonly Stack<Type> stack = new();
         private readonly Dictionary<Type, ISingleton> singletons = new();
@@ -16,6 +25,8 @@ namespace ET
         
         public void Dispose()
         {
+            instance = null;
+            
             lock (this)
             {
                 while (this.stack.Count > 0)

+ 6 - 1
Unity/Assets/Scripts/Editor/ComponentViewEditor/TypeDrawer/EntityRefTypeDrawer.cs

@@ -1,3 +1,5 @@
+#if ENABLE_VIEW
+
 using System;
 using System.Reflection;
 using UnityEditor;
@@ -25,6 +27,7 @@ namespace ET
 
         public object DrawAndGetNewValue(Type memberType, string memberName, object value, object target)
         {
+
             FieldInfo fieldInfo = memberType.GetField("entity", BindingFlags.NonPublic | BindingFlags.Instance);
             Entity entity = (Entity)fieldInfo.GetValue(value);
             GameObject go = entity?.ViewGO;
@@ -32,4 +35,6 @@ namespace ET
             return value;
         }
     }
-}
+}
+
+#endif

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs

@@ -18,7 +18,7 @@ namespace ET.Server
                     var processScenes = StartSceneConfigCategory.Instance.GetByProcess(root.Fiber().Process);
                     foreach (StartSceneConfig startConfig in processScenes)
                     {
-                        FiberManager.Instance.Create(SchedulerType.ThreadPool, startConfig.Id, startConfig.Zone, startConfig.Type, startConfig.Name);
+                        await FiberManager.Instance.Create(SchedulerType.ThreadPool, startConfig.Id, startConfig.Zone, startConfig.Type, startConfig.Name);
                     }
 
                     break;

+ 6 - 3
Unity/Assets/Scripts/Hotfix/Server/Demo/Gate/FiberInit_Gate.cs

@@ -3,12 +3,14 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Gate)]
-    public class FiberInit_Gate: AInvokeHandler<FiberInit>
+    public class FiberInit_Gate: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             root.AddComponent<PlayerComponent>();
             root.AddComponent<GateSessionKeyComponent>();
             root.AddComponent<LocationProxyComponent>();
@@ -16,6 +18,7 @@ namespace ET.Server
 
             StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.Get((int)root.Id);
             root.AddComponent<NetServerComponent, IPEndPoint>(startSceneConfig.InnerIPPort);
+            await ETTask.CompletedTask;
         }
     }
 }

+ 10 - 6
Unity/Assets/Scripts/Hotfix/Server/Demo/Realm/FiberInit_Realm.cs

@@ -3,14 +3,18 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Realm)]
-    public class FiberInit_Realm: AInvokeHandler<FiberInit>
+    public class FiberInit_Realm: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
-            Fiber fiber = fiberInit.Fiber;
-            
-            StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.Get(fiber.Id);
-            fiber.Root.AddComponent<NetServerComponent, IPEndPoint>(startSceneConfig.InnerIPPort);
+            Scene root = fiberInit.Fiber.Root;
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
+            StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.Get(root.Fiber.Id);
+            root.AddComponent<NetServerComponent, IPEndPoint>(startSceneConfig.InnerIPPort);
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/FiberInit_Map.cs

@@ -3,17 +3,21 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Map)]
-    public class FiberInit_Map: AInvokeHandler<FiberInit>
+    public class FiberInit_Map: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             root.AddComponent<UnitComponent>();
             root.AddComponent<AOIManagerComponent>();
             root.AddComponent<RoomManagerComponent>();
             root.AddComponent<LocationProxyComponent>();
             root.AddComponent<ActorLocationSenderComponent>();
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/LockStep/Map/Match2Map_GetRoomHandler.cs

@@ -11,7 +11,7 @@ namespace ET.Server
 			//RoomManagerComponent roomManagerComponent = root.GetComponent<RoomManagerComponent>();
 			
 			Fiber fiber = root.Fiber();
-			int fiberId = FiberManager.Instance.Create(SchedulerType.ThreadPool, fiber.Zone, SceneType.RoomRoot, "RoomRoot");
+			int fiberId = await FiberManager.Instance.Create(SchedulerType.ThreadPool, fiber.Zone, SceneType.RoomRoot, "RoomRoot");
 			ActorId roomRootActorId = new(fiber.Process, fiberId);
 
 			// 发送消息给房间纤程,初始化

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/LockStep/Match/FiberInit_Match.cs

@@ -3,15 +3,19 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Match)]
-    public class FiberInit_Match: AInvokeHandler<FiberInit>
+    public class FiberInit_Match: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             root.AddComponent<MatchComponent>();
             root.AddComponent<LocationProxyComponent>();
             root.AddComponent<ActorLocationSenderComponent>();
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/LockStep/Room/FiberInit_RoomRoot.cs

@@ -3,17 +3,21 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.RoomRoot)]
-    public class FiberInit_RoomRoot: AInvokeHandler<FiberInit>
+    public class FiberInit_RoomRoot: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             Room room = root.AddChild<Room>();
             root.AddComponent<LocationProxyComponent>();
             root.AddComponent<ActorLocationSenderComponent>();
             
             room.Name = "Server";
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/Module/ActorLocation/FiberInit_Location.cs

@@ -3,13 +3,17 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Location)]
-    public class FiberInit_Location: AInvokeHandler<FiberInit>
+    public class FiberInit_Location: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             root.AddComponent<LocationManagerComoponent>();
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 22 - 21
Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentSystem.cs

@@ -1,4 +1,5 @@
-using System.Net;
+using System;
+using System.Net;
 using System.Net.Sockets;
 
 namespace ET.Server
@@ -6,26 +7,26 @@ namespace ET.Server
     [FriendOf(typeof(NetInnerComponent))]
     public static partial class NetInnerComponentSystem
     {
-        [EntitySystem]
-        private static void Awake(this NetInnerComponent self)
-        {
-            switch (self.InnerProtocol)
-            {
-                case NetworkProtocol.TCP:
-                {
-                    self.AService = new TService(AddressFamily.InterNetwork, ServiceType.Inner);
-                    break;
-                }
-                case NetworkProtocol.KCP:
-                {
-                    self.AService = new KService(AddressFamily.InterNetwork, ServiceType.Inner);
-                    break;
-                }
-            }
-                
-            self.AService.ReadCallback = self.OnRead;
-            self.AService.ErrorCallback = self.OnError;
-        }
+        //[EntitySystem]
+        //private static void Awake(this NetInnerComponent self)
+        //{
+        //    switch (self.InnerProtocol)
+        //    {
+        //        case NetworkProtocol.TCP:
+        //        {
+        //            self.AService = new TService(AddressFamily.InterNetwork, ServiceType.Inner);
+        //            break;
+        //        }
+        //        case NetworkProtocol.KCP:
+        //        {
+        //            self.AService = new KService(AddressFamily.InterNetwork, ServiceType.Inner);
+        //            break;
+        //        }
+        //    }
+        //        
+        //    self.AService.ReadCallback = self.OnRead;
+        //    self.AService.ErrorCallback = self.OnError;
+        //}
 
         [EntitySystem]
         private static void Awake(this NetInnerComponent self, IPEndPoint address)

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/Module/NetInner/FiberInit_Net.cs

@@ -3,15 +3,19 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Net)]
-    public class FiberInit_NetInner: AInvokeHandler<FiberInit>
+    public class FiberInit_Net: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             StartMachineConfig startMachineConfig = StartMachineConfigCategory.Instance.Get(fiberInit.Fiber.Process);
             IPEndPoint ipEndPoint = NetworkHelper.ToIPEndPoint($"{startMachineConfig.InnerIP}:{startMachineConfig.WatcherPort}");
             root.AddComponent<NetInnerComponent, IPEndPoint>(ipEndPoint);
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/Module/Router/FiberInit_Router.cs

@@ -3,16 +3,20 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.Router)]
-    public class FiberInit_Router: AInvokeHandler<FiberInit>
+    public class FiberInit_Router: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.Get((int)root.Id);
             
             // 开发期间使用OuterIPPort,云服务器因为本机没有OuterIP,所以要改成InnerIPPort,然后在云防火墙中端口映射到InnerIPPort
             root.AddComponent<RouterComponent, IPEndPoint, string>(startSceneConfig.OuterIPPort, startSceneConfig.StartProcessConfig.InnerIP);
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 7 - 3
Unity/Assets/Scripts/Hotfix/Server/Module/Router/FiberInit_RouterManager.cs

@@ -3,14 +3,18 @@
 namespace ET.Server
 {
     [Invoke((long)SceneType.RouterManager)]
-    public class FiberInit_RouterManager: AInvokeHandler<FiberInit>
+    public class FiberInit_RouterManager: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
             Scene root = fiberInit.Fiber.Root;
-
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
             StartSceneConfig startSceneConfig = StartSceneConfigCategory.Instance.Get((int)root.Id);
             root.AddComponent<HttpComponent, string>($"http://*:{startSceneConfig.Port}/");
+
+            await ETTask.CompletedTask;
         }
     }
 }

+ 9 - 12
Unity/Assets/Scripts/Hotfix/Share/FiberInit_Main.cs

@@ -1,20 +1,17 @@
 namespace ET
 {
     [Invoke((long)SceneType.Main)]
-    public class FiberInit_Main: AInvokeHandler<FiberInit>
+    public class FiberInit_Main: AInvokeHandler<FiberInit, ETTask>
     {
-        public override void Handle(FiberInit fiberInit)
+        public override async ETTask Handle(FiberInit fiberInit)
         {
-            HandleAsync(fiberInit).Coroutine();
-        }
-
-        private async ETTask HandleAsync(FiberInit fiberInit)
-        {
-            Fiber fiber = fiberInit.Fiber;
-
-            await EventSystem.Instance.PublishAsync(fiber.Root, new EventType.EntryEvent1());
-            await EventSystem.Instance.PublishAsync(fiber.Root, new EventType.EntryEvent2());
-            await EventSystem.Instance.PublishAsync(fiber.Root, new EventType.EntryEvent3());
+            Scene root = fiberInit.Fiber.Root;
+            root.AddComponent<MailBoxComponent, MailBoxType>(MailBoxType.UnOrderedMessage);
+            root.AddComponent<ActorSenderComponent>();
+            root.AddComponent<ActorRecverComponent>();
+            await EventSystem.Instance.PublishAsync(root, new EventType.EntryEvent1());
+            await EventSystem.Instance.PublishAsync(root, new EventType.EntryEvent2());
+            await EventSystem.Instance.PublishAsync(root, new EventType.EntryEvent3());
         }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Message/NetInnerComponent.cs

@@ -4,7 +4,7 @@ using System.Net;
 namespace ET.Server
 {
     [ComponentOf(typeof(Scene))]
-    public class NetInnerComponent: Entity, IAwake<IPEndPoint>, IAwake, IDestroy, IUpdate
+    public class NetInnerComponent: Entity, IAwake<IPEndPoint>, IDestroy, IUpdate
     {
         public AService AService;
         

+ 3 - 2
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -51,9 +51,10 @@ namespace ET
             World.Instance.AddSingleton<NavmeshComponent>();
             
             World.Instance.AddSingleton<FiberManager>();
-            FiberManager.Instance.Create(SchedulerType.Main, ConstFiberId.Main, 0, SceneType.Main, "");
             
-            await ETTask.CompletedTask;
+            // 注意这里创建Main Fiber 服务端没有设置同步上下文,会导致await后面的代码回调到了其它线程,一定不要在这个await后面写代码
+            await FiberManager.Instance.Create(SchedulerType.Main, ConstFiberId.Main, 0, SceneType.Main, "");
+            // 注意下面不能加代码!!!!!!!!
         }
     }
 }

+ 1 - 1
Unity/ProjectSettings/ProjectSettings.asset

@@ -839,7 +839,7 @@ PlayerSettings:
   webGLDecompressionFallback: 0
   webGLPowerPreference: 2
   scriptingDefineSymbols:
-    Android: UNITY;SINGLE_THREAD;ENABLE_DLL;ENABLE_VIEW
+    Android: UNITY;SINGLE_THREAD;ENABLE_DLL
     Server: UNITY
     Standalone: UNITY;SINGLE_THREAD;ENABLE_VIEW;ENABLE_CODES
     WebGL: UNITY;ENABLE_CODES