quantum-space-buddies/EpicOnlineTransport/Packet.cs

50 lines
1003 B
C#
Raw Normal View History

2022-02-06 02:06:25 +00:00
using System;
2022-03-03 03:46:33 +00:00
namespace EpicTransport;
public struct Packet
2022-02-06 04:17:08 +00:00
{
2022-03-03 03:46:33 +00:00
public const int headerSize = sizeof(uint) + sizeof(uint) + 1;
public int size => headerSize + data.Length;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
// header
public int id;
public int fragment;
public bool moreFragments;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
// body
public byte[] data;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public byte[] ToBytes()
{
var array = new byte[size];
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
// Copy id
array[0] = (byte)id;
array[1] = (byte)(id >> 8);
array[2] = (byte)(id >> 0x10);
array[3] = (byte)(id >> 0x18);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
// Copy fragment
array[4] = (byte)fragment;
array[5] = (byte)(fragment >> 8);
array[6] = (byte)(fragment >> 0x10);
array[7] = (byte)(fragment >> 0x18);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
array[8] = moreFragments ? (byte)1 : (byte)0;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
Array.Copy(data, 0, array, 9, data.Length);
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
return array;
}
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
public void FromBytes(byte[] array)
{
id = BitConverter.ToInt32(array, 0);
fragment = BitConverter.ToInt32(array, 4);
moreFragments = array[8] == 1;
2022-02-06 04:17:08 +00:00
2022-03-03 03:46:33 +00:00
data = new byte[array.Length - 9];
Array.Copy(array, 9, data, 0, data.Length);
2022-02-06 04:17:08 +00:00
}
}