Prechádzať zdrojové kódy

tcp改成下一次update再写入缓冲区,减少系统sendasync调用

tanghai 7 rokov pred
rodič
commit
03a1a186b3

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

@@ -104,7 +104,8 @@ namespace ETModel
 			}
 			
 			this.StartRecv();
-			this.StartSend();
+			
+			this.GetService().MarkNeedStartSend(this.Id);
 		}
 		
 		public override void Send(MemoryStream stream)
@@ -118,10 +119,7 @@ namespace ETModel
 			this.sendBuffer.Write(this.cache, 0, this.cache.Length);
 			this.sendBuffer.ReadFrom(stream);
 
-			if(!this.isSending)
-			{
-				this.StartSend();
-			}
+			this.GetService().MarkNeedStartSend(this.Id);
 		}
 
 		private void OnComplete(object sender, SocketAsyncEventArgs e)
@@ -259,7 +257,9 @@ namespace ETModel
 			this.StartRecv();
 		}
 
-		private void StartSend()
+		public bool IsSending => this.isSending;
+
+		public void StartSend()
 		{
 			if(!this.isConnected)
 			{

+ 31 - 0
Unity/Assets/Scripts/Module/Message/Network/TCP/TService.cs

@@ -17,6 +17,8 @@ namespace ETModel
 		
 		public RecyclableMemoryStreamManager MemoryStreamManager = new RecyclableMemoryStreamManager();
 		
+		public HashSet<long> needStartSendChannel = new HashSet<long>();
+		
 		/// <summary>
 		/// 即可做client也可做server
 		/// </summary>
@@ -135,6 +137,11 @@ namespace ETModel
 			return this.ConnectChannel(ipEndPoint);
 		}
 
+		public void MarkNeedStartSend(long id)
+		{
+			this.needStartSendChannel.Add(id);
+		}
+
 		public override void Remove(long id)
 		{
 			TChannel channel;
@@ -152,6 +159,30 @@ namespace ETModel
 
 		public override void Update()
 		{
+			foreach (long id in this.needStartSendChannel)
+			{
+				TChannel channel;
+				if (!this.idChannels.TryGetValue(id, out channel))
+				{
+					continue;
+				}
+
+				if (channel.IsSending)
+				{
+					continue;
+				}
+
+				try
+				{
+					channel.StartSend();
+				}
+				catch (Exception e)
+				{
+					Log.Error(e);
+				}
+			}
+			
+			this.needStartSendChannel.Clear();
 		}
 	}
 }

+ 7 - 10
Unity/Unity.csproj

@@ -12,16 +12,13 @@
     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
     <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
-    <TargetFrameworkProfile>
-    </TargetFrameworkProfile>
-    <CompilerResponseFile>
-    </CompilerResponseFile>
+    <TargetFrameworkProfile></TargetFrameworkProfile>
+    <CompilerResponseFile></CompilerResponseFile>
     <UnityProjectGenerator>VSTU</UnityProjectGenerator>
     <UnityProjectType>Game:1</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
     <UnityVersion>2017.4.9f1</UnityVersion>
-    <RootNamespace>
-    </RootNamespace>
+    <RootNamespace></RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup>
@@ -381,9 +378,6 @@
     <Compile Include="Assets\Scripts\Module\FrameSync\UnitComponent.cs" />
     <Compile Include="Assets\Scripts\Module\FrameSync\UnitFactory.cs" />
     <Compile Include="Assets\Scripts\Module\Message\AMHandler.cs" />
-    <Compile Include="Assets\Scripts\Module\Message\Network\WebSocket\WChannel.cs" />
-    <Compile Include="Assets\Scripts\Module\Message\Network\WebSocket\WService.cs" />
-    <Compile Include="Assets\Scripts\Module\Message\OuterMessageDispatcher.cs" />
     <Compile Include="Assets\Scripts\Module\Message\ErrorCode.cs" />
     <Compile Include="Assets\Scripts\Module\Message\IActorMessage.cs" />
     <Compile Include="Assets\Scripts\Module\Message\IMessage.cs" />
@@ -408,11 +402,14 @@
     <Compile Include="Assets\Scripts\Module\Message\Network\TCP\PacketParser.cs" />
     <Compile Include="Assets\Scripts\Module\Message\Network\TCP\TChannel.cs" />
     <Compile Include="Assets\Scripts\Module\Message\Network\TCP\TService.cs" />
+    <Compile Include="Assets\Scripts\Module\Message\Network\WebSocket\WChannel.cs" />
+    <Compile Include="Assets\Scripts\Module\Message\Network\WebSocket\WService.cs" />
     <Compile Include="Assets\Scripts\Module\Message\NetworkComponent.cs" />
     <Compile Include="Assets\Scripts\Module\Message\Opcode.cs" />
     <Compile Include="Assets\Scripts\Module\Message\OpcodeHelper.cs" />
     <Compile Include="Assets\Scripts\Module\Message\OpcodeTypeComponent.cs" />
     <Compile Include="Assets\Scripts\Module\Message\OuterMessage.cs" />
+    <Compile Include="Assets\Scripts\Module\Message\OuterMessageDispatcher.cs" />
     <Compile Include="Assets\Scripts\Module\Message\OuterOpcode.cs" />
     <Compile Include="Assets\Scripts\Module\Message\ProtobufHelper.cs" />
     <Compile Include="Assets\Scripts\Module\Message\ProtobufPacker.cs" />
@@ -1152,4 +1149,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>