FastStreamMessageDecoder.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using SuperSocket.ProtoBase;
  2. using System.Collections.Generic;
  3. using System;
  4. using System.Text;
  5. namespace ET.Server
  6. {
  7. [ChildOf(typeof(FastStreamComponent))]
  8. public class FastStreamMessageDecoder : FixedHeaderReceiveFilter<FastStreamBuffer>
  9. {
  10. public const int LEN_HEAD = 5;
  11. private int uidLen;
  12. private int msgLen;
  13. public FastStreamMessageDecoder(int headerSize) : base(headerSize)
  14. {
  15. }
  16. public override FastStreamBuffer ResolvePackage(IBufferStream bufferStream)
  17. {
  18. bufferStream.Skip(LEN_HEAD);
  19. var uid = bufferStream.ReadString(uidLen, Encoding.UTF8);
  20. var data = new byte[msgLen];
  21. bufferStream.Read(data, 0, msgLen);
  22. return new FastStreamBuffer(Convert.ToUInt64(uid, 10), ref data);
  23. }
  24. protected override int GetBodyLengthFromHeader(IBufferStream buffer, int headerSize)
  25. {
  26. uidLen = buffer.ReadByte();
  27. msgLen = (int)buffer.ReadUInt32(true);
  28. return uidLen + msgLen;
  29. }
  30. }
  31. public class FastStreamBuffer : IPackageInfo<ulong>
  32. {
  33. public ulong Key { get; private set; }
  34. public byte[] Data;
  35. public FastStreamBuffer(ulong key, ref byte[] data)
  36. {
  37. this.Key = key;
  38. this.Data = data;
  39. }
  40. }
  41. }