diff --git a/F6/MsgHandler.cs b/F6/MsgHandler.cs new file mode 100644 index 0000000..6b80f98 --- /dev/null +++ b/F6/MsgHandler.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections; +using Google.Protobuf; +using pbr = global::Google.Protobuf.Reflection; + +namespace F6 +{ + public delegate Google.Protobuf.IMessage CreateMsg(); + public delegate void HandlerMsg(Google.Protobuf.IMessage msg); + internal class HandlerRec + { + internal CreateMsg createMsg; + internal List handlers = new List(); + } + + class MsgHandler + { + public delegate void HandlerDelegate(int msgId, byte[] msg); + public delegate void HandlerDelegateEx(T msg); + + private Type cmMsgType; + private Type smMsgType; + private Dictionary handlerHash = new Dictionary(); + + public MsgHandler(Type cmMsgType, Type smMsgType) + { + this.cmMsgType = cmMsgType; + this.smMsgType = smMsgType; + } + + public int GetCMMsgId(string msgName) + { + return this.GetPbMsgId(this.cmMsgType, "_" + msgName); + } + + public int GetSMMsgId(string msgName) + { + return this.GetPbMsgId(this.smMsgType, "_" + msgName); + } + + public int GetPbMsgId(Type enumType, string msgName) + { + foreach (var f in enumType.GetFields()) + { + var tAttrs = f.GetCustomAttributes(typeof(pbr::OriginalNameAttribute), false); + if (tAttrs.Length > 0) + { + var tAttr = (pbr::OriginalNameAttribute)tAttrs[0]; + if (tAttr.Name.Equals(msgName)) + { + var val = Enum.Parse(enumType, f.Name); + return Convert.ToInt32(val); + } + } + } + return 0; + } + + protected void RegisterHandler(HandlerDelegateEx handler) where T : IMessage ,new() + { + int msgId = this.GetSMMsgId(typeof(T).Name); + HandlerRec handler1; + if (!this.handlerHash.TryGetValue(msgId, out handler1)) + { + handler1 = new HandlerRec(); + handler1.createMsg += () => + { + return new T(); + }; + this.handlerHash.Add(msgId, handler1); + } + handler1.handlers.Add((Google.Protobuf.IMessage msg) => + { + handler((T)msg); + }); + } + + virtual protected void DispatchMsg(int msgId, byte[] data) + { + HandlerRec handler; + if (this.handlerHash.TryGetValue(msgId, out handler)) + { + var msg = handler.createMsg(); + msg.Descriptor.Parser.ParseFrom(data); + foreach (var h in handler.handlers) + { + h(msg); + } + } + } + + } + +} diff --git a/Net/BattleHandler.cs b/Net/BattleHandler.cs new file mode 100644 index 0000000..c4bced4 --- /dev/null +++ b/Net/BattleHandler.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Net +{ + class BattleHandler : F6.MsgHandler + { + public HandlerDelegateEx SMKcpHandshakeHandler; + + public BattleHandler(): base(typeof(battle.cs.CMMessageId_e), typeof(battle.cs.SMMessageId_e)) + { + + this.RegisterHandler(this.SMKcpHandshakeHandler); + } + } + +} diff --git a/Net/ClientNet.cs b/Net/ClientNet.cs index 0f52445..ebdf0b2 100644 --- a/Net/ClientNet.cs +++ b/Net/ClientNet.cs @@ -183,11 +183,6 @@ namespace Net } } - void Awake() - { - int i = 0; - } - } }