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

actor模型测试通过,客户端发送一个actor_test消息到gate,gate转发给map上的unit(actor),unit收到Actor_test消息回发给gatesession(actor),gatesession将收到的actor_test消息回发给client端
消息路径如下:
client->gate->map->gate->client

tanghai 8 лет назад
Родитель
Сommit
a21c68d1b4
27 измененных файлов с 224 добавлено и 11 удалено
  1. 2 0
      Server/Hotfix/Handler/Actor_TestHandler.cs
  2. 2 1
      Server/Hotfix/Handler/C2G_LoginGateHandler.cs
  3. 1 0
      Server/Hotfix/Handler/G2M_CreateUnitHandler.cs
  4. 26 0
      Server/Model/Component/Unit/UnitGateComponent.cs
  5. 2 0
      Server/Model/Entity/ActorProxy.cs
  6. 1 1
      Server/Model/Entity/Message/InnerMessage.cs
  7. 2 0
      Server/Model/Message/AActorMessage.cs
  8. 4 1
      Server/Model/Message/AMessage.cs
  9. 1 0
      Server/Model/Server.Model.csproj
  10. 24 0
      Unity/Assets/Scripts/Base/Message/AMHandler.cs
  11. 3 0
      Unity/Assets/Scripts/Base/Message/AMHandler.cs.meta
  12. 4 0
      Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs
  13. 10 0
      Unity/Assets/Scripts/Base/Message/IMHandler.cs
  14. 3 0
      Unity/Assets/Scripts/Base/Message/IMHandler.cs.meta
  15. 74 0
      Unity/Assets/Scripts/Component/MessageDispatherComponent.cs
  16. 3 0
      Unity/Assets/Scripts/Component/MessageDispatherComponent.cs.meta
  17. 1 1
      Unity/Assets/Scripts/Entity/Message/Opcode.cs
  18. 3 0
      Unity/Assets/Scripts/Handler.meta
  19. 11 0
      Unity/Assets/Scripts/Handler/Actor_TestHandler.cs
  20. 3 0
      Unity/Assets/Scripts/Handler/Actor_TestHandler.cs.meta
  21. 2 1
      Unity/Hotfix/Base/Message/AMHandler.cs
  22. 27 0
      Unity/Hotfix/Base/Message/AMessage.cs
  23. 1 1
      Unity/Hotfix/Base/Message/IMHandler.cs
  24. 1 0
      Unity/Hotfix/Init.cs
  25. 1 1
      Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs
  26. 1 0
      Unity/Hotfix/Unity.Hotfix.csproj
  27. 11 4
      Unity/Unity.csproj

+ 2 - 0
Server/Hotfix/Handler/Actor_TestHandler.cs

@@ -9,6 +9,8 @@ namespace Hotfix
 		protected override async Task<bool> Run(Unit unit, Actor_Test message)
 		{
 			Log.Info(message.Info);
+
+			unit.GetComponent<UnitGateComponent>().GetActorProxy().Send(message);
 			return true;
 		}
 	}

+ 2 - 1
Server/Hotfix/Handler/C2G_LoginGateHandler.cs

@@ -22,11 +22,12 @@ namespace Hotfix
 				Player player = ObjectFactory.Create<Player, string>(account);
 				Game.Scene.GetComponent<PlayerComponent>().Add(player);
 				session.AddComponent<SessionPlayerComponent>().Player = player;
+				session.AddComponent<ActorComponent, IEntityActorHandler>(new GateSessionEntityActorHandler());
 
 				// 在map服务器上创建战斗Unit
 				string mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfig.GetComponent<InnerConfig>().Address;
 				Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
-				M2G_CreateUnit createUnit = await mapSession.Call<M2G_CreateUnit>(new G2M_CreateUnit() { PlayerId = player.Id });
+				M2G_CreateUnit createUnit = await mapSession.Call<M2G_CreateUnit>(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.Id });
 				player.UnitId = createUnit.UnitId;
 
 				response.PlayerId = player.Id;

