using System; using System.Collections.Generic; namespace XGame.Framework.Network { public abstract class MsgGenerator : IMsgGenerator { //private Dictionary _messageMap = new(); private Dictionary _controllerMap = new(); private Dictionary _msgTypeMap = new(); public IMessage GetMessage(int protoId) { // TODO 目前使用json序列化数据,先屏蔽该方法 return null; //if (!_messageMap.TryGetValue(protoId, out IMessage msg)) //{ // return CreateMessage(protoId); //} //else //{ // _messageMap.Remove(protoId); //} //return msg; } public Type IdToType(int protoId) { if (_msgTypeMap.TryGetValue(protoId, out Type type)) { return type; } type = GetMessageType(protoId); _msgTypeMap.Add(protoId, type); return type; } public void RecycleMessage(IMessage message) { // TODO 目前使用json序列化数据,先屏蔽该方法 message.Clear(); return; //if (!_messageMap.ContainsKey(message.ProtocolID)) //{ // _messageMap.Add(message.ProtocolID, message); //} } public IMsgController GetController(int protoId) { if (!_controllerMap.TryGetValue(protoId, out IMsgController controller)) { controller = CreateController(protoId); Assert.IsNotNull(controller, "[Net] protocol:{0}没有生成对应的Controller,如已生成请检查proto文件", protoId); _controllerMap.Add(protoId, controller); } return controller; } protected abstract IMessage CreateMessage(int protoId); protected abstract IMsgController CreateController(int protoId); protected abstract Type GetMessageType(int protoId); public void Clean() { //_messageMap.Clear(); _controllerMap.Clear(); _msgTypeMap.Clear(); } } }