Просмотр исходного кода

使用protobuf序列化可能是空byte[], 修复benchmark的问题,1000个连接,每秒约4W包

tanghai 8 лет назад
Родитель
Сommit
ef8d2262c2

+ 1 - 1
Server/App/Program.cs

@@ -105,7 +105,7 @@ namespace App
 						break;
 					case AppType.Benchmark:
 						Game.Scene.AddComponent<NetOuterComponent>();
-						Game.Scene.AddComponent<BenchmarkComponent, string>(clientConfig.Address);
+						Game.Scene.AddComponent<BenchmarkComponent, IPEndPoint>(clientConfig.IPEndPoint);
 						break;
 					default:
 						throw new Exception($"命令行参数没有设置正确的AppType: {startConfig.AppType}");

+ 3 - 4
Server/Model/Component/BenchmarkComponent.cs

@@ -24,7 +24,7 @@ namespace Model
 			try
 			{
 				NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
-				for (int i = 0; i < 100; i++)
+				for (int i = 0; i < 1000; i++)
 				{
 					await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
 					this.TestAsync(networkComponent, ipEndPoint, i);
@@ -43,10 +43,9 @@ namespace Model
 				using (Session session = networkComponent.Create(ipEndPoint))
 				{
 					int i = 0;
-					while (i < 100000000)
+					while (i < 1000000000)
 					{
 						++i;
-						await Game.Scene.GetComponent<TimerComponent>().WaitAsync(10);
 						await this.Send(session, j);
 					}
 				}
@@ -68,7 +67,7 @@ namespace Model
 				await session.Call<R2C_Ping>(new C2R_Ping());
 				++this.k;
 
-				if (this.k % 10000 != 0)
+				if (this.k % 100000 != 0)
 				{
 					return;
 				}

+ 11 - 1
Server/Model/Component/Config/ClientConfig.cs

@@ -1,4 +1,5 @@
-using MongoDB.Bson.Serialization.Attributes;
+using System.Net;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
@@ -16,5 +17,14 @@ namespace Model
 				return $"{this.Host}:{this.Port}";
 			}
 		}
+
+		[BsonIgnore]
+		public IPEndPoint IPEndPoint
+		{
+			get
+			{
+				return NetworkHelper.ToIPEndPoint(this.Host, this.Port);
+			}
+		}
 	}
 }

+ 4 - 0
Unity/Assets/Scripts/Entity/Message/OuterMessage.cs

@@ -13,6 +13,7 @@ namespace Model
 	[ProtoInclude((int)Opcode.C2R_Login, typeof(C2R_Login))]
 	[ProtoInclude((int)Opcode.C2G_LoginGate, typeof(C2G_LoginGate))]
 	[ProtoInclude((int)Opcode.C2G_EnterMap, typeof(C2G_EnterMap))]
+	[ProtoInclude((int)Opcode.C2R_Ping, typeof(C2R_Ping))]
 	public abstract partial class ARequest : AMessage
 	{
 	}
@@ -21,6 +22,7 @@ namespace Model
 	[ProtoInclude((int)Opcode.R2C_Login, typeof(R2C_Login))]
 	[ProtoInclude((int)Opcode.G2C_LoginGate, typeof(G2C_LoginGate))]
 	[ProtoInclude((int)Opcode.G2C_EnterMap, typeof(G2C_EnterMap))]
+	[ProtoInclude((int)Opcode.R2C_Ping, typeof(R2C_Ping))]
 	public abstract partial class AResponse : AMessage
 	{
 	}
@@ -198,11 +200,13 @@ namespace Model
 	{
 	}
 
+	[ProtoContract]
 	[Message(Opcode.C2R_Ping)]
 	public class C2R_Ping: ARequest
 	{
 	}
 
+	[ProtoContract]
 	[Message(Opcode.R2C_Ping)]
 	public class R2C_Ping: AResponse
 	{

+ 9 - 9
Unity/Assets/Scripts/Entity/Session.cs

@@ -62,9 +62,9 @@ namespace Model
 					continue;
 				}
 
-				if (messageBytes.Length < 3)
+				if (messageBytes.Length < 2)
 				{
-					Log.Error($"message error length < 3, ip: {this.RemoteAddress}");
+					Log.Error($"message error length < 2, ip: {this.RemoteAddress}");
 					this.network.Remove(this.Id);
 					return;
 				}
@@ -131,7 +131,7 @@ namespace Model
 		{
 			request.RpcId = ++RpcId;
 
-			this.requestCallback[RpcId] = (message) =>
+			this.requestCallback[request.RpcId] = (message) =>
 			{
 				try
 				{
@@ -155,7 +155,7 @@ namespace Model
 			request.RpcId = ++RpcId;
 
 			var tcs = new TaskCompletionSource<AResponse>();
-			this.requestCallback[RpcId] = (message) =>
+			this.requestCallback[request.RpcId] = (message) =>
 			{
 				try
 				{
@@ -187,7 +187,7 @@ namespace Model
 			
 			var tcs = new TaskCompletionSource<AResponse>();
 
-			this.requestCallback[RpcId] = (message) =>
+			this.requestCallback[request.RpcId] = (message) =>
 			{
 				try
 				{
@@ -206,7 +206,7 @@ namespace Model
 				}
 			};
 
-			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
+			cancellationToken.Register(() => { this.requestCallback.Remove(request.RpcId); });
 
 			this.SendMessage(request);
 
@@ -221,7 +221,7 @@ namespace Model
 			request.RpcId = ++RpcId;
 			
 			var tcs = new TaskCompletionSource<Response>();
-			this.requestCallback[RpcId] = (message) =>
+			this.requestCallback[request.RpcId] = (message) =>
 			{
 				try
 				{
@@ -255,7 +255,7 @@ namespace Model
 			
 			var tcs = new TaskCompletionSource<Response>();
 
-			this.requestCallback[RpcId] = (message) =>
+			this.requestCallback[request.RpcId] = (message) =>
 			{
 				try
 				{
@@ -274,7 +274,7 @@ namespace Model
 				}
 			};
 
-			cancellationToken.Register(() => { this.requestCallback.Remove(RpcId); });
+			cancellationToken.Register(() => { this.requestCallback.Remove(request.RpcId); });
 
 			this.SendMessage(request);