Преглед изворни кода

多进程actor模式测试通过

tanghai пре 8 година
родитељ
комит
bd7c17d76d

+ 8 - 6
Config/StartConfig/127.0.0.1.txt

@@ -1,6 +1,8 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10000 }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20001 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10001 }], "AppId" : 2, "AppType" : "Realm", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20002 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10002 }], "AppId" : 3, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20003 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10003 }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20004 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10004 }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20005 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10005 }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10000, "Host2" : null }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98565862653976"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("98565862653977"), "Host" : "127.0.0.1", "Port" : 10001 }], "AppId" : 2, "AppType" : "Location", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20001 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }], "AppId" : 3, "AppType" : "Realm", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20002 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10003, "Host2" : null }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20003 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10004, "Host2" : null }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20004 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10005, "Host2" : null }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20005 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10006, "Host2" : null }], "AppId" : 7, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("98565723062317"), "Host" : "127.0.0.1", "Port" : 20006 }], "AppId" : 8, "AppType" : "Map", "ServerIP" : "*" }

+ 1 - 1
Config/StartConfig/LocalAllServer.txt

@@ -1 +1 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "_id" : NumberLong("98547768819755"), "Host" : "127.0.0.1", "Port" : 10001, "Host2" : null }, { "_t" : "InnerConfig", "_id" : NumberLong("98547768819756"), "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "_id" : NumberLong("98547768819757"), "Url" : "", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "_id" : NumberLong("98547768819758"), "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "_id" : NumberLong("98547768819755"), "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }, { "_t" : "InnerConfig", "_id" : NumberLong("98547768819756"), "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "_id" : NumberLong("98547768819757"), "Url" : "", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "_id" : NumberLong("98547768819758"), "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }

+ 3 - 2
Server/App/NLog.config

@@ -7,13 +7,14 @@
 		<target name="all" xsi:type="File"
 			fileName="${basedir}/../Logs/Log.txt"
 			deleteOldFileOnStartup="false"
-		layout="${longdate} ${var:appType} ${var:appId} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
+			layout="${longdate} ${var:appType} ${var:appId} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
 	</targets>
+	
 	<targets>
 		<target name="debug" xsi:type="File"
 			fileName="${basedir}/../Logs/Log-${var:appType}-${var:appId}-Debug.txt"
 			deleteOldFileOnStartup="false"
-			layout="${longdate} ${var:appType} ${var:appId} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message} ${stacktrace:format=Flat:topFrames=100:skipFrames=2}" />
+			layout="${longdate} ${var:appType} ${var:appId} ${callsite:className=false:methodName=false:fileName=true:includeSourcePath=false:skipFrames=2} ${message}" />
 	</targets>
 
 	<targets>

+ 5 - 2
Server/App/Program.cs

@@ -59,6 +59,7 @@ namespace App
 						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);
 						Game.Scene.AddComponent<LocationProxyComponent>();
 						Game.Scene.AddComponent<ActorComponent>();
+						Game.Scene.AddComponent<ActorProxyComponent>();
 						Game.Scene.AddComponent<GateSessionKeyComponent>();
 						break;
 					case AppType.Location:
@@ -67,10 +68,12 @@ namespace App
 						break;
 					case AppType.Map:
 						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
-						Game.Scene.AddComponent<LocationComponent>();
+						Game.Scene.AddComponent<ActorManagerComponent>();
 						Game.Scene.AddComponent<UnitComponent>();
 						Game.Scene.AddComponent<LocationProxyComponent>();
 						Game.Scene.AddComponent<ActorComponent>();
+						Game.Scene.AddComponent<ActorProxyComponent>();
+						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						break;
 					case AppType.AllServer:
 						Game.Scene.AddComponent<ActorProxyComponent>();
@@ -79,7 +82,7 @@ namespace App
 						Game.Scene.AddComponent<DBComponent>();
 						Game.Scene.AddComponent<DBProxyComponent>();
 						Game.Scene.AddComponent<LocationComponent>();
