微信登录

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

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