微信登录

NET - Message.cs - 解析/拼合数据工具

  1. public class Message {
  2. private byte[] data = new byte[1024];
  3. private int startIndex = 0;//我们存取了多少个字节的数据在数组里面
  4. public byte[] Data {get { return data; }}
  5. public int StartIndex{get { return startIndex; }}
  6. public int RemainSize{get { return data.Length - startIndex; }}
  7. /// 解析数据或者叫做读取数据
  8. public void ReadMessage(int newDataAmount, Action<ActionCode, string> processDataCallback) {}
  9. /// 打包数据
  10. public static byte[] PackData(请求码,动作码, 数据){}
  11. }
  1. public class Message {
  2. private byte[] data = new byte[1024];
  3. private int startIndex = 0;//我们存取了多少个字节的数据在数组里面
  4. public byte[] Data {get { return data; }}
  5. public int StartIndex{get { return startIndex; }}
  6. public int RemainSize{get { return data.Length - startIndex; }}
  7. /// 解析数据或者叫做读取数据
  8. public void ReadMessage(int newDataAmount, Action<ActionCode, string> processDataCallback) {}
  9. /// 打包数据
  10. public static byte[] PackData(RequestCode requestData,ActionCode actionCode, string data){}
  11. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using Common;
  6. using System.Text;
  7. using System.Linq;
  8. public class Message {
  9. private byte[] data = new byte[1024];
  10. private int startIndex = 0;//我们存取了多少个字节的数据在数组里面
  11. //public void AddCount(int count)
  12. //{
  13. // startIndex += count;
  14. //}
  15. public byte[] Data
  16. {
  17. get { return data; }
  18. }
  19. public int StartIndex
  20. {
  21. get { return startIndex; }
  22. }
  23. public int RemainSize
  24. {
  25. get { return data.Length - startIndex; }
  26. }
  27. /// <summary>
  28. /// 解析数据或者叫做读取数据
  29. /// </summary>
  30. public void ReadMessage(int newDataAmount, Action<ActionCode, string> processDataCallback)
  31. {
  32. startIndex += newDataAmount;
  33. while (true)
  34. {
  35. if (startIndex <= 4) return;
  36. int count = BitConverter.ToInt32(data, 0);
  37. if ((startIndex - 4) >= count)
  38. {
  39. //Console.WriteLine(startIndex);
  40. //Console.WriteLine(count);
  41. //string s = Encoding.UTF8.GetString(data, 4, count);
  42. //Console.WriteLine("解析出来一条数据:" + s);
  43. ActionCode actionCode = (ActionCode)BitConverter.ToInt32(data, 4);
  44. string s = Encoding.UTF8.GetString(data, 8, count - 4);
  45. processDataCallback(actionCode, s);
  46. Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);
  47. startIndex -= (count + 4);
  48. }
  49. else
  50. {
  51. break;
  52. }
  53. }
  54. }
  55. //public static byte[] PackData(ActionCode actionCode, string data)
  56. //{
  57. // byte[] requestCodeBytes = BitConverter.GetBytes((int)actionCode);
  58. // byte[] dataBytes = Encoding.UTF8.GetBytes(data);
  59. // int dataAmount = requestCodeBytes.Length + dataBytes.Length;
  60. // byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);
  61. // byte[] newBytes = dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>();//Concat(dataBytes);
  62. // return newBytes.Concat(dataBytes).ToArray<byte>();
  63. //}
  64. /// <summary>
  65. /// 打包数据
  66. /// </summary>
  67. public static byte[] PackData(RequestCode requestData,ActionCode actionCode, string data)
  68. {
  69. byte[] requestCodeBytes = BitConverter.GetBytes((int)requestData);
  70. byte[] actionCodeBytes = BitConverter.GetBytes((int)actionCode);
  71. byte[] dataBytes = Encoding.UTF8.GetBytes(data);
  72. int dataAmount = requestCodeBytes.Length + dataBytes.Length + actionCodeBytes.Length;
  73. byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);
  74. //byte[] newBytes = dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>();//Concat(dataBytes);
  75. //return newBytes.Concat(dataBytes).ToArray<byte>();
  76. return dataAmountBytes.Concat(requestCodeBytes).ToArray<byte>()
  77. .Concat(actionCodeBytes).ToArray<byte>()
  78. .Concat(dataBytes).ToArray<byte>();
  79. }
  80. }
NET - Message.cs - 解析/拼合数据工具