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

1.客户端发来的消息经过gate处理,加上玩家的id再发送到内部网络
2.根据消息的opcode区分各类消息

tanghai 10 лет назад
Родитель
Сommit
c1bada1b35

+ 0 - 2
CSharp/Game/Controller/Controller.csproj

@@ -39,8 +39,6 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Event\BuffTimeoutEvent.cs" />
-    <Compile Include="Event\GateMessageEvent.cs" />
-    <Compile Include="Event\MessageEvent.cs" />
     <Compile Include="BehaviorTreeNode\Not.cs" />
     <Compile Include="BehaviorTreeNode\Selector.cs" />
     <Compile Include="BehaviorTreeNode\Sequence.cs" />

+ 0 - 12
CSharp/Game/Controller/Event/GateMessageEvent.cs

@@ -1,12 +0,0 @@
-using Model;
-
-namespace Controller
-{
-	[Event(EventType.GateMessage, ServerType.Gate)]
-	public class GateMessageEvent : IEventSync
-	{
-		public void Run(Env env)
-		{
-		}
-	}
-}

+ 0 - 32
CSharp/Game/Controller/Event/MessageEvent.cs

@@ -1,32 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using Common.Network;
-using Model;
-
-namespace Controller
-{
-	[Event(EventType.Message, ServerType.All)]
-	public class MessageEvent: IEventAsync
-	{
-		public async Task RunAsync(Env env)
-		{
-			AChannel channel = env.Get<AChannel>(EnvKey.Channel);
-			ChannelUnitInfoComponent channelUnitInfoComponent =
-					channel.GetComponent<ChannelUnitInfoComponent>();
-			if (channelUnitInfoComponent != null)
-			{
-				Unit unit = World.Instance.GetComponent<UnitComponent>().Get(channelUnitInfoComponent.PlayerId);
-				if (unit == null)
-				{
-					return;
-				}
-				unit.GetComponent<ActorComponent>().Add(env);
-				return;
-			}
-
-			var message = env.Get<byte[]>(EnvKey.Message);
-			int opcode = BitConverter.ToUInt16(message, 0);
-			await World.Instance.GetComponent<EventComponent<MessageAttribute>>().RunAsync(opcode, env);
-		}
-	}
-}

+ 0 - 1
CSharp/Game/Model/AEventAttribute.cs

@@ -12,7 +12,6 @@ namespace Model
 		protected AEventAttribute(int type, ServerType serverType)
 		{
 			this.Type = type;
-			this.ServerType = serverType;
 		}
 
 		public bool Contains(ServerType serverType)

+ 1 - 0
CSharp/Game/Model/Component/ChannelUnitInfoComponent.cs

@@ -11,5 +11,6 @@ namespace Model
 	{
 		public byte[] Account { get; set; }
 		public ObjectId PlayerId { get; set; }
+		public string ServerName { get; set; }
 	}
 }

+ 14 - 5
CSharp/Game/Model/Component/GateNetworkComponent.cs

@@ -65,11 +65,20 @@ namespace Model
 				Env env = new Env();
 				env[EnvKey.Channel] = channel;
 				env[EnvKey.Message] = message;
-				// 进行消息解析分发
-#pragma warning disable 4014
-				World.Instance.GetComponent<EventComponent<EventAttribute>>()
-						.RunAsync(EventType.GateMessage, env);
-#pragma warning restore 4014
+
+				// 进行消息分发
+				int opcode = BitConverter.ToUInt16(message, 0);
+				if (!MessageTypeHelper.IsClientMessage(opcode))
+				{
+					ChannelUnitInfoComponent channelUnitInfoComponent = channel.GetComponent<ChannelUnitInfoComponent>();
+					byte[] idBuffer = channelUnitInfoComponent.PlayerId.ToByteArray();
+					byte[] buffer = new byte[message.Length + 12];
+					Array.Copy(message, 0, buffer, 0, 4);
+					Array.Copy(idBuffer, 0, buffer, 4, idBuffer.Length);
+					Array.Copy(message, 4, buffer, 4 + 12, message.Length - 4);
+					continue;
+				}
+				World.Instance.GetComponent<EventComponent<MessageAttribute>>().RunAsync(opcode, env);
 			}
 		}
 	}

+ 76 - 9
CSharp/Game/Model/Component/NetworkComponent.cs

@@ -1,6 +1,10 @@
 using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
 using Common.Base;
+using Common.Helper;
 using Common.Network;
+using MongoDB.Bson;
 using TNet;
 using UNet;
 
