1
This commit is contained in:
parent
6862308f07
commit
1235fada93
98
F6/MsgHandler.cs
Normal file
98
F6/MsgHandler.cs
Normal file
@ -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<HandlerMsg> handlers = new List<HandlerMsg>();
|
||||
}
|
||||
|
||||
class MsgHandler
|
||||
{
|
||||
public delegate void HandlerDelegate(int msgId, byte[] msg);
|
||||
public delegate void HandlerDelegateEx<in T>(T msg);
|
||||
|
||||
private Type cmMsgType;
|
||||
private Type smMsgType;
|
||||
private Dictionary<int, HandlerRec> handlerHash = new Dictionary<int, HandlerRec>();
|
||||
|
||||
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<T>(HandlerDelegateEx<T> handler) where T : IMessage<T> ,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
20
Net/BattleHandler.cs
Normal file
20
Net/BattleHandler.cs
Normal file
@ -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<battle.cs.SMKcpHandshake> SMKcpHandshakeHandler;
|
||||
|
||||
public BattleHandler(): base(typeof(battle.cs.CMMessageId_e), typeof(battle.cs.SMMessageId_e))
|
||||
{
|
||||
|
||||
this.RegisterHandler(this.SMKcpHandshakeHandler);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -183,11 +183,6 @@ namespace Net
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
int i = 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user