Quellcode durchsuchen

SessionWrap干脆就改成叫Session了,一个ETModel层的Session,一个ETHotfix层的Session

tanghai vor 7 Jahren
Ursprung
Commit
c1551ad26a

+ 1 - 1
Unity/Hotfix/Module/FrameSync/Actor_CreateUnitsHandler.cs

@@ -6,7 +6,7 @@ namespace ETHotfix
 	[MessageHandler]
 	public class Actor_CreateUnitsHandler : AMHandler<Actor_CreateUnits>
 	{
-		protected override void Run(Session session, Actor_CreateUnits message)
+		protected override void Run(ETModel.Session session, Actor_CreateUnits message)
 		{
 			// 加载Unit资源
 			ResourcesComponent resourcesComponent = ETModel.Game.Scene.GetComponent<ResourcesComponent>();

+ 1 - 1
Unity/Hotfix/Module/FrameSync/Actor_TestHandler.cs

@@ -5,7 +5,7 @@ namespace ETHotfix
 	[MessageHandler]
 	public class Actor_TestHandler : AMHandler<Actor_Test>
 	{
-		protected override void Run(Session session, Actor_Test message)
+		protected override void Run(ETModel.Session session, Actor_Test message)
 		{
 			Log.Debug(message.Info);
 		}

+ 1 - 1
Unity/Hotfix/Module/FrameSync/Frame_ClickMapHandler.cs

@@ -6,7 +6,7 @@ namespace ETHotfix
 	[MessageHandler]
 	public class Frame_ClickMapHandler : AMHandler<Frame_ClickMap>
 	{
-		protected override void Run(Session session, Frame_ClickMap message)
+		protected override void Run(ETModel.Session session, Frame_ClickMap message)
 		{
 			Unit unit = ETModel.Game.Scene.GetComponent<UnitComponent>().Get(message.Id);
 			MoveComponent moveComponent = unit.GetComponent<MoveComponent>();

+ 1 - 1
Unity/Hotfix/Module/FrameSync/G2C_TestHotfixHandler.cs

@@ -5,7 +5,7 @@ namespace ETHotfix
 	[MessageHandler]
 	public class G2C_TestHotfixMessageHandler : AMHandler<G2C_TestHotfixMessage>
 	{
-		protected override void Run(Session session, G2C_TestHotfixMessage message)
+		protected override void Run(ETModel.Session session, G2C_TestHotfixMessage message)
 		{
 			Log.Debug(message.Info);
 		}

+ 2 - 2
Unity/Hotfix/Module/FrameSync/OperaComponent.cs

@@ -42,7 +42,7 @@ namespace ETHotfix
 	            if (Physics.Raycast(ray, out hit, 1000, this.mapMask))
 	            {
 					this.ClickPoint = hit.point;
-		            SessionComponent.Instance.Session.Send(new Frame_ClickMap() { X = (int)(this.ClickPoint.x * 1000), Z = (int)(this.ClickPoint.z * 1000) });
+		            ETModel.SessionComponent.Instance.Session.Send(new Frame_ClickMap() { X = (int)(this.ClickPoint.x * 1000), Z = (int)(this.ClickPoint.z * 1000) });
 
 					// 测试actor rpc消息
 					this.TestActor();
@@ -54,7 +54,7 @@ namespace ETHotfix
 	    {
 		    try
 		    {
-			    M2C_TestActorResponse response = (M2C_TestActorResponse)await SessionWrapComponent.Instance.Session.Call(
+			    M2C_TestActorResponse response = (M2C_TestActorResponse)await SessionComponent.Instance.Session.Call(
 						new C2M_TestActorRequest() { Info = "actor rpc request" });
 			    Log.Info(response.Info);
 			}

+ 2 - 2
Unity/Hotfix/Module/Message/AMHandler.cs

@@ -5,9 +5,9 @@ namespace ETHotfix
 {
 	public abstract class AMHandler<Message> : IMHandler where Message: class
 	{
-		protected abstract void Run(Session session, Message message);
+		protected abstract void Run(ETModel.Session session, Message message);
 
-		public void Handle(Session session, object msg)
+		public void Handle(ETModel.Session session, object msg)
 		{
 			Message message = msg as Message;
 			if (message == null)

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

@@ -6,7 +6,7 @@ namespace ETHotfix
 #if ILRuntime
 	public interface IMHandler
 	{
-		void Handle(Session session, object message);
+		void Handle(ETModel.Session session, object message);
 		Type GetMessageType();
 	}
 #else

+ 1 - 1
Unity/Hotfix/Module/Message/MessageDispatherComponent.cs

@@ -89,7 +89,7 @@ namespace ETHotfix
 			this.handlers[opcode].Add(handler);
 		}
 
-		public void Handle(Session session, MessageInfo messageInfo)
+		public void Handle(ETModel.Session session, MessageInfo messageInfo)
 		{
 			List<IMHandler> actions;
 			if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))

+ 27 - 17
Unity/Hotfix/Module/Message/SessionWrap.cs → Unity/Hotfix/Module/Message/Session.cs

@@ -6,22 +6,41 @@ using ETModel;
 
 namespace ETHotfix
 {
-	public class SessionWrap: Entity
+	[ObjectSystem]
+	public class SessionAwakeSystem : AwakeSystem<Session, ETModel.Session>
 	{
-		public readonly Session session;
+		public override void Awake(Session self, ETModel.Session session)
+		{
+			self.session = session;
+			SessionCallbackComponent sessionComponent = self.session.AddComponent<SessionCallbackComponent>();
+			sessionComponent.MessageCallback = (s, p) => { self.Run(s, p); };
+			sessionComponent.DisposeCallback = s => { self.Dispose(); };
+		}
+	}
+	
+	/// <summary>
+	/// 用来收发热更层的消息
+	/// </summary>
+	public class Session: Entity
+	{
+		public ETModel.Session session;
 
 		private static int RpcId { get; set; }
 		private readonly Dictionary<int, Action<IResponse>> requestCallback = new Dictionary<int, Action<IResponse>>();
 
-		public SessionWrap(Session session)
+		public override void Dispose()
 		{
-			this.session = session;
-			SessionCallbackComponent sessionComponent = this.session.AddComponent<SessionCallbackComponent>();
-			sessionComponent.MessageCallback = (s, p) => { this.Run(s, p); };
-			sessionComponent.DisposeCallback = s => { this.Dispose(); };
+			if (this.IsDisposed)
+			{
+				return;
+			}
+			
+			base.Dispose();
+			
+			this.session.Dispose();
 		}
 
-		public void Run(Session s, Packet p)
+		public void Run(ETModel.Session s, Packet p)
 		{
 			ushort opcode = p.Opcode();
 			byte flag = p.Flag();
@@ -126,14 +145,5 @@ namespace ETHotfix
 			this.Send(0x00, request);
 			return tcs.Task;
 		}
-
-		public override void Dispose()
-		{
-			if (this.IsDisposed)
-			{
-				return;
-			}
-			base.Dispose();
-		}
 	}
 }

+ 5 - 5
Unity/Hotfix/Module/Message/SessionWrapComponent.cs → Unity/Hotfix/Module/Message/SessionComponent.cs

@@ -3,19 +3,19 @@
 namespace ETHotfix
 {
 	[ObjectSystem]
-	public class SessionComponentAwakeSystem : AwakeSystem<SessionWrapComponent>
+	public class SessionComponentAwakeSystem : AwakeSystem<SessionComponent>
 	{
-		public override void Awake(SessionWrapComponent self)
+		public override void Awake(SessionComponent self)
 		{
 			self.Awake();
 		}
 	}
 
-	public class SessionWrapComponent: Component
+	public class SessionComponent: Component
 	{
-		public static SessionWrapComponent Instance;
+		public static SessionComponent Instance;
 
-		public SessionWrap Session;
+		public Session Session;
 
 		public void Awake()
 		{

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

@@ -41,7 +41,7 @@ namespace ETHotfix
 		private void OnSend()
 		{
 			// 发送一个actor消息
-			SessionComponent.Instance.Session.Send(new Actor_Test() { Info = "message client->gate->map->gate->client" });
+			ETModel.SessionComponent.Instance.Session.Send(new Actor_Test() { Info = "message client->gate->map->gate->client" });
 		}
 
 		private async void OnSendRpc()
@@ -49,7 +49,7 @@ namespace ETHotfix
 			try
 			{
 				// 向actor发起一次rpc调用
-				Actor_TestResponse response = (Actor_TestResponse) await SessionComponent.Instance.Session.Call(new Actor_TestRequest() { request = "request actor test rpc" });
+				Actor_TestResponse response = (Actor_TestResponse) await ETModel.SessionComponent.Instance.Session.Call(new Actor_TestRequest() { request = "request actor test rpc" });
 				Log.Info($"recv response: {JsonHelper.ToJson(response)}");
 			}
 			catch (Exception e)
@@ -62,7 +62,7 @@ namespace ETHotfix
 		{
 			try
 			{
-				Actor_TransferResponse response = (Actor_TransferResponse) await SessionComponent.Instance.Session.Call(new Actor_TransferRequest() {MapIndex = 0});
+				Actor_TransferResponse response = (Actor_TransferResponse) await ETModel.SessionComponent.Instance.Session.Call(new Actor_TransferRequest() {MapIndex = 0});
 				Log.Info($"传送成功! {JsonHelper.ToJson(response)}");
 			}
 			catch (Exception e)
@@ -73,7 +73,7 @@ namespace ETHotfix
 
 		private async void OnTransfer2()
 		{
-			Actor_TransferResponse response = (Actor_TransferResponse)await SessionComponent.Instance.Session.Call(new Actor_TransferRequest() { MapIndex = 1 });
+			Actor_TransferResponse response = (Actor_TransferResponse)await ETModel.SessionComponent.Instance.Session.Call(new Actor_TransferRequest() { MapIndex = 1 });
 			Log.Info($"传送成功! {JsonHelper.ToJson(response)}");
 		}
 
@@ -81,7 +81,7 @@ namespace ETHotfix
 		{
 			try
 			{
-				G2C_EnterMap g2CEnterMap = (G2C_EnterMap)await SessionComponent.Instance.Session.Call(new C2G_EnterMap());
+				G2C_EnterMap g2CEnterMap = (G2C_EnterMap)await ETModel.SessionComponent.Instance.Session.Call(new C2G_EnterMap());
 				Game.Scene.GetComponent<UIComponent>().Remove(UIType.UILobby);
 			}
 			catch (Exception e)

+ 14 - 10
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -30,23 +30,28 @@ namespace ETHotfix
 
 		public async void OnLogin()
 		{
-			SessionWrap sessionWrap = null;
 			try
 			{
 				IPEndPoint connetEndPoint = NetworkHelper.ToIPEndPoint(GlobalConfigComponent.Instance.GlobalProto.Address);
 
 				string text = this.account.GetComponent<InputField>().text;
 
-				Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
-				sessionWrap = new SessionWrap(session);
-				R2C_Login r2CLogin = (R2C_Login) await sessionWrap.Call(new C2R_Login() { Account = text, Password = "111111" });
-				sessionWrap.Dispose();
+				// 创建一个ETModel层的Session
+				ETModel.Session session = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
+				// 创建一个ETHotfix层的Session, ETHotfix的Session会通过ETModel层的Session发送消息
+				Session realmSession = ComponentFactory.Create<Session, ETModel.Session>(session);
+				R2C_Login r2CLogin = (R2C_Login) await realmSession.Call(new C2R_Login() { Account = text, Password = "111111" });
+				realmSession.Dispose();
 
 				connetEndPoint = NetworkHelper.ToIPEndPoint(r2CLogin.Address);
-				Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
-				Game.Scene.AddComponent<SessionWrapComponent>().Session = new SessionWrap(gateSession);
-				ETModel.Game.Scene.AddComponent<SessionComponent>().Session = gateSession;
-				G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionWrapComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key });
+				// 创建一个ETModel层的Session,并且保存到ETModel.SessionComponent中
+				ETModel.Session gateSession = ETModel.Game.Scene.GetComponent<NetOuterComponent>().Create(connetEndPoint);
+				ETModel.Game.Scene.AddComponent<ETModel.SessionComponent>().Session = gateSession;
+				
+				// 创建一个ETHotfix层的Session, 并且保存到ETHotfix.SessionComponent中
+				Game.Scene.AddComponent<SessionComponent>().Session = ComponentFactory.Create<Session, ETModel.Session>(gateSession);
+				
+				G2C_LoginGate g2CLoginGate = (G2C_LoginGate)await SessionComponent.Instance.Session.Call(new C2G_LoginGate() { Key = r2CLogin.Key });
 
 				Log.Info("登陆gate成功!");
 
@@ -60,7 +65,6 @@ namespace ETHotfix
 			}
 			catch (Exception e)
 			{
-				sessionWrap?.Dispose();
 				Log.Error(e);
 			}
 		}

+ 2 - 2
Unity/Hotfix/Unity.Hotfix.csproj

@@ -85,8 +85,8 @@
     <Compile Include="Base\Object\Object.cs" />
     <Compile Include="Base\Object\EventSystem.cs" />
     <Compile Include="Base\Object\ObjectPool.cs" />
-    <Compile Include="Module\Message\SessionWrapComponent.cs" />
-    <Compile Include="Module\Message\SessionWrap.cs" />
+    <Compile Include="Module\Message\SessionComponent.cs" />
+    <Compile Include="Module\Message\Session.cs" />
     <Compile Include="Module\UI\IUIFactory.cs" />
     <Compile Include="Module\Message\MessageDispatherComponent.cs" />
     <Compile Include="Module\Message\OpcodeTypeComponent.cs" />