1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
using System.Buffers.Binary;
using System.Net.Sockets;
namespace OnekoOnline.Net;
static class NetBase
{
public static async Task<Packet> GetReliableData(NetworkStream stream, CancellationToken token = default)
{
byte[] infoBytes = new byte[PacketInfo.SizeOf];
await stream.ReadExactlyAsync(infoBytes, token);
PacketInfo info = PacketInfo.Deserialize(infoBytes);
if (info.Type == PacketType.Ping || info.Type == PacketType.Disconnect) return new(info.Type, [], info.FromId);
byte[] data = new byte[info.DataSize];
await stream.ReadExactlyAsync(data, token);
return new Packet(info.Type, data, info.FromId);
}
public static async Task SendReliableData(Packet packet, NetworkStream stream, CancellationToken token = default)
{
byte[] info = new byte[PacketInfo.SizeOf];
new PacketInfo(packet).Serialize(info);
await stream.WriteAsync(info, token);
await stream.WriteAsync(packet.Data, token);
}
}
public readonly struct Packet(PacketType type, byte[] data, int id)
{
public readonly PacketType Type = type;
public readonly byte[] Data = data;
public readonly int FromId = id;
}
public enum PacketType : byte
{
MousePosition,
OnekoState,
Ping,
OnekoSpritesheet,
Username,
UserId,
Disconnect
}
public struct PacketInfo
{
public readonly int DataSize;
public readonly PacketType Type;
public readonly int FromId;
public const int SizeOf = (sizeof(int)*2) + 1;
public PacketInfo(Packet packet)
{
DataSize = packet.Data.Length;
Type = packet.Type;
FromId = packet.FromId;
}
public PacketInfo(PacketType type, int size, int id)
{
Type = type;
DataSize = size;
FromId = id;
}
public readonly void Serialize(Span<byte> span)
{
span[0] = (byte)Type;
BinaryPrimitives.WriteInt32LittleEndian(span[1..], DataSize);
BinaryPrimitives.WriteInt32LittleEndian(span[(1+sizeof(int))..], FromId);
}
public static PacketInfo Deserialize(ReadOnlySpan<byte> span)
{
PacketType type = (PacketType)span[0];
int size = BinaryPrimitives.ReadInt32LittleEndian(span[1..]);
int id = BinaryPrimitives.ReadInt32LittleEndian(span[(1+sizeof(int))..]);
return new PacketInfo(type, size, id);
}
}
class User(int id)
{
public readonly int Id = id;
//Oneko Stuff
public byte[]? SpriteSheet;
public string? Username;
public bool ExchangedData => SpriteSheet != null && Username != null;
}
|