From e041e2b7d3e93278552e4da96ef47f131595ae55 Mon Sep 17 00:00:00 2001 From: tanghai Date: Fri, 11 Nov 2016 10:46:21 +0800 Subject: [PATCH] =?UTF-8?q?1.Object=E5=A2=9E=E5=8A=A0ToString=E6=96=B9?= =?UTF-8?q?=E6=B3=95,=E4=BD=BF=E7=94=A8MongoHelper.ToJson=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0,=E6=96=B9=E4=BE=BF=E6=89=93=E5=8D=B0=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=202.=E5=88=A0=E9=99=A4=E8=AE=A2=E9=98=85Server=20Log?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/Base/Log.cs | 19 +------- .../Message/C2R_SubscribeLogHandler.cs | 19 -------- Server/Controller/Server.Controller.csproj | 1 - .../Model/Component/LogToClientComponent.cs | 48 ------------------- Server/Model/Server.Model.csproj | 1 - .../ClientConfigEditor/ClientConfigEditor.cs | 2 +- .../Scripts/Component/BenchmakComponent.cs | 41 ++++++++-------- .../Scripts/Entity/Config/StartConfig.cs | 5 -- .../Assets/Scripts/Entity/Message/Message.cs | 31 ++++-------- Unity/Assets/Scripts/Message/AMessage.cs | 5 +- Unity/Assets/Scripts/Object/Component.cs | 11 +++-- Unity/Assets/Scripts/Object/Entity.cs | 6 ++- Unity/Assets/Scripts/Object/Object.cs | 26 +++++----- 13 files changed, 64 insertions(+), 151 deletions(-) delete mode 100644 Server/Controller/Message/C2R_SubscribeLogHandler.cs delete mode 100644 Server/Model/Component/LogToClientComponent.cs diff --git a/Server/Base/Log.cs b/Server/Base/Log.cs index 68db1cc6..4a6e31d8 100644 --- a/Server/Base/Log.cs +++ b/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> Callback { get; } = new Dictionary>(); - - 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); } } } diff --git a/Server/Controller/Message/C2R_SubscribeLogHandler.cs b/Server/Controller/Message/C2R_SubscribeLogHandler.cs deleted file mode 100644 index 3c7b8e9b..00000000 --- a/Server/Controller/Message/C2R_SubscribeLogHandler.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using Base; -using Model; - -namespace Controller -{ - [MessageHandler(AppType.Realm)] - public class C2R_SubscribeLogHandler : AMRpcHandler - { - protected override void Run(Session session, C2R_SubscribeLog message, Action reply) - { - Log.Info(MongoHelper.ToJson(message)); - - //entity.AddComponent(); - - reply(new R2C_SubscribeLog()); - } - } -} \ No newline at end of file diff --git a/Server/Controller/Server.Controller.csproj b/Server/Controller/Server.Controller.csproj index f0ef4595..b932a7f1 100644 --- a/Server/Controller/Server.Controller.csproj +++ b/Server/Controller/Server.Controller.csproj @@ -39,7 +39,6 @@ - diff --git a/Server/Model/Component/LogToClientComponent.cs b/Server/Model/Component/LogToClientComponent.cs deleted file mode 100644 index 701a248b..00000000 --- a/Server/Model/Component/LogToClientComponent.cs +++ /dev/null @@ -1,48 +0,0 @@ -using Base; - -namespace Model -{ - [ObjectEvent] - public class LogToClientComponentEvent : ObjectEvent, 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().MyConfig.AppType; - this.appId = Game.Scene.GetComponent().MyConfig.AppId; - Log.Callback.Add(this.Id, this.LogToClient); - } - - private void LogToClient(LogType type, string message) - { - if (this.Owner.Id == 0) - { - return; - } - this.GetOwner().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); - } - } -} diff --git a/Server/Model/Server.Model.csproj b/Server/Model/Server.Model.csproj index 6130d8ea..1cc5a417 100644 --- a/Server/Model/Server.Model.csproj +++ b/Server/Model/Server.Model.csproj @@ -189,7 +189,6 @@ Other\Options.cs - diff --git a/Unity/Assets/Editor/ClientConfigEditor/ClientConfigEditor.cs b/Unity/Assets/Editor/ClientConfigEditor/ClientConfigEditor.cs index 0d57d972..6a97e340 100644 --- a/Unity/Assets/Editor/ClientConfigEditor/ClientConfigEditor.cs +++ b/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); } } } diff --git a/Unity/Assets/Scripts/Component/BenchmakComponent.cs b/Unity/Assets/Scripts/Component/BenchmakComponent.cs index 84515feb..5330dbe8 100644 --- a/Unity/Assets/Scripts/Component/BenchmakComponent.cs +++ b/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(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() diff --git a/Unity/Assets/Scripts/Entity/Config/StartConfig.cs b/Unity/Assets/Scripts/Entity/Config/StartConfig.cs index 5b59843f..b5ea5c73 100644 --- a/Unity/Assets/Scripts/Entity/Config/StartConfig.cs +++ b/Unity/Assets/Scripts/Entity/Config/StartConfig.cs @@ -16,10 +16,5 @@ namespace Model public StartConfig(): base(EntityType.Config) { } - - public object Clone() - { - return MongoHelper.FromJson(MongoHelper.ToJson(this)); - } } } diff --git a/Unity/Assets/Scripts/Entity/Message/Message.cs b/Unity/Assets/Scripts/Entity/Message/Message.cs index a665439f..7fafd1f3 100644 --- a/Unity/Assets/Scripts/Entity/Message/Message.cs +++ b/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) diff --git a/Unity/Assets/Scripts/Message/AMessage.cs b/Unity/Assets/Scripts/Message/AMessage.cs index b1d85217..d8434d48 100644 --- a/Unity/Assets/Scripts/Message/AMessage.cs +++ b/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 diff --git a/Unity/Assets/Scripts/Object/Component.cs b/Unity/Assets/Scripts/Object/Component.cs index 50d234c2..04ae886d 100644 --- a/Unity/Assets/Scripts/Object/Component.cs +++ b/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() where T: Component @@ -32,8 +35,10 @@ namespace Model { return; } - + base.Dispose(); + + ObjectManager.Instance.Remove(this); } } } \ No newline at end of file diff --git a/Unity/Assets/Scripts/Object/Entity.cs b/Unity/Assets/Scripts/Object/Entity.cs index c50d3ecd..79909ce7 100644 --- a/Unity/Assets/Scripts/Object/Entity.cs +++ b/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() where K : Component, new() diff --git a/Unity/Assets/Scripts/Object/Object.cs b/Unity/Assets/Scripts/Object/Object.cs index 79a860b4..740e208d 100644 --- a/Unity/Assets/Scripts/Object/Object.cs +++ b/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()); } } } \ No newline at end of file -- GitLab