This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetcodeLibrary.cs
More file actions
102 lines (88 loc) · 3.29 KB
/
Copy pathNetcodeLibrary.cs
File metadata and controls
102 lines (88 loc) · 3.29 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
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
96
97
98
99
100
101
102
using System;
using System.Runtime.InteropServices;
namespace netcode.io
{
public static class NetcodeLibrary
{
static NetcodeLibrary()
{
netcodeNATIVE.netcode_init();
SetLogLevel(NetcodeLogLevel.None);
}
internal static void Init()
{
// Ensures static constructor is called at least once.
}
public static void SetLogLevel(NetcodeLogLevel logLevel)
{
netcodeNATIVE.netcode_log_level((int)logLevel);
}
public static byte[] GetRandomBytes(int length)
{
var unmanagedPointer = Marshal.AllocHGlobal(length);
netcodeNATIVE.netcode_random_bytes(unmanagedPointer, length);
var bytes = new byte[length];
Marshal.Copy(unmanagedPointer, bytes, 0, length);
Marshal.FreeHGlobal(unmanagedPointer);
return bytes;
}
public static int GetMaxPacketSize()
{
return netcodeNATIVE.NETCODE_MAX_PACKET_SIZE;
}
public static int GetMaxClients()
{
return netcodeNATIVE.NETCODE_MAX_CLIENTS;
}
public static void Sleep(double seconds)
{
netcodeNATIVE.netcode_sleep(seconds);
}
public static UInt64 GetRandomUInt64()
{
var length = sizeof(UInt64);
var unmanagedPointer = Marshal.AllocHGlobal(length);
netcodeNATIVE.netcode_random_bytes(unmanagedPointer, length);
var bytes = new byte[length];
Marshal.Copy(unmanagedPointer, bytes, 0, length);
Marshal.FreeHGlobal(unmanagedPointer);
return BitConverter.ToUInt64(bytes, 0);
}
public static byte[] GenerateConnectTokenFromPrivateKey(
string[] serverAddresses,
int expirySeconds,
UInt64 clientId,
UInt64 protocolId,
UInt64 sequence,
byte[] privateKey)
{
if (privateKey == null)
{
throw new ArgumentNullException(nameof(privateKey));
}
if (privateKey.Length != netcodeNATIVE.NETCODE_KEY_BYTES)
{
throw new ArgumentException(
$"The private symmetric key must be {netcodeNATIVE.NETCODE_KEY_BYTES} bytes long.",
nameof(privateKey));
}
var unmanagedPrivateKey = Marshal.AllocHGlobal(privateKey.Length);
var unmanagedConnectToken = Marshal.AllocHGlobal(netcodeNATIVE.NETCODE_CONNECT_TOKEN_BYTES);
Marshal.Copy(privateKey, 0, unmanagedPrivateKey, privateKey.Length);
netcodeNATIVE.netcode_generate_connect_token(
serverAddresses.Length,
serverAddresses,
expirySeconds,
clientId,
protocolId,
sequence,
unmanagedPrivateKey,
unmanagedConnectToken);
Marshal.FreeHGlobal(unmanagedPrivateKey);
var connectToken = new byte[netcodeNATIVE.NETCODE_CONNECT_TOKEN_BYTES];
Marshal.Copy(unmanagedConnectToken, connectToken, 0, connectToken.Length);
Marshal.FreeHGlobal(unmanagedConnectToken);
return connectToken;
}
}
}