使用socket&Tcp开发网络游戏代码

本文介绍了一个消息类的设计,包括如何解析接收到的消息数据并进行处理,同时提供了消息数据的封装方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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.   
  8.     class Message  
  9.     {  
  10.         private byte[] data = new byte[1024]; //这个数组用来存取我们读取到的数据,让消息的长度最少能存的下1024 ,如果说最大的消息数组存不下的话 就没办法完整的读取这条消息了  
  11.         //如果消息不完整 我们不处理。我们会读取下一条  
  12.         private int startIndex = 0;//标识位,表示现在数据存储在什么位置了 ,如果没数据 sartIndex从0开始,如果存了10个之后 这个startIndex就等于10;  
  13.                                    //再来数据的话就从10开始存,同时也代表了存了多少个字节的数据。  
  14.   
  15.         /// <summary>  
  16.         /// 这个数组用来存取我们读取到的数据  
  17.         /// </summary>  
  18.         public byte[] Data  
  19.         {  
  20.             get { return data; }  
  21.         }  
  22.         /// <summary>  
  23.         /// 代表存储了多少个字节的数据  开始索引  
  24.         /// </summary>  
  25.         public int StartIndex  
  26.         {  
  27.             get { return startIndex; }  
  28.         }  
  29.         /// <summary>  
  30.         /// 剩余的空间  
  31.         /// </summary>  
  32.         public int RemaniSize  
  33.         {  
  34.             get { return data.Length - startIndex; }  
  35.         }  
  36.         /// <summary>  
  37.         /// 更新了多少数据  
  38.         /// </summary>  
  39.         /// <param name="count"></param>  
  40.         //public void AddCount(int count)  
  41.         //{  
  42.         //    startIndex += count;  
  43.         //}  
  44.         /// <summary>  
  45.         /// 解析数据或者叫做读取数据  newDataAmount数量  
  46.         /// </summary>  
  47.         public void ReadMessage(int newDataAmount, Action<RequesCode, ActionCode, string> processDataCallback)  
  48.         {  
  49.             startIndex += newDataAmount;  
  50.   
  51.   
  52.             while (true)  
  53.             {  
  54.                 if (startIndex <= 4) return;  
  55.                 int count = BitConverter.ToInt32(data, 0);//这个只占前四个字节  ,变成int 就是数据长度  
  56.                 if ((startIndex - 4) >= count) //(startIndex-4)是剩余数据的长度 ,大于count 就说明数据是完整的  
  57.                 {  
  58.                     //string s = Encoding.UTF8.GetString(data, 4, count);  
  59.                     //Console.WriteLine("解析出来一条数据 :" + s);  
  60.                     RequesCode requestCode = (RequesCode)BitConverter.ToInt32(data, 4); ///解析出来的是ResourceCode  
  61.                     ActionCode actionCode = (ActionCode)BitConverter.ToInt32(data, 8); ///解析出来的是ActionCode  
  62.                     string s = Encoding.UTF8.GetString(data, 12, count - 8);  
  63.   
  64.                     processDataCallback(requestCode, actionCode, s);  
  65.   
  66.                     Array.Copy(data, count + 4, data, 0, startIndex - 4 - count);  
  67.   
  68.                     startIndex -= (count + 4); //移动完了之后更新startIndex  
  69.                 }  
  70.                 else  
  71.                 {  
  72.                     break;  
  73.                 }  
  74.             }  
  75.   
  76.   
  77.         }  
  78.   
  79.   
  80.         /// <summary>  
  81.         /// 用于包装  
  82.         /// </summary>  
  83.         /// <param name="requsetData"></param>  
  84.         /// <param name="data"></param>  
  85.         /// <returns></returns>  
  86.         public static byte[] PakeData(RequesCode requsetData,ActionCode actionCode, string data)  
  87.         {  
  88.             byte[] requsetCodeBytes = BitConverter.GetBytes((int)requsetData);  
  89.             byte[] actionCodeBytes = BitConverter.GetBytes((int)actionCode);  
  90.   
  91.             byte[] dataBytes = Encoding.UTF8.GetBytes(data);  
  92.   
  93.             int dataAmount = requsetCodeBytes.Length + dataBytes.Length+actionCodeBytes.Length;  
  94.   
  95.             byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);  
  96.   
  97.             return dataAmountBytes.Concat(requsetCodeBytes).ToArray<byte>().Concat(actionCodeBytes).ToArray<byte>().Concat(dataBytes).ToArray<byte>();  
  98.   
  99.   
  100.         }  
  101.   
  102.   
  103.   
  104.         /// <summary>  
  105.         /// 用于包装  
  106.         /// </summary>  
  107.         /// <param name="requsetData"></param>  
  108.         /// <param name="data"></param>  
  109.         /// <returns></returns>  
  110.         public static byte[] PakeData(RequesCode requsetData, string data)  
  111.         {  
  112.             byte[] requsetCodeBytes = BitConverter.GetBytes((int)requsetData);  
  113.   
  114.             byte[] dataBytes = Encoding.UTF8.GetBytes(data);  
  115.   
  116.             int dataAmount = requsetCodeBytes.Length + dataBytes.Length;  
  117.             byte[] dataAmountBytes = BitConverter.GetBytes(dataAmount);  
  118.             return dataAmountBytes.Concat(requsetCodeBytes).Concat(dataBytes).ToArray();  
  119.   
  120.   
  121.         }  
  122.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值