+ 1 - 0
Server/Hotfix/Handler/G2M_CreateUnitHandler.cs

@@ -13,6 +13,7 @@ namespace Hotfix
 			{
 				Unit unit = ObjectFactory.Create<Unit, UnitType>(UnitType.Hero);
 				unit.AddComponent<ActorComponent, IEntityActorHandler>(new CommonEntityActorHandler());
+				unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);
 				response.UnitId = unit.Id;
 				reply(response);

+ 26 - 0
Server/Model/Component/Unit/UnitGateComponent.cs

@@ -0,0 +1,26 @@
+namespace Model
+{
+	[ObjectEvent]
+	public class UnitGateComponentEvent : ObjectEvent<UnitGateComponent>, IAwake<long>
+	{
+		public void Awake(long gateSessionId)
+		{
+			this.Get().Awake(gateSessionId);
+		}
+	}
+
+	public class UnitGateComponent : Component
+	{
+		public long GateSessionId;
+
+		public void Awake(long gateSessionId)
+		{
+			this.GateSessionId = gateSessionId;
+		}
+
+		public ActorProxy GetActorProxy()
+		{
+			return Game.Scene.GetComponent<ActorProxyComponent>().Get(this.GateSessionId);
+		}
+	}
+}

+ 2 - 0
Server/Model/Entity/ActorProxy.cs

@@ -230,12 +230,14 @@ namespace Model
 
 		public void Send(AActorMessage message)
 		{
+			message.Id = this.Id;
 			ActorMessageTask task = new ActorMessageTask(this, message);
 			this.Add(task);
 		}
 
 		public Task<Response> Call<Response>(AActorRequest request)where Response : AActorResponse
 		{
+			request.Id = this.Id;
 			ActorRpcTask<Response> task = new ActorRpcTask<Response>(this, request);
 			this.Add(task);
 			return task.Tcs.Task;

+ 1 - 1
Server/Model/Entity/Message/InnerMessage.cs

@@ -56,4 +56,4 @@ namespace Model
{
	[Message(Opcode.M2A_Reload)]
	[BsonIgnoreExtraElements]
	publ
 
 	[Message(Opcode.ObjectGetRequest)]
	[BsonIgnoreExtraElements]
	public class ObjectGetRequest : ARequest
	{
		public long Key { get; set; }
	}
 
-	[Message(Opcode.ObjectGetResponse)]
	[BsonIgnoreExtraElements]
	public class ObjectGetResponse : AResponse
	{
		public int AppId { get; set; }
	}


	[Message(Opcode.R2G_GetLoginKey)]
	[BsonIgnoreExtraElements]
	public class R2G_GetLoginKey : ARequest
	{
		public string Account;
	}


	[Message(Opcode.G2R_GetLoginKey)]
	[BsonIgnoreExtraElements]
	public class G2R_GetLoginKey : AResponse
	{
		public long Key;

		public G2R_GetLoginKey()
		{
		}

		public G2R_GetLoginKey(long key)
		{
			this.Key = key;
		}
	}


	[Message(Opcode.G2M_CreateUnit)]
	[BsonIgnoreExtraElements]
	public class G2M_CreateUnit : ARequest
	{
		public long PlayerId;
	}


	[Message(Opcode.M2G_CreateUnit)]
	[BsonIgnoreExtraElements]
	public class M2G_CreateUnit : AResponse
	{
		public long UnitId;
	}
}
+	[Message(Opcode.ObjectGetResponse)]
	[BsonIgnoreExtraElements]
	public class ObjectGetResponse : AResponse
	{
		public int AppId { get; set; }
	}


	[Message(Opcode.R2G_GetLoginKey)]
	[BsonIgnoreExtraElements]
	public class R2G_GetLoginKey : ARequest
	{
		public string Account;
	}


	[Message(Opcode.G2R_GetLoginKey)]
	[BsonIgnoreExtraElements]
	public class G2R_GetLoginKey : AResponse
	{
		public long Key;

		public G2R_GetLoginKey()
		{
		}

		public G2R_GetLoginKey(long key)
		{
			this.Key = key;
		}
	}


	[Message(Opcode.G2M_CreateUnit)]
	[BsonIgnoreExtraElements]
	public class G2M_CreateUnit : ARequest
	{
		public long PlayerId;
		public long GateSessionId;
	}


	[Message(Opcode.M2G_CreateUnit)]
	[BsonIgnoreExtraElements]
	public class M2G_CreateUnit : AResponse
	{
		public long UnitId;
	}
}

