TcpDataConverter.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Azylee.Core.WindowsUtils.ConsoleUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Azylee.YeahWeb.SocketUtils.TcpUtils
  7. {
  8. public static class TcpDataConverter
  9. {
  10. const int HeadLength = 4 + 4 + 4;
  11. const int ReceiveBufferSize = 1024;
  12. public static int Byte2Model(List<byte> data, out TcpDataModel model)
  13. {
  14. model = null;
  15. if (data.Count > HeadLength &&
  16. data[0] == 111 && data[1] == 222 && data[2] == 66 && data[3] == 66)
  17. {
  18. int msgCode = BitConverter.ToInt32(new byte[] { data[4], data[5], data[6], data[7] }, 0);
  19. int msgBodyLength = BitConverter.ToInt32(new byte[] { data[8], data[9], data[10], data[11] }, 0);
  20. if (data.Count >= HeadLength + msgBodyLength)
  21. {
  22. byte[] body = data.GetRange(HeadLength, msgBodyLength).ToArray();
  23. string bodyToGBK = Encoding.GetEncoding("GBK").GetString(body);
  24. //ReceiveByteContent(body);
  25. Cons.Log(bodyToGBK);
  26. //Send(ReceiveByte.GetRange(0, 6).ToArray());
  27. data.RemoveRange(0, 6 + msgBodyLength);
  28. //ReceiveMessage?.Invoke(msgCode, body);
  29. }
  30. }
  31. else
  32. {
  33. data.Clear();
  34. //Socket.Send(new byte[] { 0 });
  35. }
  36. return 0;
  37. }
  38. }
  39. }