Prechádzať zdrojové kódy

一个进程传送也测试OK

tanghai 8 rokov pred
rodič
commit
8d33d1e7ce

+ 9 - 2
Server/Hotfix/Handler/Actor_TransferHandler.cs

@@ -14,9 +14,14 @@ namespace Hotfix
 			try
 			try
 			{
 			{
 				long unitId = unit.Id;
 				long unitId = unit.Id;
+
+
 				// 先在location锁住unit的地址
 				// 先在location锁住unit的地址
 				await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId);
 				await Game.Scene.GetComponent<LocationProxyComponent>().Lock(unitId);
 
 
+				// 删除unit actorcomponent,让其它进程发送过来的消息找不到actor,重发
+				unit.RemoveComponent<ActorComponent>();
+				
 				int mapIndex = message.MapIndex;
 				int mapIndex = message.MapIndex;
 
 
 				StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
 				StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
@@ -31,9 +36,11 @@ namespace Hotfix
 				StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
 				StartConfig mapConfig = startConfigComponent.MapConfigs[mapIndex];
 				string address = mapConfig.GetComponent<InnerConfig>().Address;
 				string address = mapConfig.GetComponent<InnerConfig>().Address;
 				Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
 				Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(address);
-				await session.Call<M2M_TrasferUnitResponse>(new M2M_TrasferUnitRequest() { Unit = unit });
 
 
-				Game.Scene.GetComponent<UnitComponent>().Remove(unitId);
+				// 只删除不disponse否则M2M_TrasferUnitRequest无法序列化Unit
+				Game.Scene.GetComponent<UnitComponent>().RemoveNoDispose(unitId);
+				await session.Call<M2M_TrasferUnitResponse>(new M2M_TrasferUnitRequest() { Unit = unit });
+				unit.Dispose();
 
 
 				// 解锁unit的地址,并且更新unit的地址
 				// 解锁unit的地址,并且更新unit的地址
 				await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, mapConfig.AppId);
 				await Game.Scene.GetComponent<LocationProxyComponent>().UnLock(unitId, mapConfig.AppId);

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

@@ -15,7 +15,6 @@ namespace Hotfix
 				Log.Debug(MongoHelper.ToJson(message.Unit));
 				Log.Debug(MongoHelper.ToJson(message.Unit));
 				// 这里不需要注册location,因为unlock会更新位置
 				// 这里不需要注册location,因为unlock会更新位置
 				unit.AddComponent<ActorComponent>();
 				unit.AddComponent<ActorComponent>();
-				Game.Scene.GetComponent<UnitComponent>().Remove(unit.Id);
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);
 				Game.Scene.GetComponent<UnitComponent>().Add(unit);
 				reply(response);
 				reply(response);
 			}
 			}

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

@@ -13,6 +13,8 @@ namespace Model
 	{
 	{
 	}
 	}
 
 
+	[BsonKnownTypes(typeof(Actor_TestResponse))]
+	[BsonKnownTypes(typeof(Actor_TransferResponse))]
 	public abstract class AActorResponse : AResponse
 	public abstract class AActorResponse : AResponse
 	{
 	{
 	}
 	}

+ 6 - 2
Server/Model/Base/Message/AMActorHandler.cs

@@ -77,8 +77,12 @@ namespace Model
 					{
 					{
 						return;
 						return;
 					}
 					}
-					response.RpcId = message.RpcId;
-					session.Reply(response);
+					ActorRpcResponse actorResponse = new ActorRpcResponse
+					{
+						RpcId = message.RpcId,
+						AMessage = response
+					};
+					session.Reply(actorResponse);
 				});
 				});
 			}
 			}
 			catch (Exception e)
 			catch (Exception e)

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

