Przeglądaj źródła

1.Object增加ToString方法,使用MongoHelper.ToJson实现,方便打印对象
2.删除订阅Server Log功能

tanghai 9 lat temu
rodzic
commit
e041e2b7d3

+ 1 - 18
Server/Base/Log.cs

@@ -1,44 +1,27 @@
-using System;
-using System.Collections.Generic;
-
-namespace Base
+namespace Base
 {
 	public static class Log
 	{
 		private static readonly ILog globalLog = new NLogAdapter();
 
-		public static Dictionary<long, Action<LogType, string>> Callback { get; } = new Dictionary<long, Action<LogType, string>>();
-
-		private static void OnCallback(LogType type, string message)
-		{
-			foreach (var action in Callback.Values)
-			{
-				action(type, message);
-			}
-		}
-
 		public static void Warning(string message)
 		{
 			globalLog.Warning(message);
-			OnCallback(LogType.Warning, message);
 		}
 
 		public static void Info(string message)
 		{
 			globalLog.Info(message);
-			OnCallback(LogType.Info, message);
 		}
 
 		public static void Debug(string message)
 		{
 			globalLog.Debug(message);
-			OnCallback(LogType.Debug, message);
 		}
 
 		public static void Error(string message)
 		{
 			globalLog.Error(message);
-			OnCallback(LogType.Error, message);
 		}
 	}
 }

+ 0 - 19
Server/Controller/Message/C2R_SubscribeLogHandler.cs

@@ -1,19 +0,0 @@
-using System;
-using Base;
-using Model;
-
-namespace Controller
-{
-	[MessageHandler(AppType.Realm)]
-	public class C2R_SubscribeLogHandler : AMRpcHandler<C2R_SubscribeLog, R2C_SubscribeLog>
-	{
-		protected override void Run(Session session, C2R_SubscribeLog message, Action<R2C_SubscribeLog> reply)
-		{
-			Log.Info(MongoHelper.ToJson(message));
-
-			//entity.AddComponent<LogToClientComponent>();
-
-			reply(new R2C_SubscribeLog());
-		}
-	}
-}

+ 0 - 1
Server/Controller/Server.Controller.csproj

@@ -39,7 +39,6 @@
     <Compile Include="Message\M2A_ReloadHandler.cs" />
     <Compile Include="Message\C2M_ReloadHandler.cs" />
     <Compile Include="Message\R2G_GetLoginKeyHandler.cs" />
-    <Compile Include="Message\C2R_SubscribeLogHandler.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Message\C2R_LoginHandler.cs" />
   </ItemGroup>

+ 0 - 48
Server/Model/Component/LogToClientComponent.cs

@@ -1,48 +0,0 @@
-using Base;
-
-namespace Model
-{
-	[ObjectEvent]
-	public class LogToClientComponentEvent : ObjectEvent<LogToClientComponent>, IAwake
-	{
-		public void Awake()
-		{
-			this.GetValue().Awake();
-		}
-	}
-
-	public class LogToClientComponent : Component
-	{
-		private AppType appType;
-		private int appId;
-
-		public void Awake()
-		{
-			this.appType = Game.Scene.GetComponent<StartConfigComponent>().MyConfig.AppType;
-			this.appId = Game.Scene.GetComponent<StartConfigComponent>().MyConfig.AppId;
-			Log.Callback.Add(this.Id, this.LogToClient);
-		}
-
-		private void LogToClient(LogType type, string message)
-		{
-			if (this.Owner.Id == 0)
-			{
-				return;
-			}
-			this.GetOwner<Session>().Send(new R2C_ServerLog { AppType = this.appType, AppId = this.appId, Type = type, Log = message });
-		}
-
-		public override void Dispose()
-		{
-			if (this.Id == 0)
-			{
-				return;
-			}
-			long id = this.Id;
-
-			base.Dispose();
-
-			Log.Callback.Remove(id);
-		}
-	}
-}

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

@@ -189,7 +189,6 @@
       <Link>Other\Options.cs</Link>
     </Compile>
     <Compile Include="Component\AppManagerComponent.cs" />
-    <Compile Include="Component\LogToClientComponent.cs" />
     <Compile Include="Component\GateSessionKeyComponent.cs" />
     <Compile Include="Component\RealmGateAddressComponent.cs" />
     <Compile Include="Component\StartConfigComponent.cs" />

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

