45 lines
1.0 KiB
C#
45 lines
1.0 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace Oculus.Platform.Samples.NetChat
|
|
{
|
|
public class chatPacket
|
|
{
|
|
public int packetID { get; set; }
|
|
|
|
public string textString { get; set; }
|
|
|
|
public byte[] Serialize()
|
|
{
|
|
using (MemoryStream memoryStream = new MemoryStream())
|
|
{
|
|
using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
|
|
{
|
|
if (textString.Length > 512)
|
|
{
|
|
textString = textString.Substring(0, 511);
|
|
}
|
|
binaryWriter.Write(packetID);
|
|
binaryWriter.Write(textString.ToCharArray());
|
|
binaryWriter.Write('\0');
|
|
}
|
|
return memoryStream.ToArray();
|
|
}
|
|
}
|
|
|
|
public static chatPacket Deserialize(byte[] data)
|
|
{
|
|
chatPacket chatPacket2 = new chatPacket();
|
|
using (MemoryStream input = new MemoryStream(data))
|
|
{
|
|
using (BinaryReader binaryReader = new BinaryReader(input))
|
|
{
|
|
chatPacket2.packetID = binaryReader.ReadInt32();
|
|
chatPacket2.textString = Encoding.Default.GetString(binaryReader.ReadBytes(512));
|
|
return chatPacket2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|