@@ -2,12 +2,14 @@
 
 
 namespace Model
 namespace Model
 {
 {
+	[BsonKnownTypes(typeof(ARequest))]
+	[BsonKnownTypes(typeof(AResponse))]
 	[BsonKnownTypes(typeof(AActorMessage))]
 	[BsonKnownTypes(typeof(AActorMessage))]
-	[BsonKnownTypes(typeof(AActorRequest))]
 	public abstract class AMessage
 	public abstract class AMessage
 	{
 	{
 	}
 	}
 
 
+	[BsonKnownTypes(typeof(AActorRequest))]
 	public abstract class ARequest: AMessage
 	public abstract class ARequest: AMessage
 	{
 	{
 		[BsonIgnoreIfDefault]
 		[BsonIgnoreIfDefault]
@@ -17,6 +19,7 @@ namespace Model
 	/// <summary>
 	/// <summary>
 	/// 服务端回的RPC消息需要继承这个抽象类
 	/// 服务端回的RPC消息需要继承这个抽象类
 	/// </summary>
 	/// </summary>
+	[BsonKnownTypes(typeof(AActorResponse))]
 	public abstract class AResponse: AMessage
 	public abstract class AResponse: AMessage
 	{
 	{
 		public uint RpcId;
 		public uint RpcId;

+ 20 - 0
Server/Model/Base/Message/InnerMessageDispatcher.cs

@@ -6,12 +6,32 @@ namespace Model
 	{
 	{
 		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
 		public void Dispatch(Session session, ushort opcode, int offset, byte[] messageBytes, AMessage message)
 		{
 		{
+			// 收到actor rpc request
+			if (message is ActorRpcRequest actorRpcRequest)
+			{
+				Entity entity = Game.Scene.GetComponent<ActorManagerComponent>().Get(actorRpcRequest.Id);
+				if (entity == null)
+				{
+					Log.Warning($"not found actor: {actorRpcRequest.Id}");
+					ActorRpcResponse response = new ActorRpcResponse
+					{
+						RpcId = actorRpcRequest.RpcId,
+						Error = ErrorCode.ERR_NotFoundActor
+					};
+					session.Reply(response);
+					return;
+				}
+				entity.GetComponent<ActorComponent>().Add(new ActorMessageInfo() { Session = session, Message = actorRpcRequest });
+				return;
+			}
+
 			// 收到actor消息分发给actor自己去处理
 			// 收到actor消息分发给actor自己去处理
 			if (message is ActorRequest actorRequest)
 			if (message is ActorRequest actorRequest)
 			{
 			{
 				Entity entity = Game.Scene.GetComponent<ActorManagerComponent>().Get(actorRequest.Id);
 				Entity entity = Game.Scene.GetComponent<ActorManagerComponent>().Get(actorRequest.Id);
 				if (entity == null)
 				if (entity == null)
 				{
 				{
+					Log.Warning($"not found actor: {actorRequest.Id}");
 					ActorResponse response = new ActorResponse
 					ActorResponse response = new ActorResponse
 					{
 					{
 						RpcId = actorRequest.RpcId,
 						RpcId = actorRequest.RpcId,

+ 2 - 2
Server/Model/Component/LocationComponent.cs

@@ -159,8 +159,8 @@ namespace Model
 			{
 			{
 				Log.Error($"unlock appid is different {lockAppId} {unLockAppId}" );
 				Log.Error($"unlock appid is different {lockAppId} {unLockAppId}" );
 			}
 			}
-			Log.Info($"location unlock key: {key} unLockAppId: {unLockAppId}");
-			this.Add(key, value);
+			Log.Info($"location unlock key: {key} unLockAppId: {unLockAppId} new: {value}");
+			this.locations[key] = value;
 			this.UnLock(key);
 			this.UnLock(key);
 		}
 		}
 
 

+ 5 - 0
Server/Model/Component/UnitComponent.cs

@@ -26,6 +26,11 @@ namespace Model
 			unit?.Dispose();
 			unit?.Dispose();
 		}
 		}
 
 
+		public void RemoveNoDispose(long id)
+		{
+			this.idUnits.Remove(id);
+		}
+
 		public int Count
 		public int Count
 		{
 		{
 			get
 			get

+ 3 - 4
Server/Model/Entity/ActorProxy.cs

@@ -1,5 +1,4 @@
 using System;
 using System;
-using System.Collections.Generic;
 using System.Threading;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using MongoDB.Bson.Serialization.Attributes;
 using MongoDB.Bson.Serialization.Attributes;
@@ -59,11 +58,11 @@ namespace Model
 
 
 		public override async Task<AResponse> Run()
 		public override async Task<AResponse> Run()
 		{
 		{
-			ActorRequest request = new ActorRequest() { Id = this.proxy.Id, AMessage = this.message };
-			Response response = await this.proxy.RealCall<Response>(request, this.proxy.CancellationTokenSource.Token);
+			ActorRpcRequest request = new ActorRpcRequest() { Id = this.proxy.Id, AMessage = this.message };
+			ActorRpcResponse response = await this.proxy.RealCall<ActorRpcResponse>(request, this.proxy.CancellationTokenSource.Token);
 			if (response.Error != ErrorCode.ERR_NotFoundActor)
 			if (response.Error != ErrorCode.ERR_NotFoundActor)
 			{
 			{
-				this.Tcs.SetResult(response);
+				this.Tcs.SetResult((Response)response.AMessage);
 			}
 			}
 			return response;
 			return response;
 		}
 		}

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

@@ -5,7 +5,12 @@ using System.Collections.Generic;
using MongoDB.Bson.Serialization.Attributes;
 namespace Model
{
 namespace Model
{
 	/// <summary>
	/// 用来包装actor消息
	/// </summary>
	[Message(Opcode.ActorRequest)]
	[BsonIgnoreExtraElements]
	public class ActorRequest : ARequest
	{
		public long Id { get; set; }

		public AMessage AMessage { get; set; }
	}
 	/// <summary>
	/// 用来包装actor消息
	/// </summary>
	[Message(Opcode.ActorRequest)]
	[BsonIgnoreExtraElements]
	public class ActorRequest : ARequest
	{
		public long Id { get; set; }

		public AMessage AMessage { get; set; }
	}
 
 
-	/// <summary>
	/// actor RPC消息响应
	/// </summary>
	[Message(Opcode.ActorResponse)]
	[BsonIgnoreExtraElements]
	public class ActorResponse : AResponse
	{
	}


	/// <summary>
	/// 传送unit
	/// </summary>
	[Message(Opcode.M2M_TrasferUnitRequest)]
	[BsonIgnoreExtraElements]
	public class M2M_TrasferUnitRequest : ARequest
	{
		public Unit Unit;
	}
	
	[Message(Opcode.M2M_TrasferUnitResponse)]
	[BsonIgnoreExtraElements]
	public class M2M_TrasferUnitResponse : AResponse
	{
	}
	


	[Message(Opcode.M2A_Reload)]
	[BsonIgnoreExtraElements]
	public class M2A_Reload : ARequest
	{
	}
+	/// <summary>
	/// actor RPC消息响应
	/// </summary>
	[Message(Opcode.ActorResponse)]
	[BsonIgnoreExtraElements]
	public class ActorResponse : AResponse
	{
	}

	/// <summary>
	/// 用来包装actor消息
	/// </summary>
	[Message(Opcode.ActorRpcRequest)]
	[BsonIgnoreExtraElements]
	public class ActorRpcRequest : ActorRequest
	{
	}
+
+	/// <summary>
	/// actor RPC消息响应带回应
	/// </summary>
    [Message(Opcode.ActorRpcResponse)]
	[BsonIgnoreExtraElements]
	public class ActorRpcResponse : ActorResponse
	{
		public AMessage AMessage { get; set; }
	}
+
+
+	/// <summary>
	/// 传送unit
	/// </summary>
    [Message(Opcode.M2M_TrasferUnitRequest)]
	[BsonIgnoreExtraElements]
	public class M2M_TrasferUnitRequest : ARequest
	{
		public Unit Unit;
	}
	
	[Message(Opcode.M2M_TrasferUnitResponse)]
	[BsonIgnoreExtraElements]
	public class M2M_TrasferUnitResponse : AResponse
	{
	}
	


	[Message(Opcode.M2A_Reload)]
	[BsonIgnoreExtraElements]
	public class M2A_Reload : ARequest
	{
	}
 
 
 
	[Message(Opcode.A2M_Reload)]
	[BsonIgnoreExtraElements]
	public class A2M_Reload : AResponse
	{
	}
 
	[Message(Opcode.A2M_Reload)]
	[BsonIgnoreExtraElements]
	public class A2M_Reload : AResponse
	{
	}
 
 

+ 2 - 1
Server/Model/Entity/Message/InnerOpcode.cs

@@ -4,7 +4,8 @@
 	{
 	{
 		public const ushort ActorRequest = 1;
 		public const ushort ActorRequest = 1;
 		public const ushort ActorResponse = 2;
 		public const ushort ActorResponse = 2;
-		public const ushort ActorResponseWithM = 3;
+		public const ushort ActorRpcRequest = 3;
+		public const ushort ActorRpcResponse = 4;
 		public const ushort G2G_LockRequest = 10;
 		public const ushort G2G_LockRequest = 10;
 		public const ushort G2G_LockResponse = 11;
 		public const ushort G2G_LockResponse = 11;
 		public const ushort G2G_LockReleaseRequest = 12;
 		public const ushort G2G_LockReleaseRequest = 12;