Explorar el Código

entity增加Type字段,以便统计对象数量,防止内存泄漏

tanghai hace 9 años
padre
commit
20d2b6a408

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

@@ -77,6 +77,9 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Other\CommandLine.cs">
     <Compile Include="..\..\Unity\Assets\Scripts\Other\CommandLine.cs">
       <Link>Other\CommandLine.cs</Link>
       <Link>Other\CommandLine.cs</Link>
     </Compile>
     </Compile>
+    <Compile Include="..\..\Unity\Assets\Scripts\Other\EntityType.cs">
+      <Link>Other\EntityType.cs</Link>
+    </Compile>
     <Compile Include="..\..\Unity\Assets\Scripts\Other\Options.cs">
     <Compile Include="..\..\Unity\Assets\Scripts\Other\Options.cs">
       <Link>Other\Options.cs</Link>
       <Link>Other\Options.cs</Link>
     </Compile>
     </Compile>

+ 1 - 1
Unity/Assets/Plugins/Base/Game.cs

@@ -10,7 +10,7 @@
 			{
 			{
 				if (game == null)
 				if (game == null)
 				{
 				{
-					game = new Entity();
+					game = new Entity("Scene");
 				}
 				}
 				return game;
 				return game;
 			}
 			}

+ 6 - 2
Unity/Assets/Plugins/Base/Object/Entity.cs

@@ -8,17 +8,21 @@ namespace Base
 {
 {
 	public sealed class Entity: Object
 	public sealed class Entity: Object
 	{
 	{
+		public string Type { get; set; }
+
 		[BsonElement, BsonIgnoreIfNull]
 		[BsonElement, BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
 		private HashSet<Component> components = new HashSet<Component>();
 		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 
 
-		public Entity()
+		public Entity(string entityType)
 		{
 		{
+			this.Type = entityType;
 			ObjectManager.Add(this);
 			ObjectManager.Add(this);
 		}
 		}
 
 
-		public Entity(long id): base(id)
+		public Entity(long id, string entityType) : base(id)
 		{
 		{
+			this.Type = entityType;
 			ObjectManager.Add(this);
 			ObjectManager.Add(this);
 		}
 		}
 		
 		

+ 3 - 3
Unity/Assets/Scripts/Component/NetworkComponent.cs

@@ -74,7 +74,7 @@ namespace Model
 
 
 				AChannel channel = await this.Service.AcceptChannel();
 				AChannel channel = await this.Service.AcceptChannel();
 
 
-				Entity session = new Entity();
+				Entity session = new Entity(EntityType.Session);
 				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 				channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 				session.AddComponent<MessageComponent, AChannel>(channel);
 				session.AddComponent<MessageComponent, AChannel>(channel);
 				this.Add(session);
 				this.Add(session);
@@ -87,7 +87,7 @@ namespace Model
 			this.adressSessions.Add(session.GetComponent<MessageComponent>().RemoteAddress, session);
 			this.adressSessions.Add(session.GetComponent<MessageComponent>().RemoteAddress, session);
 		}
 		}
 
 
-		private void Remove(long id)
+		public void Remove(long id)
 		{
 		{
 			Entity session;
 			Entity session;
 			if (!this.sessions.TryGetValue(id, out session))
 			if (!this.sessions.TryGetValue(id, out session))
@@ -117,7 +117,7 @@ namespace Model
 			int port = int.Parse(ss[1]);
 			int port = int.Parse(ss[1]);
 			string host = ss[0];
 			string host = ss[0];
 			AChannel channel = this.Service.ConnectChannel(host, port);
 			AChannel channel = this.Service.ConnectChannel(host, port);
-			session = new Entity();
+			session = new Entity(EntityType.Session);
 			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 			channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
 			session.AddComponent<MessageComponent, AChannel>(channel);
 			session.AddComponent<MessageComponent, AChannel>(channel);
 			this.Add(session);
 			this.Add(session);

+ 3 - 18
Unity/Assets/Scripts/Other/EntityType.cs

@@ -1,23 +1,8 @@
 namespace Model
 namespace Model
 {
 {
-	public enum EntityType
+	public static class EntityType
 	{
 	{
-		None = 0,
-		Hero = 1,
-		Building = 2,
-		Soldier = 3,
-		Summon = 4,
-		Wild_lv1 = 5,
-		Item = 6,
-		Bullet = 7,
-		Wild_lv2 = 8,
-		Wild_lv3 = 9,
-		Watcher = 10,
-
-		Actor = 100,
-		Buff = 102,
-		Illusion = 103,
-		Effect = 104,
-		Skill = 200,
+		public const string None = "None";
+		public const string Session = "Session";
 	}
 	}
 }
 }

+ 2 - 1
Unity/Controller/Event/InitSceneStartEvent_InitGame.cs

@@ -20,7 +20,8 @@ namespace Controller
 			{
 			{
 				// 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端
 				// 订阅服务端日志, 服务端收到这个消息会将之后的日志转发给客户端
 				await session.GetComponent<MessageComponent>().Call<C2R_SubscribeLog, R2C_SubscribeLog>(new C2R_SubscribeLog());
 				await session.GetComponent<MessageComponent>().Call<C2R_SubscribeLog, R2C_SubscribeLog>(new C2R_SubscribeLog());
-				R2C_Login s2CLogin = await session.GetComponent<MessageComponent>().Call<C2R_Login, R2C_Login>(new C2R_Login {Account = "111", Password = "111111"});
+				R2C_Login s2CLogin = await session.GetComponent<MessageComponent>().Call<C2R_Login, R2C_Login>(new C2R_Login {Account = "abcdef", Password = "111111"});
+				networkComponent.Remove(session.Id);
 
 
 				// 连接Gate
 				// 连接Gate
 				Entity gateSession = networkComponent.Get(s2CLogin.Address);
 				Entity gateSession = networkComponent.Get(s2CLogin.Address);