123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- namespace XGame.Framework.Network
- {
- public abstract class MsgGenerator : IMsgGenerator
- {
- //private Dictionary<int, IMessage> _messageMap = new();
- private Dictionary<int, IMsgController> _controllerMap = new();
- private Dictionary<int, Type> _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();
- }
- }
- }
|