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

1.gate收到消息加上id转发给logic server.
2.logic server收到client消息交给相应的unit actor执行,其余的消息进行分发处理

tanghai 10 жил өмнө
parent
commit
093b0b60c4

+ 15 - 0
CSharp/Game/Controller/AddressHelper.cs

@@ -0,0 +1,15 @@
+using Model;
+
+namespace Controller
+{
+	public static class AddressHelper
+	{
+		public static string GetAddressByServerName(string serverName)
+		{
+			ServerInfoConfig serverInfoConfig = World.Instance.GetComponent<ConfigComponent>()
+						.GetCategory<ServerInfoCategory>()[serverName];
+			string address = serverInfoConfig.Host + ":" + serverInfoConfig.Port;
+			return address;
+		}
+	}
+}

+ 1 - 1
CSharp/Game/Controller/ConfigCategory/BuffCategory.cs

@@ -2,7 +2,7 @@
 
 namespace Controller
 {
-	[Config(ServerType.Realm | ServerType.Gate | ServerType.City)]
+	[Config(ServerType.City)]
 	public class BuffCategory: ACategory<BuffConfig>
 	{
 	}

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

@@ -38,6 +38,7 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AddressHelper.cs" />
     <Compile Include="Event\BuffTimeoutEvent.cs" />
     <Compile Include="BehaviorTreeNode\Not.cs" />
     <Compile Include="BehaviorTreeNode\Selector.cs" />
@@ -47,6 +48,8 @@
     <Compile Include="ConfigCategory\NodeCategory.cs" />
     <Compile Include="ConfigCategory\ServerInfoCategory.cs" />
     <Compile Include="ConfigCategory\UnitCategory.cs" />
+    <Compile Include="Event\LogicMessageEvent.cs" />
+    <Compile Include="Event\GateMessageEvent.cs" />
     <Compile Include="Factory\UnitFactory.cs" />
     <Compile Include="Message\CMsgLoginEvent.cs" />
     <Compile Include="NodeType.cs" />

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

@@ -0,0 +1,32 @@
+using System;
+using Common.Network;
+using Model;
+
+namespace Controller
+{
+	[Event(EventType.GateMessage, ServerType.Gate)]
+	public class GateMessageEvent : IEventSync
+	{
+		public void Run(Env env)
+		{
+			byte[] message = env.Get<byte[]>(EnvKey.Message);
+			AChannel channel = env.Get<AChannel>(EnvKey.Channel);
+
+			// 进行消息分发
+			int opcode = BitConverter.ToUInt16(message, 0);
+			if (!MessageTypeHelper.IsClientMessage(opcode))
+			{
+				return;
+			}
+
+			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);
+			string address = AddressHelper.GetAddressByServerName(channelUnitInfoComponent.ServerName);
+			World.Instance.GetComponent<NetworkComponent>().SendAsync(address, buffer);
+		}
+	}
+}

+ 33 - 0
CSharp/Game/Controller/Event/LogicMessageEvent.cs

@@ -0,0 +1,33 @@
+using System;
+using Model;
+using MongoDB.Bson;
+
+namespace Controller
+{
+	[Event(EventType.Message, ServerType.All)]
+	public class LogicMessageEvent : IEventSync
+	{
+		public void Run(Env env)
+		{
+			byte[] message = env.Get<byte[]>(EnvKey.Message);
+
+			int opcode = BitConverter.ToUInt16(message, 0);
+			// 如果是客户端消息,转交给unit actor处理
+			// 逻辑服收到客户端消息opcode(2) + id(12) + content
+			if (MessageTypeHelper.IsClientMessage(opcode))
+			{
+				byte[] idBuffer = new byte[12];
+				Array.Copy(message, 2, idBuffer, 0, 12);
+				ObjectId unitId = new ObjectId(idBuffer);
+				Unit unit = World.Instance.GetComponent<UnitComponent>().Get(unitId);
+				if (unit != null)
+				{
+					unit.GetComponent<ActorComponent>().Add(env);
+				}
+				return;
+			}
+
+			World.Instance.GetComponent<EventComponent<MessageAttribute>>().RunAsync(opcode, env);
+		}
+	}
+}

+ 1 - 13
CSharp/Game/Model/Component/GateNetworkComponent.cs

@@ -66,19 +66,7 @@ namespace Model
 				env[EnvKey.Channel] = channel;
 				env[EnvKey.Message] = message;
 
-				// 进行消息分发
-				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);
+				World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.GateMessage, env);
 			}
 		}
 	}

+ 6 - 6
CSharp/Game/Model/Component/NetworkComponent.cs

@@ -72,11 +72,6 @@ namespace Model
 				env[EnvKey.Message] = message;
 				int opcode = BitConverter.ToUInt16(message, 0);
 
-				if (MessageTypeHelper.IsClientMessage(opcode))
-				{
-					continue;
-				}
-
 				// 这个区间表示消息是rpc响应消息
 				if (MessageTypeHelper.IsRpcResponseMessage(opcode))
 				{
@@ -86,10 +81,15 @@ namespace Model
 				}
 
 				// 进行消息分发
-				World.Instance.GetComponent<EventComponent<MessageAttribute>>().RunAsync(opcode, env);
+				World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.GateMessage, env);
 			}
 		}
 
+		public async void SendAsync(string address, byte[] buffer)
+		{
+			
+		}
+
 		// 消息回调或者超时回调
 		public void RequestCallback(AChannel channel, int id, byte[] buffer, bool isOK)
 		{