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

NetInnerComponent以地址为Key缓存Session, NetOuterComponent不缓存Session

tanghai 9 лет назад
Родитель
Сommit
730f54ddf6

+ 1 - 0
Unity/Assets/Editor/ClientConfigEditor/ClientConfigEditor.cs

@@ -55,6 +55,7 @@ namespace MyEditor
 
 		private void OnDestroy()
 		{
+			this.config.Dispose();
 		}
 	}
 }

+ 11 - 8
Unity/Assets/Editor/ServerManagerEditor/ServerManagerEditor.cs

@@ -65,16 +65,19 @@ namespace MyEditor
 						reloadType = reloadType | this.serverTypes[i];
 					}
 				}
-				NetworkComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
-				Session session = networkComponent.Get($"{this.managerAddress}");
-				try
+				NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
+				using (Session session = networkComponent.Create($"{this.managerAddress}"))
 				{
-					session.Call<C2M_Reload, M2C_Reload>(new C2M_Reload { AppType = reloadType });
-				}
-				catch (RpcException e)
-				{
-					Log.Error(e.ToString());
+					try
+					{
+						session.Call<C2M_Reload, M2C_Reload>(new C2M_Reload { AppType = reloadType });
+					}
+					catch (RpcException e)
+					{
+						Log.Error(e.ToString());
+					}
 				}
+
 				Log.Info("Reload OK!");
 			}
 		}

+ 7 - 5
Unity/Assets/Scripts/Component/BenchmakComponent.cs

@@ -19,7 +19,7 @@ namespace Model
 
 		public void Awake(string address)
 		{
-			NetworkComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
+			NetOuterComponent networkComponent = Game.Scene.GetComponent<NetOuterComponent>();
 
 			for (int i = 0; i < 400; i++)
 			{
@@ -27,7 +27,7 @@ namespace Model
 			}
 		}
 
