Procházet zdrojové kódy

加上了DBTask对象池

tanghai před 8 roky
rodič
revize
af9e11aa4d

+ 7 - 7
Server/Model/Component/DBCacheComponent.cs

@@ -26,9 +26,8 @@ namespace Model
 		{
 			for (int i = 0; i < taskCount; ++i)
 			{
-				DBTaskQueue taskQueue = new DBTaskQueue();
+				DBTaskQueue taskQueue = EntityFactory.Create<DBTaskQueue>();
 				this.tasks.Add(taskQueue);
-				taskQueue.Start();
 			}
 		}
 
@@ -42,7 +41,7 @@ namespace Model
 			{
 				collectionName = entity.GetType().Name;
 			}
-			DBSaveTask task = new DBSaveTask(entity, collectionName, tcs);
+			DBSaveTask task = EntityFactory.CreateWithId<DBSaveTask, Entity, string, TaskCompletionSource<bool>>(entity.Id, entity, collectionName, tcs);
 			this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
 
 			return tcs.Task;
@@ -51,7 +50,7 @@ namespace Model
 		public Task<bool> AddBatch(List<Entity> entitys, string collectionName)
 		{
 			TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
-			DBSaveBatchTask task = new DBSaveBatchTask(entitys, collectionName, tcs);
+			DBSaveBatchTask task = EntityFactory.Create<DBSaveBatchTask, List<Entity>, string, TaskCompletionSource<bool>>(entitys, collectionName, tcs);
 			this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
 			return tcs.Task;
 		}
@@ -105,7 +104,8 @@ namespace Model
 			}
 
 			TaskCompletionSource<Entity> tcs = new TaskCompletionSource<Entity>();
-			this.tasks[(int)((ulong)id % taskCount)].Add(new DBQueryTask(id, collectionName, tcs));
+			DBQueryTask dbQueryTask = EntityFactory.CreateWithId<DBQueryTask, string, TaskCompletionSource<Entity>>(id, collectionName, tcs);
+			this.tasks[(int)((ulong)id % taskCount)].Add(dbQueryTask);
 
 			return tcs.Task;
 		}
@@ -131,7 +131,7 @@ namespace Model
 			}
 
 			TaskCompletionSource<List<Entity>> tcs = new TaskCompletionSource<List<Entity>>();
-			DBQueryBatchTask dbQueryBatchTask = new DBQueryBatchTask(idList, collectionName, tcs);
+			DBQueryBatchTask dbQueryBatchTask = EntityFactory.Create<DBQueryBatchTask, List<long>, string, TaskCompletionSource<List<Entity>>>(idList, collectionName, tcs);
 			this.tasks[(int)((ulong)dbQueryBatchTask.Id % taskCount)].Add(dbQueryBatchTask);
 
 			return tcs.Task;
@@ -141,7 +141,7 @@ namespace Model
 		{
 			TaskCompletionSource<List<Entity>> tcs = new TaskCompletionSource<List<Entity>>();
 			
-			DBQueryJsonTask dbQueryJsonTask = new DBQueryJsonTask(collectionName, json, tcs);
+			DBQueryJsonTask dbQueryJsonTask = EntityFactory.Create<DBQueryJsonTask, string, string, TaskCompletionSource< List < Entity >>>(collectionName, json, tcs);
 			this.tasks[(int)((ulong)dbQueryJsonTask.Id % taskCount)].Add(dbQueryJsonTask);
 
 			return tcs.Task;

+ 63 - 0
Server/Model/Entity/DBQueryBatchTask.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using MongoDB.Bson;
+using MongoDB.Driver;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class DBQueryBatchTaskEvent : ObjectEvent<DBQueryBatchTask>, IAwake<List<long>, string, TaskCompletionSource<List<Entity>>>
+	{
+		public void Awake(List<long> idList, string collectionName, TaskCompletionSource<List<Entity>> tcs)
+		{
+			DBQueryBatchTask self = this.Get();
+
+			self.IdList = idList;
+			self.CollectionName = collectionName;
+			self.Tcs = tcs;
+		}
+	}
+
+	public sealed class DBQueryBatchTask : DBTask
+	{
+		public string CollectionName { get; set; }
+
+		public List<long> IdList { get; set; }
+
+		public TaskCompletionSource<List<Entity>> Tcs { get; set; }
+		
+		public override async Task Run()
+		{
+			DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+			List<Entity> result = new List<Entity>();
+
+			try
+			{
+				// 执行查询数据库任务
+				foreach (long id in IdList)
+				{
+					Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, id);
+					if (entity == null)
+					{
+						entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync();
+						dbCacheComponent.AddToCache(entity);
+					}
+					
+					if (entity == null)
+					{
+						continue;
+					}
+					result.Add(entity);
+				}
+				
+				this.Tcs.SetResult(result);
+			}
+			catch (Exception e)
+			{
+				this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e));
+			}
+		}
+	}
+}

