Эх сурвалжийг харах

Unity自带的同步上下文timescale为0的时候不会某些await不会回调,用OneThreadSynchronizationContenx替换

tanghai 8 жил өмнө
parent
commit
651e410e0f

+ 1 - 1
Config/StartConfig/LocalAllServer.txt

@@ -1 +1 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }, { "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "Url" : "", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : "127.0.0.1" }, { "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "Url" : "", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }

+ 1 - 0
Server/Model/Server.Model.csproj

@@ -56,6 +56,7 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Object.cs" Link="Base\Object\Object.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ObjectPool.cs" Link="Base\Object\ObjectPool.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ObjectSystemAttribute.cs" Link="Base\Object\ObjectSystemAttribute.cs" />
+    <Compile Include="..\..\Unity\Assets\Scripts\Base\OneThreadSynchronizationContext.cs" Link="Base\OneThreadSynchronizationContext.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\QueueDictionary.cs" Link="Base\QueueDictionary.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\TryLocker.cs" Link="Base\TryLocker.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Base\UnOrderMultiMap.cs" Link="Base\UnOrderMultiMap.cs" />

+ 0 - 0
Server/Model/Other/OneThreadSynchronizationContext.cs → Unity/Assets/Scripts/Base/OneThreadSynchronizationContext.cs


+ 12 - 0
Unity/Assets/Scripts/Base/OneThreadSynchronizationContext.cs.meta

@@ -0,0 +1,12 @@
+fileFormatVersion: 2
+guid: fcb4a65fef4e05448a17ace64a0658dd
+timeCreated: 1519875927
+licenseType: Free
+MonoImporter:
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 6 - 0
Unity/Assets/Scripts/Init.cs

@@ -1,10 +1,13 @@
 using System;
+using System.Threading;
 using UnityEngine;
 
 namespace Model
 {
 	public class Init : MonoBehaviour
 	{
+		private readonly OneThreadSynchronizationContext contex = new OneThreadSynchronizationContext();
+
 		private async void Start()
 		{
 			try
@@ -14,6 +17,8 @@ namespace Model
 					Log.Warning($"当前版本:{Application.unityVersion}, 最好使用运行指南推荐版本!");
 				}
 
+				SynchronizationContext.SetSynchronizationContext(this.contex);
+
 				DontDestroyOnLoad(gameObject);
 				Game.EventSystem.Add(DLLType.Model, typeof(Init).Assembly);
 
@@ -50,6 +55,7 @@ namespace Model
 
 		private void Update()
 		{
+			this.contex.Update();
 			Game.Hotfix.Update?.Invoke();
 			Game.EventSystem.Update();
 		}

+ 7 - 1
Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs

@@ -161,6 +161,9 @@ namespace Model
 					await this.sendBuffer.ReadAsync(stream);
 				}
 			}
+			catch (ObjectDisposedException)
+			{
+			}
 			catch (Exception e)
 			{
 				Log.Error(e.ToString());
@@ -186,7 +189,7 @@ namespace Model
 					}
 
 					int n = await this.recvBuffer.WriteAsync(stream);
-					
+
 					if (n == 0)
 					{
 						this.OnError(this, SocketError.NetworkReset);
@@ -207,6 +210,9 @@ namespace Model
 					}
 				}
 			}
+			catch (ObjectDisposedException)
+			{
+			}
 			catch (Exception e)
 			{
 				Log.Error(e.ToString());

+ 1 - 0
Unity/Unity.csproj

@@ -175,6 +175,7 @@
     <Compile Include="Assets\Scripts\Base\Object\Object.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ObjectPool.cs" />
     <Compile Include="Assets\Scripts\Base\Object\ObjectSystemAttribute.cs" />
+    <Compile Include="Assets\Scripts\Base\OneThreadSynchronizationContext.cs" />
     <Compile Include="Assets\Scripts\Base\QueueDictionary.cs" />
     <Compile Include="Assets\Scripts\Base\TryLocker.cs" />
     <Compile Include="Assets\Scripts\Base\UnOrderMultiMap.cs" />