-		private async void TestAsync(NetworkComponent networkComponent, string address, int j)
+		private async void TestAsync(NetOuterComponent networkComponent, string address, int j)
 		{
 			using (Session session = networkComponent.Create(address))
 			{
@@ -38,9 +38,11 @@ namespace Model
 					try
 					{
 						R2C_Login s2CLogin = await session.Call<C2R_Login, R2C_Login>(new C2R_Login { Account = "abcdef", Password = "111111" });
-						
-						Session gateSession = networkComponent.Get(s2CLogin.Address);
-						await gateSession.Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
+
+						using (Session gateSession = networkComponent.Create(s2CLogin.Address))
+						{
+							await gateSession.Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
+						}
 
 						++this.k;
 						if (this.k % 1000 == 0)

+ 51 - 1
Unity/Assets/Scripts/Component/NetInnerComponent.cs

@@ -1,4 +1,6 @@
-using Base;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Base;
 
 namespace Model
 {
@@ -24,6 +26,8 @@ namespace Model
 
 	public class NetInnerComponent : NetworkComponent
 	{
+		private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
+
 		public void Awake()
 		{
 			this.Awake(NetworkProtocol.TCP);
@@ -33,5 +37,51 @@ namespace Model
 		{
 			this.Awake(NetworkProtocol.TCP, host, port);
 		}
+
+		protected override async Task<Session> Accept()
+		{
+			Session session = await base.Accept();
+			this.AddToAddressDict(session);
+			return session;
+		}
+
+		private void AddToAddressDict(Session session)
+		{
+			Session s;
+			if (this.adressSessions.TryGetValue(session.RemoteAddress, out s))
+			{
+				this.Remove(s.Id);
+				Log.Warning($"session 地址冲突, 可能是客户端断开, 服务器还没检测到!: {session.RemoteAddress}");
+			}
+			this.adressSessions.Add(session.RemoteAddress, session);
+		}
+
+		public override void Remove(long id)
+		{
+			Session session = this.Get(id);
+			if (session == null)
+			{
+				return;
+			}
+			this.adressSessions.Remove(session.RemoteAddress);
+
+			base.Remove(id);
+		}
+
+		/// <summary>
+		/// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
+		/// </summary>
+		public Session Get(string address)
+		{
+			Session session;
+			if (this.adressSessions.TryGetValue(address, out session))
+			{
+				return session;
+			}
+
+			session = this.Create(address);
+			this.AddToAddressDict(session);
+			return session;
+		}
 	}
 }

+ 11 - 51
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Threading.Tasks;
 using Base;
 
 namespace Model
@@ -10,21 +11,6 @@ namespace Model
 		private AService Service;
 
 		private readonly Dictionary<long, Session> sessions = new Dictionary<long, Session>();
-		private readonly Dictionary<string, Session> adressSessions = new Dictionary<string, Session>();
-
-		private event Action<Session> removeCallback = session => { };
-
-		public event Action<Session> RemoveCallback
-		{
-			add
-			{
-				this.removeCallback += value;
-			}
-			remove
-			{
-				this.removeCallback -= value;
-			}
-		}
 
 		protected void Awake(NetworkProtocol protocol)
 		{
@@ -67,38 +53,28 @@ namespace Model
 					return;
 				}
 
-				AChannel channel = await this.Service.AcceptChannel();
-
-				Session session = new Session(this, channel);
-				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
-				this.sessions.Add(session.Id, session);
+				await this.Accept();
 			}
 		}
-		
-		private void AddToAddressDict(Session session)
+
+		protected virtual async Task<Session> Accept()
 		{
-			Session s;
-			if (this.adressSessions.TryGetValue(session.RemoteAddress, out s))
-			{
-				this.Remove(s.Id);
-				Log.Warning($"session 地址冲突, 可能是客户端断开, 服务器还没检测到!: {session.RemoteAddress}");
-			}
-			this.adressSessions.Add(session.RemoteAddress, session);
+
+			AChannel channel = await this.Service.AcceptChannel();
+			Session session = new Session(this, channel);
+			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
+			this.sessions.Add(session.Id, session);
+			return session;
 		}
 
-		public void Remove(long id)
+		public virtual void Remove(long id)
 		{
 			Session session;
 			if (!this.sessions.TryGetValue(id, out session))
 			{
 				return;
 			}
-			removeCallback.Invoke(session);
 			this.sessions.Remove(id);
-			if (session.ChannelType == ChannelType.Connect)
-			{
-				this.adressSessions.Remove(session.RemoteAddress);
-			}
 			session.Dispose();
 		}
 
@@ -109,22 +85,6 @@ namespace Model
 			return session;
 		}
 
-		/// <summary>
-		/// 从地址缓存中取Session,如果没有则创建一个新的Session,并且保存到地址缓存中
-		/// </summary>
-		public Session Get(string address)
-		{
-			Session session;
-			if (this.adressSessions.TryGetValue(address, out session))
-			{
-				return session;
-			}
-
-			session = this.Create(address);
-			this.AddToAddressDict(session);
-			return session;
-		}
-
 		/// <summary>
 		/// 创建一个新Session
 		/// </summary>

+ 22 - 19
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs

@@ -14,27 +14,30 @@ namespace Controller
 		{
 			Game.Scene.AddComponent<MessageDispatherComponent, AppType>(AppType.Client);
 			ClientConfig clientConfig = Game.Scene.AddComponent<ClientConfigComponent>().Config;
-			NetworkComponent networkComponent = Game.Scene.AddComponent<NetOuterComponent>();
-			Session session = networkComponent.Get(clientConfig.Address);
-
-			try
+			NetOuterComponent networkComponent = Game.Scene.AddComponent<NetOuterComponent>();
+			using (Session session = networkComponent.Create(clientConfig.Address))
 			{
-				R2C_Login s2CLogin = await session.Call<C2R_Login, R2C_Login>(new C2R_Login {Account = "abcdef", Password = "111111"});
-				networkComponent.Remove(session.Id);
+				try
+				{
+					R2C_Login s2CLogin = await session.Call<C2R_Login, R2C_Login>(new C2R_Login { Account = "abcdef", Password = "111111" });
+					networkComponent.Remove(session.Id);
 
-				// 连接Gate
-				Log.Debug(MongoHelper.ToJson(s2CLogin));
-				Session gateSession = networkComponent.Get(s2CLogin.Address);
-				await gateSession.Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
-				Log.Info("连接Gate验证成功!");
-			}
-			catch (RpcException e)
-			{
-				Log.Error(e.ToString());
-			}
-			catch (Exception e)
-			{
-				Log.Error(e.ToString());
+					// 连接Gate
+					using (Session gateSession = networkComponent.Create(s2CLogin.Address))
+					{
+						await gateSession.Call<C2G_LoginGate, G2C_LoginGate>(new C2G_LoginGate(s2CLogin.Key));
+					}
+					
+					Log.Info("连接Gate验证成功!");
+				}
+				catch (RpcException e)
+				{
+					Log.Error(e.ToString());
+				}
+				catch (Exception e)
+				{
+					Log.Error(e.ToString());
+				}
 			}
 		}
 	}