+ 45 - 0
Server/Model/Entity/DBQueryJsonTask.cs

@@ -0,0 +1,45 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using MongoDB.Driver;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class DBQueryJsonTaskEvent : ObjectEvent<DBQueryJsonTask>, IAwake<string, string, TaskCompletionSource<List<Entity>>>
+	{
+		public void Awake(string collectionName, string json, TaskCompletionSource<List<Entity>> tcs)
+		{
+			DBQueryJsonTask self = this.Get();
+			
+			self.CollectionName = collectionName;
+			self.Json = json;
+			self.Tcs = tcs;
+		}
+	}
+
+	public sealed class DBQueryJsonTask : DBTask
+	{
+		public string CollectionName { get; set; }
+
+		public string Json { get; set; }
+
+		public TaskCompletionSource<List<Entity>> Tcs { get; set; }
+		
+		public override async Task Run()
+		{
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+			try
+			{
+				// 执行查询数据库任务
+				FilterDefinition<Entity> filterDefinition = new JsonFilterDefinition<Entity>(this.Json);
+				List<Entity> entitys = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync();
+				this.Tcs.SetResult(entitys);
+			}
+			catch (Exception e)
+			{
+				this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
+			}
+		}
+	}
+}

+ 55 - 0
Server/Model/Entity/DBQueryTask.cs

@@ -0,0 +1,55 @@
+using System;
+using System.Threading.Tasks;
+using MongoDB.Driver;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class DBQueryTaskEvent : ObjectEvent<DBQueryTask>, IAwake<string, TaskCompletionSource<Entity>>
+	{
+		public void Awake(string collectionName, TaskCompletionSource<Entity> tcs)
+		{
+			DBQueryTask self = this.Get();
+			self.CollectionName = collectionName;
+			self.Tcs = tcs;
+		}
+	}
+
+	public sealed class DBQueryTask : DBTask
+	{
+		public string CollectionName { get; set; }
+
+		public TaskCompletionSource<Entity> Tcs { get; set; }
+
+		public DBQueryTask(long id): base(id)
+		{
+		}
+
+		public override async Task Run()
+		{
+			DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+			// 执行查询前先看看cache中是否已经存在
+			Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, this.Id);
+			if (entity != null)
+			{
+				this.Tcs.SetResult(entity);
+				return;
+			}
+			try
+			{
+				// 执行查询数据库任务
+				entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync();
+				if (entity != null)
+				{
+					dbCacheComponent.AddToCache(entity);
+				}
+				this.Tcs.SetResult(entity);
+			}
+			catch (Exception e)
+			{
+				this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
+			}
+		}
+	}
+}

+ 55 - 0
Server/Model/Entity/DBSaveBatchTask.cs

@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using MongoDB.Bson;
+using MongoDB.Driver;
+
+namespace Model
+{
+	[ObjectEvent]
+	public class DBSaveBatchTaskEvent : ObjectEvent<DBSaveBatchTask>, IAwake<List<Entity>, string, TaskCompletionSource<bool>>
+	{
+		public void Awake(List<Entity> entitys, string collectionName, TaskCompletionSource<bool> tcs)
+		{
+			DBSaveBatchTask self = this.Get();
+			
+			self.Entitys = entitys;
+			self.CollectionName = collectionName;
+			self.Tcs = tcs;
+		}
+	}
+
+	public sealed class DBSaveBatchTask : DBTask
+	{
+		public string CollectionName { get; set; }
+
+		public List<Entity> Entitys;
+
+		public TaskCompletionSource<bool> Tcs;
+	
+		public override async Task Run()
+		{
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+
+			foreach (Entity entity in this.Entitys)
+			{
+				if (entity == null)
+				{
+					continue;
+				}
+
+				try
+				{
+					// 执行保存数据库任务
+					await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == entity.Id, entity, new UpdateOptions { IsUpsert = true });
+				}
+				catch (Exception e)
+				{
+					Log.Debug($"{entity.GetType().Name} {entity.ToJson()}" + e.ToString());
+					this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Entitys.ListToString()}", e));
+				}
+			}
+			this.Tcs.SetResult(true);
+		}
+	}
+}

+ 49 - 0
Server/Model/Entity/DBSaveTask.cs