-						Game.Scene.AddComponent<ActorMessageDispatherComponent, AppType>(AppType.AllServer);
+						Game.Scene.AddComponent<ActorMessageDispatherComponent>();
 						Game.Scene.AddComponent<ActorManagerComponent>();
 						Game.Scene.AddComponent<NetInnerComponent, string, int>(innerConfig.Host, innerConfig.Port);
 						Game.Scene.AddComponent<NetOuterComponent, string, int>(outerConfig.Host, outerConfig.Port);

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

@@ -9,7 +9,6 @@ namespace Hotfix
 	{
 		protected override async Task<bool> Run(Unit entity, ActorRpc_TestRequest message, Action<ActorRpc_TestResponse> reply)
 		{
-			Log.Info(message.request);
 			reply(new ActorRpc_TestResponse() {response = "response actor rpc"});
 			return true;
 		}

+ 13 - 15
Server/Model/Component/ActorMessageDispatherComponent.cs

@@ -5,11 +5,11 @@ using System.Threading.Tasks;
 namespace Model
 {
 	[ObjectEvent]
-	public class ActorMessageDispatherComponentEvent : ObjectEvent<ActorMessageDispatherComponent>, IAwake<AppType>, ILoad
+	public class ActorMessageDispatherComponentEvent : ObjectEvent<ActorMessageDispatherComponent>, IStart, ILoad
 	{
-		public void Awake(AppType appType)
+		public void Start()
 		{
-			this.Get().Awake(appType);
+			this.Get().Start();
 		}
 
 		public void Load()
@@ -26,9 +26,10 @@ namespace Model
 		private AppType AppType;
 		private Dictionary<Type, IMActorHandler> handlers;
 		
-		public void Awake(AppType appType)
+		public void Start()
 		{
-			this.AppType = appType;
+			StartConfig startConfig = this.GetComponent<StartConfigComponent>().StartConfig;
+			this.AppType = startConfig.AppType;
 			this.Load();
 		}
 
@@ -73,21 +74,18 @@ namespace Model
 
 		public async Task<bool> Handle(Session session, Entity entity, ActorRequest message)
 		{
-			ARequest request = message.AMessage as ARequest;
-			if (request == null)
+			if (!this.handlers.TryGetValue(message.AMessage.GetType(), out IMActorHandler handler))
 			{
-				Log.Error($"ActorRequest.AMessage as ARequest fail: {message.AMessage.GetType().FullName}");
+				Log.Error($"not found message handler: {message.GetType().FullName}");
 				return false;
 			}
-
-			request.RpcId = message.RpcId;
-
-			if (!this.handlers.TryGetValue(request.GetType(), out IMActorHandler handler))
+			
+			if (message.AMessage is ARequest request)
 			{
-				Log.Error($"not found message handler: {message.GetType().FullName}");
-				return false;
+				request.RpcId = message.RpcId;
 			}
-			return await handler.Handle(session, entity, request);
+
+			return await handler.Handle(session, entity, message.AMessage);
 		}
 
 		public override void Dispose()

+ 4 - 2
Server/Model/Component/LocationProxyComponent.cs

@@ -19,8 +19,10 @@ namespace Model
 
 		public void Awake()
 		{
-			StartConfig startConfig = Game.Scene.GetComponent<StartConfigComponent>().LocationConfig;
-			this.AppId = startConfig.AppId;
+			StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
+			this.AppId = startConfigComponent.StartConfig.AppId;
+
+			StartConfig startConfig = startConfigComponent.LocationConfig;
 			this.LocationAddress = startConfig.GetComponent<InnerConfig>().Address;
 		}
 

+ 0 - 1
Server/Model/Component/StartConfigComponent.cs

@@ -44,7 +44,6 @@ namespace Model
 					StartConfig startConfig = MongoHelper.FromJson<StartConfig>(s2);
 					this.allConfigs.Add(startConfig);
 					this.configDict.Add(startConfig.AppId, startConfig);
-
 					if (startConfig.AppType.Is(AppType.Realm))
 					{
 						this.RealmConfig = startConfig;

+ 5 - 1
Server/Model/Message/AActorMessage.cs

@@ -1,9 +1,13 @@
-namespace Model
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Model
 {
+	[BsonKnownTypes(typeof(Actor_Test))]
 	public abstract class AActorMessage : AMessage
 	{
 	}
 
+	[BsonKnownTypes(typeof(ActorRpc_TestRequest))]
 	public abstract class AActorRequest : ARequest
 	{
 	}

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

@@ -2,6 +2,8 @@
 
 namespace Model
 {
+	[BsonKnownTypes(typeof(AActorMessage))]
+	[BsonKnownTypes(typeof(AActorRequest))]
 	public abstract class AMessage
 	{
 	}

+ 0 - 1
Server/Model/Message/InnerMessageDispatcher.cs

@@ -9,7 +9,6 @@ namespace Model
 			// 收到actor消息分发给actor自己去处理
 			if (message is ActorRequest actorRequest)
 			{
-				//Log.Info(MongoHelper.ToJson(actorRequest));
 				Entity entity = Game.Scene.GetComponent<ActorManagerComponent>().Get(actorRequest.Id);
 				entity.GetComponent<ActorComponent>().Add(new ActorMessageInfo() { Session = session, Message = actorRequest });
 				return;

+ 1 - 1
Unity/Assets/Editor/ServerCommandLineEditor/ServerCommandLineEditor.cs

@@ -304,7 +304,7 @@ namespace MyEditor
 					newStartConfig.AddComponent<OuterConfig>();
 				}
 
-				if (this.AppType.Is(AppType.Gate | AppType.Realm | AppType.Manager | AppType.Http | AppType.DB | AppType.Map))
+				if (this.AppType.Is(AppType.Gate | AppType.Realm | AppType.Manager | AppType.Http | AppType.DB | AppType.Map | AppType.Location))
 				{
 					newStartConfig.AddComponent<InnerConfig>();
 				}

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

@@ -6,8 +6,6 @@ namespace Model
 	{
 		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, object message)
 		{
-			//Log.Debug($"recv {JsonHelper.ToJson(message)}");
-
 			// 普通消息或者是Rpc请求消息
 			if (message is AMessage || message is ARequest)
 			{

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

@@ -1,5 +1,7 @@
 // 服务器与客户端之间的消息 Opcode从1-9999
 
+using MongoDB.Bson.Serialization;
+
 namespace Model
 {
 	[Message(Opcode.C2R_Login)]

+ 3 - 1
Unity/Assets/Scripts/Entity/Session.cs

@@ -102,6 +102,8 @@ namespace Model
 			Type messageType = this.network.Owner.GetComponent<OpcodeTypeComponent>().GetType(opcode);
 			object message = this.network.MessagePacker.DeserializeFrom(messageType, messageBytes, offset, messageBytes.Length - offset);
 
+			//Log.Debug($"recv: {JsonHelper.ToJson(message)}");
+
 			AResponse response = message as AResponse;
 			if (response != null)
 			{
@@ -174,7 +176,7 @@ namespace Model
 						tcs.SetException(new RpcException(response.Error, response.Message));
 						return;
 					}
-					//Log.Debug($"recv: {response.ToJson()}");
+					//Log.Debug($"recv: {JsonHelper.ToJson(response)}");
 					tcs.SetResult(response);
 				}
 				catch (Exception e)

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

@@ -22,7 +22,7 @@ namespace Hotfix
 			Session session = null;
 			try
 			{
-				session = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10001");
+				session = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create("127.0.0.1:10002");
 				R2C_Login r2CLogin = await session.Call<R2C_Login>(new C2R_Login() { Account = "abcdef", Password = "111111" });
 				Session gateSession = Hotfix.Scene.ModelScene.GetComponent<NetOuterComponent>().Create(r2CLogin.Address);
 				G2C_LoginGate g2CLoginGate = await gateSession.Call<G2C_LoginGate>(new C2G_LoginGate(r2CLogin.Key));