MsgGenerator.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. namespace XGame.Framework.Network
  4. {
  5. public abstract class MsgGenerator : IMsgGenerator
  6. {
  7. //private Dictionary<int, IMessage> _messageMap = new();
  8. private Dictionary<int, IMsgController> _controllerMap = new();
  9. private Dictionary<int, Type> _msgTypeMap = new();
  10. public IMessage GetMessage(int protoId)
  11. { // TODO 目前使用json序列化数据,先屏蔽该方法
  12. return null;
  13. //if (!_messageMap.TryGetValue(protoId, out IMessage msg))
  14. //{
  15. // return CreateMessage(protoId);
  16. //}
  17. //else
  18. //{
  19. // _messageMap.Remove(protoId);
  20. //}
  21. //return msg;
  22. }
  23. public Type IdToType(int protoId)
  24. {
  25. if (_msgTypeMap.TryGetValue(protoId, out Type type))
  26. {
  27. return type;
  28. }
  29. type = GetMessageType(protoId);
  30. _msgTypeMap.Add(protoId, type);
  31. return type;
  32. }
  33. public void RecycleMessage(IMessage message)
  34. { // TODO 目前使用json序列化数据,先屏蔽该方法
  35. message.Clear();
  36. return;
  37. //if (!_messageMap.ContainsKey(message.ProtocolID))
  38. //{
  39. // _messageMap.Add(message.ProtocolID, message);
  40. //}
  41. }
  42. public IMsgController GetController(int protoId)
  43. {
  44. if (!_controllerMap.TryGetValue(protoId, out IMsgController controller))
  45. {
  46. controller = CreateController(protoId);
  47. Assert.IsNotNull(controller, "[Net] protocol:{0}没有生成对应的Controller,如已生成请检查proto文件", protoId);
  48. _controllerMap.Add(protoId, controller);
  49. }
  50. return controller;
  51. }
  52. protected abstract IMessage CreateMessage(int protoId);
  53. protected abstract IMsgController CreateController(int protoId);
  54. protected abstract Type GetMessageType(int protoId);
  55. public void Clean()
  56. {
  57. //_messageMap.Clear();
  58. _controllerMap.Clear();
  59. _msgTypeMap.Clear();
  60. }
  61. }
  62. }