public static byte[] EncodeMsg(int opCode, int subCode, object value)
{
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(opCode);
bw.Write(subCode);
//如果不等于null 才需要把object 转换成字节数据 存起来
if (value != null)
{
byte[] valueBytes = EncodeObj(value);
bw.Write(valueBytes);
}
byte[] data = new byte[ms.Length];
Buffer.BlockCopy(ms.GetBuffer(), 0, data, 0, (int)ms.Length);
bw.Close();
ms.Close();
return data;
}
public static byte[] EncodePacket(byte[] data)
{
//内存流对象
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms))
{
//先写入长度
bw.Write(data.Length);
//再写入数据
bw.Write(data);
byte[] byteArray = new byte[(int)ms.Length];
Buffer.BlockCopy(ms.GetBuffer(), 0, byteArray, 0, (int)ms.Length);
return byteArray;
}
}
}