-
Notifications
You must be signed in to change notification settings - Fork 5
/
LibENet.cs
272 lines (219 loc) · 13.5 KB
/
LibENet.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
using System;
using System.Runtime.InteropServices;
using System.IO;
using Native = ENet.Managed.Structures;
using ENet.Managed.Platforms;
namespace ENet.Managed
{
public static unsafe class LibENet
{
public const CallingConvention ENetCallingConvention = CallingConvention.Cdecl;
public const int ENET_PROTOCOL_MAXIMUM_PACKET_COMMANDS = 32;
public const int ENET_PEER_UNSEQUENCED_WINDOW_SIZE = 1024;
public const int ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xFFF;
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetInitializeDelegate();
public static ENetInitializeDelegate Initialize { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetInitializeWithCallbacksDelegate(uint version, Native.ENetCallbacks* callbacks);
public static ENetInitializeWithCallbacksDelegate InitializeWithCallbacks { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetDeinitializeDelegate();
public static ENetDeinitializeDelegate Deinitialize { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate uint ENetLinkedVersionDelegate();
public static ENetLinkedVersionDelegate LinkedVersion { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetAddressGetHost(Native.ENetAddress* address, IntPtr hostName, UIntPtr nameLength);
public static ENetAddressGetHost AddressGetHost { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetAddressGetHostIP(Native.ENetAddress* address, IntPtr hostName, UIntPtr nameLength);
public static ENetAddressGetHostIP AddressGetHostIP { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetAddressSetHost(Native.ENetAddress* address, [MarshalAs(UnmanagedType.LPStr)] string hostName);
public static ENetAddressSetHost AddressSetHost { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetHostBandwidthLimitDelegate(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
public static ENetHostBandwidthLimitDelegate HostBandwidthLimit { get; private set; }
//[UnmanagedFunctionPointer(ENetCallingConvention)]
//public delegate void ENetHostBandwidthThrottleDelegate(IntPtr host);
//public static ENetHostBandwidthThrottleDelegate HostBandwidthThrottle { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetHostBroadcastDelegate(IntPtr host, byte channel, Native.ENetPacket* packet);
public static ENetHostBroadcastDelegate HostBroadcast { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetHostChannelLimitDelegate(IntPtr host, UIntPtr channelLimit);
public static ENetHostChannelLimitDelegate HostChannelLimit { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public unsafe delegate void ENetHostCheckEventsDelegate(IntPtr host, Native.ENetEvent* e);
public static ENetHostCheckEventsDelegate HostCheckEvents { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetHostCompressDelegate(IntPtr host, Native.ENetCompressor* compressor);
public static ENetHostCompressDelegate HostCompress { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetHostCompressWithRangeCoderDelegate(IntPtr host);
public static ENetHostCompressWithRangeCoderDelegate HostCompressWithRangeCoder { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate Native.ENetPeer* ENetHostConnectDelegate(IntPtr host, Native.ENetAddress* address, UIntPtr channelCount, uint data);
public static ENetHostConnectDelegate HostConnect { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate IntPtr ENetHostCreateDelegate(Native.ENetAddress* address, UIntPtr peerCount, UIntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
public static ENetHostCreateDelegate HostCreate { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetHostDestroyDelegate(IntPtr host);
public static ENetHostDestroyDelegate HostDestroy { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetHostFlushDelegate(IntPtr host);
public static ENetHostFlushDelegate HostFlush { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetHostServiceDelegate(IntPtr host, Native.ENetEvent* e, uint timeout);
public static ENetHostServiceDelegate HostService { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetCrc32Delegate(Native.ENetBuffer* buffers, UIntPtr buffersCount);
public static ENetCrc32Delegate Crc32 { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate Native.ENetPacket* ENetPacketCreateDelegate(IntPtr data, UIntPtr dataLength, ENetPacketFlags flags);
public static ENetPacketCreateDelegate PacketCreate { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPacketDestroyDelegate(Native.ENetPacket* packet);
public static ENetPacketDestroyDelegate PacketDestroy;
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetPacketResizeDelegate(Native.ENetPacket* packet, UIntPtr dataLength);
public static ENetPacketResizeDelegate PacketResize { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPeerDisconnectDelegate(Native.ENetPeer* peer, uint data);
public static ENetPeerDisconnectDelegate PeerDisconnect { get; private set; }
public static ENetPeerDisconnectDelegate PeerDisconnectNow { get; private set; }
public static ENetPeerDisconnectDelegate PeerDisconnectLater { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPeerPingDelegate(Native.ENetPeer* peer);
public static ENetPeerPingDelegate PeerPing { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPeerPingIntervalDelegate(Native.ENetPeer* peer, uint pingInterval);
public static ENetPeerPingIntervalDelegate PeerPingInterval { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate Native.ENetPacket* ENetPeerReceiveDelegate(Native.ENetPeer* peer, byte* channelID);
public static ENetPeerReceiveDelegate PeerReceive { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPeerResetDelegate(Native.ENetPeer* peer);
public static ENetPeerResetDelegate PeerReset { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate int ENetPeerSendDelegate(Native.ENetPeer* peer, byte channelID, Native.ENetPacket* packet);
public static ENetPeerSendDelegate PeerSend { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPeerThrottleConfigureDelegate(Native.ENetPeer* peer, uint interval, uint acceleration, uint deceleration);
public static ENetPeerThrottleConfigureDelegate PeerThrottleConfigure { get; private set; }
[UnmanagedFunctionPointer(ENetCallingConvention)]
public delegate void ENetPeerTimeoutDelegate(Native.ENetPeer* peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
public static ENetPeerTimeoutDelegate PeerTimeout { get; private set; }
public static string DllPath { get; set; }
public static IntPtr DllHandle { get; private set; } = IntPtr.Zero;
public static bool IsLoaded { get; private set; } = false;
static LibENet()
{
var dllName = Platform.Current.GetENetBinaryName();
DllPath = Path.Combine(Path.GetTempPath(), "enet_managed_resource", dllName);
}
public static bool TryDelete()
{
if (IsLoaded) return false;
try
{
File.Delete(DllPath);
return true;
}
catch
{
return false;
}
}
public static void Unload()
{
if (!IsLoaded) return;
Deinitialize();
Platform.Current.FreeLibrary(DllHandle);
DllHandle = IntPtr.Zero;
IsLoaded = false;
}
public static void Load()
{
if (IsLoaded) return;
try
{
Load(false);
}
catch
{
Load(true);
}
IsLoaded = true;
}
public static T GetProc<T>(string procName)
{
return (T)(object)Marshal.GetDelegateForFunctionPointer(GetProc(procName), typeof(T));
}
public static IntPtr GetProc(string procName)
{
if (DllHandle == IntPtr.Zero)
throw new Exception("ENet library is not loaded.");
IntPtr address = Platform.Current.GetProcAddress(DllHandle, procName);
if (address == IntPtr.Zero)
{
throw new DllNotFoundException(string.Format("Function {0} didn't found within ENet library.", procName));
}
return address;
}
static void Load(bool overwrite)
{
LoadDll(overwrite);
Initialize = GetProc<ENetInitializeDelegate>("enet_initialize");
InitializeWithCallbacks = GetProc<ENetInitializeWithCallbacksDelegate>("enet_initialize_with_callbacks");
Deinitialize = GetProc<ENetDeinitializeDelegate>("enet_deinitialize");
LinkedVersion = GetProc<ENetLinkedVersionDelegate>("enet_linked_version");
AddressGetHost = GetProc<ENetAddressGetHost>("enet_address_get_host");
AddressGetHostIP = GetProc<ENetAddressGetHostIP>("enet_address_get_host_ip");
AddressSetHost = GetProc<ENetAddressSetHost>("enet_address_set_host");
HostBandwidthLimit = GetProc<ENetHostBandwidthLimitDelegate>("enet_host_bandwidth_limit");
//HostBandwidthThrottle = GetProc<ENetHostBandwidthThrottleDelegate>("enet_host_bandwidth_throttle");
HostBroadcast = GetProc<ENetHostBroadcastDelegate>("enet_host_broadcast");
HostChannelLimit = GetProc<ENetHostChannelLimitDelegate>("enet_host_channel_limit");
HostCheckEvents = GetProc<ENetHostCheckEventsDelegate>("enet_host_check_events");
HostCompress = GetProc<ENetHostCompressDelegate>("enet_host_compress");
HostCompressWithRangeCoder = GetProc<ENetHostCompressWithRangeCoderDelegate>("enet_host_compress_with_range_coder");
HostConnect = GetProc<ENetHostConnectDelegate>("enet_host_connect");
HostCreate = GetProc<ENetHostCreateDelegate>("enet_host_create");
HostDestroy = GetProc<ENetHostDestroyDelegate>("enet_host_destroy");
HostFlush = GetProc<ENetHostFlushDelegate>("enet_host_flush");
HostService = GetProc<ENetHostServiceDelegate>("enet_host_service");
Crc32 = GetProc<ENetCrc32Delegate>("enet_crc32");
PacketCreate = GetProc<ENetPacketCreateDelegate>("enet_packet_create");
PacketDestroy = GetProc<ENetPacketDestroyDelegate>("enet_packet_destroy");
PacketResize = GetProc<ENetPacketResizeDelegate>("enet_packet_resize");
PeerDisconnect = GetProc<ENetPeerDisconnectDelegate>("enet_peer_disconnect");
PeerDisconnectLater = GetProc<ENetPeerDisconnectDelegate>("enet_peer_disconnect_later");
PeerDisconnectNow = GetProc<ENetPeerDisconnectDelegate>("enet_peer_disconnect_now");
PeerPing = GetProc<ENetPeerPingDelegate>("enet_peer_ping");
PeerPingInterval = GetProc<ENetPeerPingIntervalDelegate>("enet_peer_ping_interval");
PeerReceive = GetProc<ENetPeerReceiveDelegate>("enet_peer_receive");
PeerReset = GetProc<ENetPeerResetDelegate>("enet_peer_reset");
PeerSend = GetProc<ENetPeerSendDelegate>("enet_peer_send");
PeerThrottleConfigure = GetProc<ENetPeerThrottleConfigureDelegate>("enet_peer_throttle_configure");
PeerTimeout = GetProc<ENetPeerTimeoutDelegate>("enet_peer_timeout");
}
static void LoadDll(bool overwrite)
{
if (DllHandle != IntPtr.Zero)
Platform.Current.FreeLibrary(DllHandle);
var dllBytes = Environment.Is64BitProcess ?
ENetBinariesResource.libenet_64 :
ENetBinariesResource.libenet_32;
if (!File.Exists(DllPath) || overwrite)
{
Directory.CreateDirectory(Path.GetDirectoryName(DllPath));
File.WriteAllBytes(DllPath, dllBytes);
}
DllHandle = Platform.Current.LoadLibrary(DllPath);
if (DllHandle == IntPtr.Zero) throw new Exception("Failed to load ENet library.");
}
}
}