Răsfoiți Sursa

找到了ENet性能问题,poll的time设置为0性能极好
一秒近发送1W来回。在linux上我写得C++ tcp协议测试,一秒钟差不多4W来回

tanghai 12 ani în urmă
părinte
comite
ceeddb1542

+ 21 - 1
CSharp/CSharp.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 2013
-VisualStudioVersion = 12.0.21005.1
+VisualStudioVersion = 12.0.30110.0
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Modules", "Modules", "{C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}"
 EndProject
@@ -64,6 +64,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Component", "Game\Component
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tree", "App\Modules\Tree\Tree.csproj", "{6CD185D1-08E0-4729-A999-2D5B57BA8193}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ENetExe", "Platform\ENetExe\ENetExe.csproj", "{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{2814EB55-E1C2-4751-AC14-94116DA9D22B}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -344,6 +348,18 @@ Global
 		{6CD185D1-08E0-4729-A999-2D5B57BA8193}.Release|Mixed Platforms.Build.0 = Release|Any CPU
 		{6CD185D1-08E0-4729-A999-2D5B57BA8193}.Release|Win32.ActiveCfg = Release|Any CPU
 		{6CD185D1-08E0-4729-A999-2D5B57BA8193}.Release|x86.ActiveCfg = Release|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|Win32.ActiveCfg = Debug|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Debug|x86.ActiveCfg = Debug|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Release|Any CPU.Build.0 = Release|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Release|Mixed Platforms.Build.0 = Release|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Release|Win32.ActiveCfg = Release|Any CPU
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}.Release|x86.ActiveCfg = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -361,6 +377,7 @@ Global
 		{C9992B7C-313E-4C9F-A954-640D01EDFB58} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{D0B4CFAC-A368-4742-9863-68776CFA9938} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{901A8E5C-C4C6-4C3C-8E18-068D75119F5D} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
+		{CBA52DC8-1C80-4A79-9AC5-73514EBBD749} = {ADBF5F67-B480-4A93-9D50-C81856FC61A9}
 		{5D6ECBCD-BE14-4DCB-BAEC-57089748B164} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
 		{5AA48F9A-455D-4CD8-A605-A3AC38283E60} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
 		{B07D70E7-F902-4F67-A486-B7AF27D6B813} = {C4C64188-4FAE-4CC3-A9E6-D9D4AF7429B6}
@@ -373,4 +390,7 @@ Global
 		{CB5BCF0A-4741-477A-94C6-49ECA782555F} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 		{0FA529D1-D0A9-4A8E-90F5-117CE80F2EDE} = {D0CC1FF4-2747-4278-A51F-BE9AA959175B}
 	EndGlobalSection
+	GlobalSection(Performance) = preSolution
+		HasPerformanceSessions = true
+	EndGlobalSection
 EndGlobal

+ 3 - 5
CSharp/Platform/ENet/ESocket.cs

@@ -112,7 +112,7 @@ namespace ENet
 			return tcs.Task;
 		}
 
-		public Task<bool> AcceptAsync(Action action)
+		public Task<bool> AcceptAsync()
 		{
 			if (this.service.PeersManager.ContainsKey(IntPtr.Zero))
 			{
@@ -128,8 +128,7 @@ namespace ENet
 				this.service.ConnEEvents.RemoveFirst();
 
 				this.PeerPtr = eEvent.PeerPtr;
-				this.service.PeersManager.Add(this.PeerPtr, this);
-				action();
+				this.service.PeersManager.Add(this.PeerPtr, this);
 				tcs.TrySetResult(true);
 			}
 			else
@@ -145,8 +144,7 @@ namespace ENet
 					this.service.PeersManager.Remove(IntPtr.Zero);
 
 					this.PeerPtr = eEvent.PeerPtr;
-					this.service.PeersManager.Add(this.PeerPtr, this);
-					action();
+					this.service.PeersManager.Add(this.PeerPtr, this);
 					tcs.TrySetResult(true); 
 				};
 			}

+ 6 - 0
CSharp/Platform/ENetExe/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
+    </startup>
+</configuration>

+ 59 - 0
CSharp/Platform/ENetExe/ENetExe.csproj

@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{CBA52DC8-1C80-4A79-9AC5-73514EBBD749}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>ENetExe</RootNamespace>
+    <AssemblyName>ENetExe</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>..\..\Bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>..\..\Bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\ENetTest\ENetTest.csproj">
+      <Project>{901a8e5c-c4c6-4c3c-8e18-068d75119f5d}</Project>
+      <Name>ENetTest</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

+ 12 - 0
CSharp/Platform/ENetExe/Program.cs

@@ -0,0 +1,12 @@
+
+namespace ENetExe
+{
+	static class Program
+	{
+		static void Main(string[] args)
+		{
+			var test = new ENetTest.ENetClientServerTest();
+			test.ClientSendToServer();
+		}
+	}
+}

+ 36 - 0
CSharp/Platform/ENetExe/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的常规信息通过以下
+// 特性集控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("ENetProgram")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("ENetProgram")]
+[assembly: AssemblyCopyright("Copyright ©  2014")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 使此程序集中的类型
+// 对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
+// 则将该类型上的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("f73e95f0-d6fe-427f-960c-701f86fa309d")]
+
+// 程序集的版本信息由下面四个值组成: 
+//
+//      主版本
+//      次版本 
+//      生成号
+//      修订号
+//
+// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
+// 方法是按如下所示使用“*”: 
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 20 - 12
CSharp/Platform/ENetTest/ENetClientServerTest.cs

@@ -5,12 +5,12 @@ using Helper;
 using Log;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 
-namespace ENetCSTest
+namespace ENetTest
 {
 	[TestClass]
 	public class ENetClientServerTest
 	{
-		private const int pingPangCount = 1000;
+		private const int pingPangCount = 10000;
 		private static async void ClientEvent(EService service, string hostName, ushort port)
 		{
 			var eSocket = new ESocket(service);
@@ -31,19 +31,27 @@ namespace ENetCSTest
 			service.Stop();
 		}
 
-		private static void ServerEvent(EService service, Barrier barrier)
+		private static async void ServerEvent(EService service, Barrier barrier)
 		{
 			barrier.SignalAndWait();
-			StartAccept(service);
+
+			bool isRunning = true;
+			while (isRunning)
+			{
+				Logger.Debug("start accept");
+				var eSocket = new ESocket(service);
+				await eSocket.AcceptAsync();
+				eSocket.Disconnect += ev =>
+				{
+					isRunning = false;
+					service.Stop();
+				};
+				Echo(eSocket);
+			}
 		}
 
-		private static async void StartAccept(EService service)
+		private static async void Echo(ESocket eSocket)
 		{
-			var eSocket = new ESocket(service);
-			await eSocket.AcceptAsync(() => StartAccept(service));
-			// Client断开,Server端收到Disconnect事件,结束Server线程
-			eSocket.Disconnect += ev => service.Stop();
-
 			for (int i = 0; i < pingPangCount; ++i)
 			{
 				var bytes = await eSocket.ReadAsync();
@@ -62,8 +70,8 @@ namespace ENetCSTest
 			var clientHost = new EService();
 			var serverHost = new EService(hostName, port);
 
-			var serverThread = new Thread(() => serverHost.Start(10));
-			var clientThread = new Thread(() => clientHost.Start(10));
+			var serverThread = new Thread(() => serverHost.Start());
+			var clientThread = new Thread(() => clientHost.Start());
 
 			serverThread.Start();
 			clientThread.Start();