+ 2 - 0
Server/Model/Message/AActorMessage.cs

@@ -4,11 +4,13 @@ namespace Model
 {
 	public interface IActorMessage
 	{
+		[BsonIgnoreIfDefault]
 		long Id { get; set; }
 	}
 
 	public abstract class AActorMessage: ARequest, IActorMessage
 	{
+		[BsonIgnoreIfDefault]
 		public long Id { get; set; }
 	}
 

+ 4 - 1
Server/Model/Message/AMessage.cs

@@ -1,4 +1,6 @@
-namespace Model
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
 {
 	public abstract class AMessage
 	{
@@ -6,6 +8,7 @@
 
 	public abstract class ARequest: AMessage
 	{
+		[BsonIgnoreIfDefault]
 		public uint RpcId;
 	}
 

+ 1 - 0
Server/Model/Server.Model.csproj

@@ -69,6 +69,7 @@
     </Compile>
     <Compile Include="Component\ActorProxyComponent.cs" />
     <Compile Include="Component\PlayerComponent.cs" />
+    <Compile Include="Component\Unit\UnitGateComponent.cs" />
     <Compile Include="Entity\Player.cs" />
     <Compile Include="Entity\Location.cs" />
     <Compile Include="Helper\ObjectFactory.cs" />

+ 24 - 0
Unity/Assets/Scripts/Base/Message/AMHandler.cs

@@ -0,0 +1,24 @@
+using System;
+
+namespace Model
+{
+	public abstract class AMHandler<Message> : IMHandler where Message : AMessage
+	{
+		protected abstract void Run(Message message);
+
+		public void Handle(object msg)
+		{
+			Message message = msg as Message;
+			if (message == null)
+			{
+				Log.Error($"消息类型转换错误: {msg.GetType().Name} to {typeof(Message).Name}");
+			}
+			this.Run(message);
+		}
+
+		public Type GetMessageType()
+		{
+			return typeof(Message);
+		}
+	}
+}

+ 3 - 0
Unity/Assets/Scripts/Base/Message/AMHandler.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 51f27baf9bcf4588a008ab171d42ffd7
+timeCreated: 1503744375

+ 4 - 0
Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs

@@ -16,6 +16,10 @@ namespace Model
 				{
 					Game.Scene.GetComponent<CrossComponent>().Run(CrossIdType.MessageDeserializeFinish, messageInfo);
 				}
+				else
+				{
+					Game.Scene.GetComponent<MessageDispatherComponent>().Handle(messageInfo);
+				}
 				return;
 			}
 

+ 10 - 0
Unity/Assets/Scripts/Base/Message/IMHandler.cs

@@ -0,0 +1,10 @@
+using System;
+
+namespace Model
+{
+	public interface IMHandler
+	{
+		void Handle(object message);
+		Type GetMessageType();
+	}
+}

+ 3 - 0
Unity/Assets/Scripts/Base/Message/IMHandler.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 6081581f6dde45a0889b8593bdedf860
+timeCreated: 1503744375

+ 74 - 0
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Generic;
+
+namespace Model
+{
+	/// <summary>
+	/// 消息分发组件
+	/// </summary>
+	[ObjectEvent(EntityEventId.MessageDispatherComponent)]
+	public class MessageDispatherComponent : Component, IAwake, ILoad
+	{
+		private Dictionary<ushort, List<IMHandler>> handlers;
+
+		public void Awake()
+		{
+			this.Load();
+		}
+
+		public void Load()
+		{
+			handlers = new Dictionary<ushort, List<IMHandler>>();
+
+			Type[] types = DllHelper.GetMonoTypes();
+
+			foreach (Type type in types)
+			{
+				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
+				IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
+				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
+				{
+					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMHandler>());
+				}
+				this.handlers[messageHandlerAttribute.Opcode].Add(iMHandler);
+			}
+		}
+
+		public void Handle(MessageInfo messageInfo)
+		{
+			List<IMHandler> actions;
+			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
+			{
+				Log.Error($"消息 {messageInfo.Opcode} 没有处理");
+				return;
+			}
+
+			foreach (IMHandler ev in actions)
+			{
+				try
+				{
+					ev.Handle(messageInfo.Message);
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
+			}
+		}
+
+		public override void Dispose()
+		{
+			if (this.Id == 0)
+			{
+				return;
+			}
+
+			base.Dispose();
+		}
+	}
+}

+ 3 - 0
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: f6a1591f4b7a4121a01428af98aed2c1
+timeCreated: 1503745497

+ 1 - 1
Unity/Assets/Scripts/Entity/Message/Opcode.cs

@@ -11,6 +11,6 @@
 		public const ushort G2C_GetPlayerInfo = 1007;
 		public const ushort C2M_Reload = 1008;
 
-		public const ushort Actor_Test = 1009;
+		public const ushort Actor_Test = 2001;
 	}
 }