@@ -43,7 +43,7 @@ namespace MyEditor
 			{
 				using (StreamWriter sw = new StreamWriter(new FileStream(Path, FileMode.Create)))
 				{
-					sw.Write(MongoHelper.ToJson(this.config));
+					sw.Write(this.config);
 				}
 			}
 		}

+ 22 - 19
Unity/Assets/Scripts/Component/BenchmakComponent.cs

@@ -17,7 +17,7 @@ namespace Model
 	{
 		private int k;
 
-		private long time1 = TimeHelper.ClientNowTicks();
+		private long time1 = TimeHelper.ClientNow();
 
 		public async void Awake(string address)
 		{
@@ -32,35 +32,38 @@ namespace Model
 
 		private async void TestAsync(NetOuterComponent networkComponent, string address, int j)
 		{
-			using (Session session = networkComponent.Create(address))
+			try
 			{
-				int i = 0;
-				while (i < 10000000)
+				using (Session session = networkComponent.Create(address))
 				{
-					++i;
-					try
+					int i = 0;
+					while (i < 10000000)
 					{
+						++i;
 						await session.Call<C2R_Ping, R2C_Ping>(new C2R_Ping());
 
 						++this.k;
-						if (this.k % 100000 == 0)
+
+						if (this.k % 100000 != 0)
 						{
-							long time2 = TimeHelper.ClientNowTicks();
-							long time = time2 - this.time1;
-							this.time1 = time2;
-							Log.Info($"{j} Benchmark k: {k} 每10W次耗时: {time}");
+							continue;
 						}
-					}
-					catch (RpcException e)
-					{
-						Log.Error(e.ToString());
-					}
-					catch (Exception e)
-					{
-						Log.Error(e.ToString());
+
+						long time2 = TimeHelper.ClientNow();
+						long time = time2 - this.time1;
+						this.time1 = time2;
+						Log.Info($"{j} Benchmark k: {this.k} 每10W次耗时: {time} ms");
 					}
 				}
 			}
+			catch (RpcException e)
+			{
+				Log.Error(e.ToString());
+			}
+			catch (Exception e)
+			{
+				Log.Error(e.ToString());
+			}
 		}
 
 		public override void Dispose()

+ 0 - 5
Unity/Assets/Scripts/Entity/Config/StartConfig.cs

@@ -16,10 +16,5 @@ namespace Model
 		public StartConfig(): base(EntityType.Config)
 		{
 		}
-
-		public object Clone()
-		{
-			return MongoHelper.FromJson<StartConfig>(MongoHelper.ToJson(this));
-		}
 	}
 }

+ 9 - 22
Unity/Assets/Scripts/Entity/Message/Message.cs

@@ -1,4 +1,3 @@
-using System.Collections.Generic;
 using Base;
 using MongoDB.Bson.Serialization.Attributes;
 
@@ -8,9 +7,9 @@ namespace Model
 	[BsonIgnoreExtraElements]
 	public class C2R_Login: ARequest
 	{
-		[BsonElement("a")]
+		[BsonElement("A")]
 		public string Account;
-		[BsonElement("p")]
+		[BsonElement("P")]
 		public string Password;
 	}
 
@@ -18,9 +17,9 @@ namespace Model
 	[BsonIgnoreExtraElements]
 	public class R2C_Login: AResponse
 	{
-		[BsonElement("a")]
+		[BsonElement("A")]
 		public string Address { get; set; }
-		[BsonElement("k")]
+		[BsonElement("K")]
 		public long Key { get; set; }
 	}
 
@@ -28,28 +27,16 @@ namespace Model
 	[BsonIgnoreExtraElements]
 	public class R2C_ServerLog: AMessage
 	{
-		[BsonElement("at")]
+		[BsonElement("AT")]
 		public AppType AppType { get; set; }
-		[BsonElement("a")]
+		[BsonElement("A")]
 		public int AppId { get; set; }
-		[BsonElement("t")]
+		[BsonElement("T")]
 		public LogType Type { get; set; }
-		[BsonElement("l")]
+		[BsonElement("L")]
 		public string Log { get; set; }
 	}
 