@@ -0,0 +1,49 @@
+using System;
+using System.Threading.Tasks;
+using MongoDB.Driver;
+
+namespace Model
+{
+
+	[ObjectEvent]
+	public class DBSaveTaskEvent : ObjectEvent<DBSaveTask>, IAwake<Entity, string, TaskCompletionSource<bool>>
+	{
+		public void Awake(Entity entity, string collectionName, TaskCompletionSource<bool> tcs)
+		{
+			DBSaveTask self = this.Get();
+
+			self.Entity = entity;
+			self.CollectionName = collectionName;
+			self.Tcs = tcs;
+		}
+	}
+
+	public sealed class DBSaveTask : DBTask
+	{
+		public Entity Entity;
+
+		public string CollectionName { get; set; }
+
+		public TaskCompletionSource<bool> Tcs;
+
+		public DBSaveTask(long id): base(id)
+		{
+		}
+
+		public override async Task Run()
+		{
+			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
+
+			try
+			{
+				// 执行保存数据库任务
+				await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Entity.Id, this.Entity, new UpdateOptions {IsUpsert = true});
+				this.Tcs.SetResult(true);
+			}
+			catch (Exception e)
+			{
+				this.Tcs.SetException(new Exception($"保存数据失败!  {CollectionName} {Id}", e));
+			}
+		}
+	}
+}

+ 3 - 201
Server/Model/Entity/DBTask.cs

@@ -1,12 +1,8 @@
-using System;
-using System.Collections.Generic;
-using System.Threading.Tasks;
-using MongoDB.Bson;
-using MongoDB.Driver;
+using System.Threading.Tasks;
 
 namespace Model
 {
-	public abstract class DBTask : Entity
+	public abstract class DBTask : Disposer
 	{
 		protected DBTask()
 		{
@@ -15,201 +11,7 @@ namespace Model
 		protected DBTask(long id): base(id)
 		{
 		}
-
+		
 		public abstract Task Run();
 	}
-
-	public sealed class DBSaveTask : DBTask
-	{
-		public Entity Entity;
-
-		public string CollectionName { get; }
-
-		public TaskCompletionSource<bool> Tcs;
-
-		public DBSaveTask(Entity entity, string collectionName, TaskCompletionSource<bool> tcs) : base(entity.Id)
-		{
-			this.Entity = entity;
-			this.CollectionName = collectionName;
-			this.Tcs = tcs;
-		}
-
-		public override async Task Run()
-		{
-			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-
-			try
-			{
-				// 执行保存数据库任务
-				await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Entity.Id, this.Entity, new UpdateOptions {IsUpsert = true});
-				this.Tcs.SetResult(true);
-			}
-			catch (Exception e)
-			{
-				this.Tcs.SetException(new Exception($"保存数据失败!  {CollectionName} {Id}", e));
-			}
-		}
-	}
-
-	public sealed class DBSaveBatchTask : DBTask
-	{
-		public string CollectionName { get; }
-
-		public List<Entity> Entitys;
-
-		public TaskCompletionSource<bool> Tcs;
-	
-		public DBSaveBatchTask(List<Entity> entitys, string collectionName, TaskCompletionSource<bool> tcs)
-		{
-			this.Entitys = entitys;
-			this.CollectionName = collectionName;
-			this.Tcs = tcs;
-		}
-	
-		public override async Task Run()
-		{
-			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-
-			foreach (Entity entity in this.Entitys)
-			{
-				if (entity == null)
-				{
-					continue;
-				}
-
-				try
-				{
-					// 执行保存数据库任务
-					await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == entity.Id, entity, new UpdateOptions { IsUpsert = true });
-				}
-				catch (Exception e)
-				{
-					Log.Debug($"{entity.GetType().Name} {entity.ToJson()}" + e.ToString());
-					this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Entitys.ListToString()}", e));
-				}
-			}
-			this.Tcs.SetResult(true);
-		}
-	}
-
-	public sealed class DBQueryTask : DBTask
-	{
-		public string CollectionName { get; }
-
-		public TaskCompletionSource<Entity> Tcs { get; }
-
-		public DBQueryTask(long id, string collectionName, TaskCompletionSource<Entity> tcs) : base(id)
-		{
-			this.CollectionName = collectionName;
-			this.Tcs = tcs;
-		}
-
-		public override async Task Run()
-		{
-			DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
-			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-			// 执行查询前先看看cache中是否已经存在
-			Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, this.Id);
-			if (entity != null)
-			{
-				this.Tcs.SetResult(entity);
-				return;
-			}
-			try
-			{
-				// 执行查询数据库任务
-				entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync();
-				if (entity != null)
-				{
-					dbCacheComponent.AddToCache(entity);
-				}
-				this.Tcs.SetResult(entity);
-			}
-			catch (Exception e)
-			{
-				this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {Id}", e));
-			}
-		}
-	}
-
-	public sealed class DBQueryBatchTask : DBTask
-	{
-		public string CollectionName { get; }
-
-		public List<long> IdList { get; }
-
-		public TaskCompletionSource<List<Entity>> Tcs { get; }
-
-		public DBQueryBatchTask(List<long> list, string collectionName, TaskCompletionSource<List<Entity>> tcs)
-		{
-			this.IdList = list;
-			this.CollectionName = collectionName;
-			this.Tcs = tcs;
-		}
-
-		public override async Task Run()
-		{
-			DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
-			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-			List<Entity> result = new List<Entity>();
-
-			try
-			{
-				// 执行查询数据库任务
-				foreach (long id in IdList)
-				{
-					Entity entity = dbCacheComponent.GetFromCache(this.CollectionName, id);
-					if (entity == null)
-					{
-						entity = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync();
-						dbCacheComponent.AddToCache(entity);
-					}
-					
-					if (entity == null)
-					{
-						continue;
-					}
-					result.Add(entity);
-				}
-				
-				this.Tcs.SetResult(result);
-			}
-			catch (Exception e)
-			{
-				this.Tcs.SetException(new Exception($"查询数据库异常! {this.CollectionName} {IdList.ListToString()}", e));
-			}
-		}
-	}
-
-	public sealed class DBQueryJsonTask : DBTask
-	{
-		public string CollectionName { get; }
-
-		public string Json { get; }
-
-		public TaskCompletionSource<List<Entity>> Tcs { get; }
-
-		public DBQueryJsonTask(string collectionName, string json, TaskCompletionSource<List<Entity>> tcs)
-		{
-			this.CollectionName = collectionName;
-			this.Json = json;
-			this.Tcs = tcs;
-		}
-
-		public override async Task Run()
-		{
-			DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
-			try
-			{
-				// 执行查询数据库任务
-				FilterDefinition<Entity> filterDefinition = new JsonFilterDefinition<Entity>(this.Json);
-				List<Entity> entitys = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync();
-				this.Tcs.SetResult(entitys);
-			}
-			catch (Exception e)
-			{
-				this.Tcs.SetException(new Exception($"查询数据库异常! {CollectionName} {this.Json}", e));
-			}
-		}
-	}
 }

