-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathConnectTokenData.cs
More file actions
58 lines (46 loc) · 1.21 KB
/
ConnectTokenData.cs
File metadata and controls
58 lines (46 loc) · 1.21 KB
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
using System;
using System.Net;
using NetcodeIO.NET.Utils.IO;
namespace NetcodeIO.NET
{
internal enum NetcodeAddressType : byte
{
None = 0,
IPv4 = 1,
IPv6 = 2,
}
internal class ConnectTokenServerEntry
{
private static byte[] tempIPV4 = new byte[4];
private static byte[] tempIPV6 = new byte[16];
public NetcodeAddressType AddressType;
public IPEndPoint Endpoint;
public void ReadData(ByteArrayReaderWriter stream)
{
byte addressVal = stream.ReadByte();
// if address type is not 1 or 2, data is not valid
if (addressVal != 1 && addressVal != 2)
throw new FormatException();
this.AddressType = (NetcodeAddressType)addressVal;
IPAddress ip = null;
if (this.AddressType == NetcodeAddressType.IPv4)
{
stream.ReadBytesIntoBuffer(tempIPV4, 4);
ip = new IPAddress(tempIPV4);
}
else
{
stream.ReadBytesIntoBuffer(tempIPV6, 16);
ip = new IPAddress(tempIPV6);
}
var port = stream.ReadUInt16();
this.Endpoint = new IPEndPoint(ip, port);
}
public void WriteData(ByteArrayReaderWriter stream)
{
stream.Write((byte)this.AddressType);
stream.Write(this.Endpoint.Address.GetAddressBytes());
stream.Write((ushort)this.Endpoint.Port);
}
}
}