-	[Message(4)]
-	[BsonIgnoreExtraElements]
-	public class C2R_SubscribeLog: ARequest
-	{
-	}
-
-	[Message(5)]
-	[BsonIgnoreExtraElements]
-	public class R2C_SubscribeLog: AResponse
-	{
-	}
-
 	[Message(6)]
 	[BsonIgnoreExtraElements]
 	public class R2G_GetLoginKey : ARequest
@@ -72,7 +59,7 @@ namespace Model
 	[BsonIgnoreExtraElements]
 	public class C2G_LoginGate : ARequest
 	{
-		[BsonElement("k")]
+		[BsonElement("K")]
 		public long Key;
 
 		public C2G_LoginGate(long key)

+ 4 - 1
Unity/Assets/Scripts/Message/AMessage.cs

@@ -1,7 +1,10 @@
 namespace Model
 {
-	public abstract class AMessage
+	public abstract class AMessage: Object
 	{
+		protected AMessage(): base(0)
+		{
+		}
 	}
 
 	public abstract class ARequest : AMessage

+ 8 - 3
Unity/Assets/Scripts/Object/Component.cs

@@ -1,9 +1,10 @@
-using MongoDB.Bson.Serialization.Attributes;
+using System;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
 	[BsonKnownTypes(typeof(AConfigComponent))]
-	public abstract class Component : Object
+	public abstract class Component : Object, IDisposable
 	{
 		[BsonIgnore]
 		public Entity Owner { get; set; }
@@ -15,10 +16,12 @@ namespace Model
 
 		protected Component()
 		{
+			ObjectManager.Instance.Add(this);
 		}
 
 		protected Component(long id): base(id)
 		{
+			ObjectManager.Instance.Add(this);
 		}
 
 		protected T GetComponent<T>() where T: Component
@@ -32,8 +35,10 @@ namespace Model
 			{
 				return;
 			}
-			
+
 			base.Dispose();
+			
+			ObjectManager.Instance.Remove(this);
 		}
 	}
 }

+ 5 - 1
Unity/Assets/Scripts/Object/Entity.cs

@@ -20,11 +20,13 @@ namespace Model
 		protected Entity(EntityType entityType)
 		{
 			this.Type = entityType;
+			ObjectManager.Instance.Add(this);
 		}
 
 		protected Entity(long id, EntityType entityType) : base(id)
 		{
 			this.Type = entityType;
+			ObjectManager.Instance.Add(this);
 		}
 		
 		public override void Dispose()
@@ -33,7 +35,7 @@ namespace Model
 			{
 				return;
 			}
-			
+
 			base.Dispose();
 
 			foreach (Component component in this.GetComponents())
@@ -47,6 +49,8 @@ namespace Model
 					Log.Error(e.ToString());
 				}
 			}
+
+			ObjectManager.Instance.Remove(this);
 		}
 
 		public K AddComponent<K>() where K : Component, new()

+ 14 - 12
Unity/Assets/Scripts/Object/Object.cs

@@ -5,41 +5,43 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-	public abstract class Object: IDisposable, ISupportInitialize
+	public abstract class Object: IDisposable, ISupportInitialize, ICloneable
 	{
 		[BsonId]
+		[BsonIgnoreIfDefault]
 		public long Id { get; private set; }
 
 		protected Object()
 		{
 			Id = IdGenerater.GenerateId();
-			ObjectManager.Instance.Add(this);
 		}
 
 		protected Object(long id)
 		{
 			this.Id = id;
-			ObjectManager.Instance.Add(this);
 		}
 
-		public virtual void Dispose()
+		public virtual void BeginInit()
 		{
-			if (this.Id == 0)
-			{
-				return;
-			}
+		}
 
-			ObjectManager.Instance.Remove(this);
+		public virtual void EndInit()
+		{
+		}
 
-			this.Id = 0;
+		public override string ToString()
+		{
+			return MongoHelper.ToJson(this);
 		}
 
-		public virtual void BeginInit()
+		public virtual void Dispose()
 		{
+			this.Id = 0;
 		}
 
-		public virtual void EndInit()
+		public object Clone()
 		{
+			return MongoHelper.FromJson(this.GetType(), this.ToString());
 		}
 	}
 }