+ 26 - 13
Server/Model/Entity/DBTaskQueue.cs

@@ -3,26 +3,33 @@ using System.Threading.Tasks;
 
 namespace Model
 {
-	public sealed class DBTaskQueue : Entity
+	[ObjectEvent]
+	public class DBTaskQueueEvent : ObjectEvent<DBTaskQueue>, IAwake, IStart
 	{
-		public EQueue<DBTask> queue = new EQueue<DBTask>();
-
-		private TaskCompletionSource<DBTask> tcs;
+		public void Awake()
+		{
+			DBTaskQueue self = this.Get();
+			self.queue.Clear();
+		}
 
 		public async void Start()
 		{
+			DBTaskQueue self = this.Get();
+
 			while (true)
 			{
-				if (this.Id == 0)
+				if (self.Id == 0)
 				{
 					return;
 				}
-				
-				DBTask task = await this.Get();
+
+				DBTask task = await self.Get();
 
 				try
 				{
 					await task.Run();
+
+					task.Dispose();
 				}
 				catch (Exception e)
 				{
@@ -30,6 +37,14 @@ namespace Model
 				}
 			}
 		}
+	}
+
+	public sealed class DBTaskQueue : Disposer
+	{
+		public EQueue<DBTask> queue = new EQueue<DBTask>();
+
+		public TaskCompletionSource<DBTask> tcs;
+
 		public void Add(DBTask task)
 		{
 			if (this.tcs != null)
@@ -45,16 +60,14 @@ namespace Model
 
 		public Task<DBTask> Get()
 		{
-			TaskCompletionSource<DBTask> t = new TaskCompletionSource<DBTask>();
 			if (this.queue.Count > 0)
 			{
 				DBTask task = this.queue.Dequeue();
-				t.SetResult(task);
-			}
-			else
-			{
-				this.tcs = t;
+				return Task.FromResult(task);
 			}
+
+			TaskCompletionSource<DBTask> t = new TaskCompletionSource<DBTask>();
+			this.tcs = t;
 			return t.Task;
 		}
 	}

+ 1 - 1
Server/Model/Entity/Player.cs

@@ -1,7 +1,7 @@
 namespace Model
 {
 	[ObjectEvent]
-	public class GamerEvent : ObjectEvent<Player>, IAwake<string>
+	public class PlayerEvent : ObjectEvent<Player>, IAwake<string>
 	{
 		public void Awake(string account)
 		{

+ 0 - 3
Unity/Assets/ThirdParty/ILRuntime/Mono.Cecil.Mdb/mdb/Mono.Cecil.Mdb.meta

@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: 8c01944eba5c4312bc36ab97974848a8
-timeCreated: 1509538086

+ 0 - 3
Unity/Assets/ThirdParty/ILRuntime/Mono.Cecil.Mdb/mdb/Mono.CompilerServices.SymbolWriter.meta

@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: 7dc3339561a540edaaa26bfa8063908e
-timeCreated: 1509538086