+ 3 - 0
Unity/Assets/Scripts/Handler.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 1760c4c295484ca7ba64936681612d21
+timeCreated: 1503744277

+ 11 - 0
Unity/Assets/Scripts/Handler/Actor_TestHandler.cs

@@ -0,0 +1,11 @@
+namespace Model
+{
+	[MessageHandler(Opcode.Actor_Test)]
+	public class Actor_TestHandler : AMHandler<Actor_Test>
+	{
+		protected override void Run(Actor_Test message)
+		{
+			Log.Debug(message.Info);
+		}
+	}
+}

+ 3 - 0
Unity/Assets/Scripts/Handler/Actor_TestHandler.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: ddd6230b24c24bb3b358f5de9ba82bb5
+timeCreated: 1503744322

+ 2 - 1
Unity/Hotfix/Base/Message/AMHandler.cs

@@ -1,6 +1,7 @@
 using System;
+using Model;
 
-namespace Model
+namespace Hotfix
 {
 	public abstract class AMHandler<Message> : IMHandler where Message : AMessage
 	{

+ 27 - 0
Unity/Hotfix/Base/Message/AMessage.cs

@@ -0,0 +1,27 @@
+namespace Hotfix
+{
+	public abstract class AMessage
+	{
+	}
+
+	public abstract class ARequest: AMessage
+	{
+		public uint RpcId;
+	}
+
+	/// <summary>
+	/// 服务端回的RPC消息需要继承这个抽象类
+	/// </summary>
+	public abstract class AResponse
+	{
+		public uint RpcId;
+
+		public int Error = 0;
+		public string Message = "";
+	}
+
+	public abstract class AFrameMessage : AMessage
+	{
+		public long Id;
+	}
+}

+ 1 - 1
Unity/Hotfix/Base/Message/IMHandler.cs

@@ -1,6 +1,6 @@
 using System;
 
-namespace Model
+namespace Hotfix
 {
 	public interface IMHandler
 	{

+ 1 - 0
Unity/Hotfix/Init.cs

@@ -11,6 +11,7 @@ namespace Hotfix
 			{
 				Hotfix.Scene.ModelScene = Game.Scene;
 				Hotfix.Scene.ModelScene.AddComponent<OpcodeTypeComponent>();
+				Hotfix.Scene.ModelScene.AddComponent<Model.MessageDispatherComponent>();
 				Hotfix.Scene.ModelScene.AddComponent<NetOuterComponent>();
 				Hotfix.Scene.ModelScene.AddComponent<ResourcesComponent>();
 				Hotfix.Scene.ModelScene.AddComponent<BehaviorTreeComponent>();  

+ 1 - 1
Unity/Hotfix/UI/UILobby/Component/UILobbyComponent.cs

@@ -32,7 +32,7 @@ namespace Hotfix
 				Log.Info("登陆gate成功!");
 
 				// 发送一个actor消息
-				gateSession.Send(new Actor_Test() { Info = "send to actor" });
+				gateSession.Send(new Actor_Test() { Info = "message client->gate->map->gate->client" });
 			}
 			catch (Exception e)
 			{

+ 1 - 0
Unity/Hotfix/Unity.Hotfix.csproj

@@ -56,6 +56,7 @@
   </ItemGroup>
   <ItemGroup>
     <Compile Include="Base\Helper\ExceptionHelper.cs" />
+    <Compile Include="Base\Message\AMessage.cs" />
     <Compile Include="Base\Message\AMHandler.cs" />
     <Compile Include="Base\Message\IMHandler.cs" />
     <Compile Include="Component\MessageDispatherComponent.cs" />

+ 11 - 4
Unity/Unity.csproj

@@ -12,12 +12,15 @@
     <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>
     <UnityProjectType>Game:1</UnityProjectType>
     <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
     <UnityVersion>2017.1.0f3</UnityVersion>
-    <RootNamespace></RootNamespace>
+    <RootNamespace>
+    </RootNamespace>
     <LangVersion>6</LangVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@@ -371,11 +374,13 @@
     <Compile Include="Assets\Scripts\Base\LogType.cs" />
     <Compile Include="Assets\Scripts\Base\Message\AActorMessage.cs" />
     <Compile Include="Assets\Scripts\Base\Message\AMessage.cs" />
+    <Compile Include="Assets\Scripts\Base\Message\AMHandler.cs" />
     <Compile Include="Assets\Scripts\Base\Message\AppType.cs" />
     <Compile Include="Assets\Scripts\Base\Message\ClientDispatcher.cs" />
     <Compile Include="Assets\Scripts\Base\Message\ErrorCode.cs" />
     <Compile Include="Assets\Scripts\Base\Message\IMessageDispatcher.cs" />
     <Compile Include="Assets\Scripts\Base\Message\IMessagePacker.cs" />
+    <Compile Include="Assets\Scripts\Base\Message\IMHandler.cs" />
     <Compile Include="Assets\Scripts\Base\Message\JsondotnetPacker.cs" />
     <Compile Include="Assets\Scripts\Base\Message\MessageAttribute.cs" />
     <Compile Include="Assets\Scripts\Base\Message\MessageHandlerAttribute.cs" />
@@ -486,6 +491,7 @@
     <Compile Include="Assets\Scripts\Component\Config\VersionConfig.cs" />
     <Compile Include="Assets\Scripts\Component\CrossComponent.cs" />
     <Compile Include="Assets\Scripts\Component\EventComponent.cs" />
+    <Compile Include="Assets\Scripts\Component\MessageDispatherComponent.cs" />
     <Compile Include="Assets\Scripts\Component\NetOuterComponent.cs" />
     <Compile Include="Assets\Scripts\Component\NetworkComponent.cs" />
     <Compile Include="Assets\Scripts\Component\OpcodeTypeComponent.cs" />
@@ -501,6 +507,7 @@
     <Compile Include="Assets\Scripts\Entity\Scene.cs" />
     <Compile Include="Assets\Scripts\Entity\Session.cs" />
     <Compile Include="Assets\Scripts\Entity\WWWAsync.cs" />
+    <Compile Include="Assets\Scripts\Handler\Actor_TestHandler.cs" />
     <Compile Include="Assets\Scripts\Helper\ActionHelper.cs" />
     <Compile Include="Assets\Scripts\Helper\GameObjectHelper.cs" />
     <Compile Include="Assets\Scripts\Helper\JsonHelper.cs" />
@@ -529,4 +536,4 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Target Name="GenerateTargetFrameworkMonikerAttribute" />
-</Project>
+</Project>