@@ -10,6 +14,10 @@ namespace Model
 	{
 		private IService service;
 
+		private int requestId;
+
+		private readonly Dictionary<int, Action<byte[], bool>> requestCallback = new Dictionary<int, Action<byte[], bool>>();
+
 		private void Accept(string host, int port, NetworkProtocol protocol = NetworkProtocol.TCP)
 		{
 			switch (protocol)
@@ -46,7 +54,7 @@ namespace Model
 			while (true)
 			{
 				AChannel channel = await this.service.GetChannel();
-				ProcessChannel(channel);
+				this.ProcessChannel(channel);
 			}
 		}
 
@@ -54,7 +62,7 @@ namespace Model
 		/// 接收分发封包
 		/// </summary>
 		/// <param name="channel"></param>
-		private static async void ProcessChannel(AChannel channel)
+		private async void ProcessChannel(AChannel channel)
 		{
 			while (true)
 			{
@@ -63,20 +71,79 @@ namespace Model
 				env[EnvKey.Channel] = channel;
 				env[EnvKey.Message] = message;
 				int opcode = BitConverter.ToUInt16(message, 0);
+
+				if (MessageTypeHelper.IsClientMessage(opcode))
+				{
+					continue;
+				}
+
 				// 这个区间表示消息是rpc响应消息
-				if (opcode >= 40000 && opcode < 50000)
+				if (MessageTypeHelper.IsRpcResponseMessage(opcode))
 				{
 					int id = BitConverter.ToInt32(message, 2);
-					channel.RequestCallback(id, message, true);
+					this.RequestCallback(channel, id, message, true);
 					continue;
 				}
 
-				// 进行消息解析分发
-#pragma warning disable 4014
-				World.Instance.GetComponent<EventComponent<EventAttribute>>()
-						.RunAsync(EventType.Message, env);
-#pragma warning restore 4014
+				// 进行消息分发
+				World.Instance.GetComponent<EventComponent<MessageAttribute>>().RunAsync(opcode, env);
+			}
+		}
+
+		// 消息回调或者超时回调
+		public void RequestCallback(AChannel channel, int id, byte[] buffer, bool isOK)
+		{
+			Action<byte[], bool> action;
+			if (this.requestCallback.TryGetValue(id, out action))
+			{
+				action(buffer, isOK);
 			}
+			this.requestCallback.Remove(id);
+		}
+
+		/// <summary>
+		/// Rpc请求
+		/// </summary>
+		public Task<T> Request<T, K>(AChannel channel, short type, K request, int waitTime = 0)
+		{
+			++this.requestId;
+			byte[] requestBuffer = MongoHelper.ToBson(request);
+			byte[] typeBuffer = BitConverter.GetBytes(type);
+			byte[] idBuffer = BitConverter.GetBytes(this.requestId);
+			channel.SendAsync(new List<byte[]> { typeBuffer, idBuffer, requestBuffer });
+			var tcs = new TaskCompletionSource<T>();
+			this.requestCallback[this.requestId] = (e, b) =>
+			{
+				if (b)
+				{
+					T response = MongoHelper.FromBson<T>(e, 6);
+					tcs.SetResult(response);
+				}
+				else
+				{
+					tcs.SetException(new Exception(string.Format("rpc timeout {0} {1}", type, MongoHelper.ToJson(request))));
+				}
+			};
+
+			if (waitTime > 0)
+			{
+				this.service.Timer.Add(TimeHelper.Now() + waitTime, () =>
+				{
+					this.RequestCallback(channel, this.requestId, null, false);
+				});
+			}
+			return tcs.Task;
+		}
+
+		/// <summary>
+		/// Rpc响应
+		/// </summary>
+		public void Response<T>(AChannel channel, short type, int id, T response)
+		{
+			byte[] responseBuffer = MongoHelper.ToBson(response);
+			byte[] typeBuffer = BitConverter.GetBytes(type);
+			byte[] idBuffer = BitConverter.GetBytes(id);
+			channel.SendAsync(new List<byte[]> { typeBuffer, idBuffer, responseBuffer });
 		}
 	}
 }

+ 3 - 1
CSharp/Game/Model/MessageAttribute.cs

