1)需要一个构造函数,接受报文数据
//需要一个构造函数,接受报文数据
public BufferEntity(IPEndPoint endPoint,byte[] buffer)
{
this.endPoint = endPoint;
this.buffer = buffer;
Decode();
}
2)接受报文数据的具体函数Decode()
//当我们收到客户端的报文,或服务器收到客户端的报文,要怎么处理收到的报文
//将报文反序列化成实体里面的成员
private void Decode()
{
//将字节数组转化成int或long
//Toint32,转化成int对象
//Toint64,转换成long对象
protoSize = BitConverter.ToInt32(buffer,0); //从0的位置,取四个字节转成int
session = BitConverter.ToInt32(buffer, 4); //从4的位置,取四个字节转换成int
sn = BitConverter.ToInt32(buffer, 8); //从8的位置,取四个字节抓换成int
moduleID = BitConverter.ToInt64(buffer, 12);
time = BitConverter.ToInt32(buffer, 16); //从16的位置,取八个字节转换成int
messageType = BitConverter.ToInt32(buffer, 24);
messageID = BitConverter.ToInt32(buffer, 28);
//messageType为0表示ACK报文
if (0 == messageType)
{
}
//否则为业务数据
else
{
//new一个空间来存业务数据,空间大小为protoSize
proto = new byte[protoSize];
//把buffer剩下的数据复制到proto,也就是将buffer32位以后的数据复制到proto里面,复制大小为protoSize的大小
Array.Copy(buffer, 32, proto, 0, protoSize);
}
}
3)用到的函数
1)Toint32,转化成int对象,Toint64,转换成long对象
例如:
2)Array.Copy()将数据复制到proto