ZoneNodeCodec.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using CommonAI.Zone;
  2. using CommonAI.ZoneServer.JSGModule;
  3. using CommonLang.ByteOrder;
  4. using CommonLang.IO;
  5. using CommonLang.IO.Attribute;
  6. using CommonLang.Log;
  7. using CommonLang.Property;
  8. using CommonLang.Protocol;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Text;
  14. namespace XmdsServerNode.Node
  15. {
  16. public class ZoneNodeCodec
  17. {
  18. private Logger log = LoggerFactory.GetLogger("BattleCodec");
  19. private ArraySegment<byte> ZERO_BUFF = new ArraySegment<byte>(new byte[0]);
  20. private const int FIXED_HEADER_SIZE = 4;
  21. private const int DEFAULT_BUFFER_SIZE = 1024;
  22. private IExternalizableFactory sl = null;
  23. private TemplateManager templates = ZoneNodeManager.Templates.Templates;
  24. private MemoryStream input;
  25. private MemoryStream output;
  26. private InputStream reader;
  27. private OutputStream writer;
  28. public ZoneNodeCodec(IExternalizableFactory externalizableFactory)
  29. {
  30. this.sl = externalizableFactory;
  31. this.input = new MemoryStream(DEFAULT_BUFFER_SIZE);
  32. this.output = new MemoryStream(DEFAULT_BUFFER_SIZE);
  33. this.reader = new InputStream(input, sl);
  34. this.writer = new OutputStream(output, sl);
  35. }
  36. public CommonLang.IO.IExternalizableFactory Factory
  37. {
  38. get { return sl; }
  39. }
  40. public bool doDecode(byte[] data, out IMessage message)
  41. {
  42. //lock (input)
  43. {
  44. input.Position = 0;
  45. input.Write(data, 0, data.Length);
  46. input.Position = 0;
  47. int typeInt = reader.GetS32();
  48. Type type = sl.GetType(typeInt);
  49. if (type == null)
  50. {
  51. log.Error("Unknow Protocol : >>>" + typeInt + "<<<");
  52. message = null;
  53. return false;
  54. }
  55. else
  56. {
  57. IMessage nm = (IMessage)ReflectionUtil.CreateInstance(type);
  58. nm.ReadExternal(reader);
  59. if (nm is BattleMessage)
  60. {
  61. ((BattleMessage)nm).EndRead(templates);
  62. }
  63. #if JSGProfile
  64. JSGServerProfile.RecordRecv(typeInt, input.Position);
  65. #endif
  66. message = nm;
  67. return true;
  68. }
  69. }
  70. }
  71. public bool doEncode(IMessage message, out ArraySegment<byte> data)
  72. {
  73. int typeInt = sl.GetTypeID(message.GetType());
  74. if (typeInt == 0)
  75. {
  76. log.Error("Unknow Protocol : >>>" + typeInt + "<<< - " + message.GetType().FullName);
  77. data = ZERO_BUFF;
  78. return false;
  79. }
  80. if (message is BattleMessage)
  81. {
  82. ((BattleMessage)message).BeforeWrite(templates);
  83. }
  84. //lock (output)
  85. {
  86. output.Position = 0;
  87. writer.PutS32(typeInt);
  88. message.WriteExternal(writer);
  89. int len = (int)output.Position;
  90. data = new ArraySegment<byte>(output.GetBuffer(), 0, len);
  91. }
  92. return true;
  93. }
  94. }
  95. }