@@ -1,4 +1,6 @@
-namespace Model
+using System;
+
+namespace Model
 {
 	/// <summary>
 	/// 搭配EventComponent用来分发消息

+ 50 - 0
CSharp/Game/Model/MessageType.cs

@@ -2,6 +2,56 @@
 {
 	public static class MessageType
 	{
+	#region client message 0
 		public const int CMsgLogin = 1;
+	#endregion client message 10000
+
+	#region server message 10000
+	#endregion server message 20000
+
+	#region rpc request message 20000
+	#endregion rpc request message 30000
+
+	#region rpc request message 30000
+	#endregion rpc request message 40000
+	}
+
+	public static class MessageTypeHelper
+	{
+		public static bool IsClientMessage(int opcode)
+		{
+			if (opcode > 0 && opcode < 10000)
+			{
+				return true;
+			}
+			return false;
+		}
+
+		public static bool IsServerMessage(int opcode)
+		{
+			if (opcode > 10000 && opcode < 20000)
+			{
+				return true;
+			}
+			return false;
+		}
+
+		public static bool IsRpcRequestMessage(int opcode)
+		{
+			if (opcode > 20000 && opcode < 30000)
+			{
+				return true;
+			}
+			return false;
+		}
+
+		public static bool IsRpcResponseMessage(int opcode)
+		{
+			if (opcode > 30000 && opcode < 40000)
+			{
+				return true;
+			}
+			return false;
+		}
 	}
 }

+ 0 - 65
CSharp/Platform/Common/Network/AChannel.cs

@@ -18,9 +18,7 @@ namespace Common.Network
 	public abstract class AChannel: Entity<AChannel>, IDisposable
 	{
 		protected IService service;
-		private int requestId;
 		protected Action<AChannel> onDispose = channel => { };
-		private readonly Dictionary<int, Action<byte[], bool>> requestCallback = new Dictionary<int, Action<byte[], bool>>();
 
 		protected AChannel(IService service)
 		{
@@ -58,68 +56,5 @@ namespace Common.Network
 		}
 
 		public abstract void Dispose();
-
-		// 消息回调或者超时回调
-		public void RequestCallback(int id, byte[] buffer, bool isOK)
-		{
-			Action<byte[], bool> action;
-			if (this.requestCallback.TryGetValue(id, out action))
-			{
-				action(buffer, isOK);
-			}
-			this.requestCallback.Remove(id);
-		}
-
-		/// <summary>
-		/// Rpc请求
-		/// </summary>
-		/// <typeparam name="T"></typeparam>
-		/// <typeparam name="K"></typeparam>
-		/// <param name="type"></param>
-		/// <param name="request"></param>
-		/// <param name="waitTime"></param>
-		/// <returns></returns>
-		public Task<T> Request<T, K>(short type, K request, int waitTime = 0)
-		{
-			++this.requestId;
-			byte[] requestBuffer = MongoHelper.ToBson(request);
-			byte[] typeBuffer = BitConverter.GetBytes(type);
-			byte[] idBuffer = BitConverter.GetBytes(this.requestId);
-			this.SendAsync(new List<byte[]> { typeBuffer, idBuffer, requestBuffer });
-			var tcs = new TaskCompletionSource<T>();
-			this.requestCallback[this.requestId] = (e, b) =>
-			{
-				if (b)
-				{
-					T response = MongoHelper.FromBson<T>(e, 6);
-					tcs.SetResult(response);
-				}
-				else
-				{
-					tcs.SetException(new Exception(string.Format("rpc timeout {0} {1}", type, MongoHelper.ToJson(request))));
-				}
-			};
-
-			if (waitTime > 0)
-			{
-				this.service.Timer.Add(TimeHelper.Now() + waitTime, () => { this.RequestCallback(this.requestId, null, false); });
-			}
-			return tcs.Task;
-		}
-
-		/// <summary>
-		/// Rpc响应
-		/// </summary>
-		/// <typeparam name="T"></typeparam>
-		/// <param name="type"></param>
-		/// <param name="id"></param>
-		/// <param name="response"></param>
-		public void Response<T>(short type, int id, T response)
-		{
-			byte[] responseBuffer = MongoHelper.ToBson(response);
-			byte[] typeBuffer = BitConverter.GetBytes(type);
-			byte[] idBuffer = BitConverter.GetBytes(id);
-			this.SendAsync(new List<byte[]> { typeBuffer, idBuffer, responseBuffer });
-		}
 	}
 }

+ 11 - 1
CSharp/Test/ModelTest/MongoDBTest.cs

@@ -1,5 +1,7 @@
-using Common.Helper;
+using System;
+using Common.Helper;
 using Model;
+using MongoDB.Bson;
 using MongoDB.Driver;
 using NUnit.Framework;
 
@@ -42,5 +44,13 @@ namespace MongoDBTest
 			//
 			//Assert.AreEqual(MongoHelper.ToJson(player1), MongoHelper.ToJson(player2));
 		}
+
+		[Test]
+		public void Test()
+		{
+			ObjectId id = ObjectId.GenerateNewId();
+			byte[] bytes = id.ToByteArray();
+			Console.WriteLine(bytes.Length);
+		}
 	}
 }