ENet.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * Managed C# wrapper for an extended version of ENet
  3. * Copyright (c) 2013 James Bellinger
  4. * Copyright (c) 2016 Nate Shoffner
  5. * Copyright (c) 2018 Stanislav Denisov
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. using System;
  26. using System.Runtime.InteropServices;
  27. using System.Security;
  28. using System.Text;
  29. namespace ENet {
  30. [Flags]
  31. public enum PacketFlags {
  32. None = 0,
  33. Reliable = 1 << 0,
  34. Unsequenced = 1 << 1,
  35. NoAllocate = 1 << 2,
  36. UnreliableFragment = 1 << 3
  37. }
  38. public enum EventType {
  39. None = 0,
  40. Connect = 1,
  41. Disconnect = 2,
  42. Receive = 3,
  43. Timeout = 4
  44. }
  45. public enum PeerState {
  46. Uninitialized = -1,
  47. Disconnected = 0,
  48. Connecting = 1,
  49. AcknowledgingConnect = 2,
  50. ConnectionPending = 3,
  51. ConnectionSucceeded = 4,
  52. Connected = 5,
  53. DisconnectLater = 6,
  54. Disconnecting = 7,
  55. AcknowledgingDisconnect = 8,
  56. Zombie = 9
  57. }
  58. [StructLayout(LayoutKind.Sequential)]
  59. public struct ENetAddress {
  60. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
  61. public byte[] host;
  62. public ushort port;
  63. public ushort sin6_scope_id;
  64. }
  65. [StructLayout(LayoutKind.Sequential)]
  66. public struct ENetEvent {
  67. public EventType type;
  68. public IntPtr peer;
  69. public byte channelID;
  70. public uint data;
  71. public IntPtr packet;
  72. }
  73. [StructLayout(LayoutKind.Sequential)]
  74. public struct ENetCallbacks {
  75. public IntPtr malloc;
  76. public IntPtr free;
  77. public IntPtr no_memory;
  78. }
  79. public delegate IntPtr AllocCallback(IntPtr size);
  80. public delegate void FreeCallback(IntPtr memory);
  81. public delegate void OutOfMemoryCallback();
  82. internal static class ArrayPool {
  83. [ThreadStatic]
  84. private static byte[] buffer;
  85. public static byte[] GetBuffer() {
  86. if (buffer == null)
  87. buffer = new byte[16];
  88. return buffer;
  89. }
  90. }
  91. public struct Address {
  92. private ENetAddress nativeAddress;
  93. internal ENetAddress NativeData {
  94. get {
  95. return nativeAddress;
  96. }
  97. set {
  98. nativeAddress = value;
  99. }
  100. }
  101. public Address(ENetAddress address) {
  102. nativeAddress = address;
  103. }
  104. public ushort Port {
  105. get {
  106. return nativeAddress.port;
  107. }
  108. set {
  109. nativeAddress.port = value;
  110. }
  111. }
  112. public bool SetHost(string hostName) {
  113. if (hostName == null)
  114. throw new ArgumentNullException("hostName");
  115. return Native.enet_address_set_host(ref nativeAddress, Encoding.ASCII.GetBytes(hostName)) == 0;
  116. }
  117. }
  118. public struct Event {
  119. private ENetEvent nativeEvent;
  120. internal ENetEvent NativeData {
  121. get {
  122. return nativeEvent;
  123. }
  124. set {
  125. nativeEvent = value;
  126. }
  127. }
  128. public Event(ENetEvent @event) {
  129. nativeEvent = @event;
  130. }
  131. public EventType Type {
  132. get {
  133. return nativeEvent.type;
  134. }
  135. }
  136. public Peer Peer {
  137. get {
  138. return new Peer(nativeEvent.peer);
  139. }
  140. }
  141. public byte ChannelID {
  142. get {
  143. return nativeEvent.channelID;
  144. }
  145. }
  146. public uint Data {
  147. get {
  148. return nativeEvent.data;
  149. }
  150. }
  151. public Packet Packet {
  152. get {
  153. return new Packet(nativeEvent.packet);
  154. }
  155. }
  156. }
  157. public class Callbacks {
  158. private ENetCallbacks nativeCallbacks;
  159. internal ENetCallbacks NativeData {
  160. get {
  161. return nativeCallbacks;
  162. }
  163. set {
  164. nativeCallbacks = value;
  165. }
  166. }
  167. public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, OutOfMemoryCallback outOfMemoryCallback) {
  168. nativeCallbacks.malloc = Marshal.GetFunctionPointerForDelegate(allocCallback);
  169. nativeCallbacks.free = Marshal.GetFunctionPointerForDelegate(freeCallback);
  170. nativeCallbacks.no_memory = Marshal.GetFunctionPointerForDelegate(outOfMemoryCallback);
  171. }
  172. }
  173. public struct Packet : IDisposable {
  174. private IntPtr nativePacket;
  175. internal IntPtr NativeData {
  176. get {
  177. return nativePacket;
  178. }
  179. set {
  180. nativePacket = value;
  181. }
  182. }
  183. public Packet(IntPtr packet) {
  184. nativePacket = packet;
  185. }
  186. public void Dispose() {
  187. if (nativePacket != IntPtr.Zero) {
  188. Native.enet_packet_destroy(nativePacket);
  189. nativePacket = IntPtr.Zero;
  190. }
  191. }
  192. public bool IsSet {
  193. get {
  194. return nativePacket != IntPtr.Zero;
  195. }
  196. }
  197. public IntPtr Data {
  198. get {
  199. CheckCreated();
  200. return Native.enet_packet_get_data(nativePacket);
  201. }
  202. }
  203. public int Length {
  204. get {
  205. CheckCreated();
  206. return Native.enet_packet_get_length(nativePacket);
  207. }
  208. }
  209. internal void CheckCreated() {
  210. if (nativePacket == IntPtr.Zero)
  211. throw new InvalidOperationException("Packet not created");
  212. }
  213. public void Create(byte[] data) {
  214. if (data == null)
  215. throw new ArgumentNullException("data");
  216. Create(data, data.Length);
  217. }
  218. public void Create(byte[] data, int length) {
  219. Create(data, length, PacketFlags.None);
  220. }
  221. public void Create(byte[] data, int length, PacketFlags flags) {
  222. if (data == null)
  223. throw new ArgumentNullException("data");
  224. if (length < 0 || length > data.Length)
  225. throw new ArgumentOutOfRangeException();
  226. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  227. }
  228. public void CopyTo(byte[] destination) {
  229. if (destination == null)
  230. throw new ArgumentNullException("destination");
  231. Marshal.Copy(Data, destination, 0, Length);
  232. }
  233. }
  234. public class Host : IDisposable {
  235. private IntPtr nativeHost;
  236. internal IntPtr NativeData {
  237. get {
  238. return nativeHost;
  239. }
  240. set {
  241. nativeHost = value;
  242. }
  243. }
  244. public void Dispose() {
  245. Dispose(true);
  246. GC.SuppressFinalize(this);
  247. }
  248. protected virtual void Dispose(bool disposing) {
  249. if (nativeHost != IntPtr.Zero) {
  250. Native.enet_host_destroy(nativeHost);
  251. nativeHost = IntPtr.Zero;
  252. }
  253. }
  254. ~Host() {
  255. Dispose(false);
  256. }
  257. public bool IsSet {
  258. get {
  259. return nativeHost != IntPtr.Zero;
  260. }
  261. }
  262. public uint PeersCount {
  263. get {
  264. CheckCreated();
  265. return Native.enet_host_get_peers_count(nativeHost);
  266. }
  267. }
  268. public uint PacketsSent {
  269. get {
  270. CheckCreated();
  271. return Native.enet_host_get_packets_sent(nativeHost);
  272. }
  273. }
  274. public uint PacketsReceived {
  275. get {
  276. CheckCreated();
  277. return Native.enet_host_get_packets_received(nativeHost);
  278. }
  279. }
  280. public uint BytesSent {
  281. get {
  282. CheckCreated();
  283. return Native.enet_host_get_bytes_sent(nativeHost);
  284. }
  285. }
  286. public uint BytesReceived {
  287. get {
  288. CheckCreated();
  289. return Native.enet_host_get_bytes_received(nativeHost);
  290. }
  291. }
  292. internal void CheckCreated() {
  293. if (nativeHost == IntPtr.Zero)
  294. throw new InvalidOperationException("Host not created");
  295. }
  296. private void CheckChannelLimit(int channelLimit) {
  297. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  298. throw new ArgumentOutOfRangeException("channelLimit");
  299. }
  300. public void Create() {
  301. Create(null, 1, 0);
  302. }
  303. public void Create(Address? address, int peerLimit) {
  304. Create(address, peerLimit, 0);
  305. }
  306. public void Create(Address? address, int peerLimit, int channelLimit) {
  307. Create(address, peerLimit, channelLimit, 0, 0);
  308. }
  309. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  310. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  311. }
  312. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  313. if (nativeHost != IntPtr.Zero)
  314. throw new InvalidOperationException("Host already created");
  315. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  316. throw new ArgumentOutOfRangeException("peerLimit");
  317. CheckChannelLimit(channelLimit);
  318. if (address != null) {
  319. var nativeAddress = address.Value.NativeData;
  320. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  321. } else {
  322. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  323. }
  324. if (nativeHost == IntPtr.Zero)
  325. throw new ENetException(0, "Host creation call failed");
  326. }
  327. public void Broadcast(byte channelID, ref Packet packet) {
  328. CheckCreated();
  329. packet.CheckCreated();
  330. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  331. packet.NativeData = IntPtr.Zero;
  332. }
  333. public int CheckEvents(out Event @event) {
  334. CheckCreated();
  335. ENetEvent nativeEvent;
  336. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  337. if (result <= 0) {
  338. @event = new Event();
  339. return result;
  340. }
  341. @event = new Event(nativeEvent);
  342. return result;
  343. }
  344. public Peer Connect(Address address) {
  345. return Connect(address, 0, 0);
  346. }
  347. public Peer Connect(Address address, int channelLimit) {
  348. return Connect(address, channelLimit, 0);
  349. }
  350. public Peer Connect(Address address, int channelLimit, uint data) {
  351. CheckCreated();
  352. CheckChannelLimit(channelLimit);
  353. var nativeAddress = address.NativeData;
  354. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  355. if (peer.NativeData == IntPtr.Zero)
  356. throw new ENetException(0, "Host connect call failed");
  357. return peer;
  358. }
  359. public int Service(int timeout, out Event @event) {
  360. if (timeout < 0)
  361. throw new ArgumentOutOfRangeException("timeout");
  362. CheckCreated();
  363. ENetEvent nativeEvent;
  364. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  365. if (result <= 0) {
  366. @event = new Event();
  367. return result;
  368. }
  369. @event = new Event(nativeEvent);
  370. return result;
  371. }
  372. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  373. CheckCreated();
  374. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  375. }
  376. public void SetChannelLimit(int channelLimit) {
  377. CheckCreated();
  378. CheckChannelLimit(channelLimit);
  379. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  380. }
  381. public void Flush() {
  382. CheckCreated();
  383. Native.enet_host_flush(nativeHost);
  384. }
  385. }
  386. public struct Peer {
  387. private IntPtr nativePeer;
  388. internal IntPtr NativeData {
  389. get {
  390. return nativePeer;
  391. }
  392. set {
  393. nativePeer = value;
  394. }
  395. }
  396. public Peer(IntPtr peer) {
  397. nativePeer = peer;
  398. }
  399. public bool IsSet {
  400. get {
  401. return nativePeer != IntPtr.Zero;
  402. }
  403. }
  404. public uint ID {
  405. get {
  406. CheckCreated();
  407. return Native.enet_peer_get_id(nativePeer);
  408. }
  409. }
  410. public string IP {
  411. get {
  412. CheckCreated();
  413. byte[] ip = ArrayPool.GetBuffer();
  414. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
  415. if (Encoding.ASCII.GetString(ip).Remove(7) == "::ffff:")
  416. return Encoding.ASCII.GetString(ip).Substring(7);
  417. return Encoding.ASCII.GetString(ip);
  418. } else {
  419. return String.Empty;
  420. }
  421. }
  422. }
  423. public ushort Port {
  424. get {
  425. CheckCreated();
  426. return Native.enet_peer_get_port(nativePeer);
  427. }
  428. }
  429. public PeerState State {
  430. get {
  431. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  432. }
  433. }
  434. public uint RoundTripTime {
  435. get {
  436. CheckCreated();
  437. return Native.enet_peer_get_rtt(nativePeer);
  438. }
  439. }
  440. public ulong PacketsSent {
  441. get {
  442. CheckCreated();
  443. return Native.enet_peer_get_packets_sent(nativePeer);
  444. }
  445. }
  446. public uint PacketsLost {
  447. get {
  448. CheckCreated();
  449. return Native.enet_peer_get_packets_lost(nativePeer);
  450. }
  451. }
  452. public ulong BytesSent {
  453. get {
  454. CheckCreated();
  455. return Native.enet_peer_get_bytes_sent(nativePeer);
  456. }
  457. }
  458. public ulong BytesReceived {
  459. get {
  460. CheckCreated();
  461. return Native.enet_peer_get_bytes_received(nativePeer);
  462. }
  463. }
  464. public IntPtr Data {
  465. get {
  466. CheckCreated();
  467. return Native.enet_peer_get_data(nativePeer);
  468. }
  469. set {
  470. CheckCreated();
  471. Native.enet_peer_set_data(nativePeer, value);
  472. }
  473. }
  474. internal void CheckCreated() {
  475. if (nativePeer == IntPtr.Zero)
  476. throw new InvalidOperationException("Peer not created");
  477. }
  478. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration) {
  479. CheckCreated();
  480. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration);
  481. }
  482. public bool Send(byte channelID, ref Packet packet) {
  483. CheckCreated();
  484. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) >= 0;
  485. }
  486. public void Ping() {
  487. CheckCreated();
  488. Native.enet_peer_ping(nativePeer);
  489. }
  490. public void PingInterval(uint interval) {
  491. CheckCreated();
  492. Native.enet_peer_ping_interval(nativePeer, interval);
  493. }
  494. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  495. CheckCreated();
  496. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  497. }
  498. public void Disconnect(uint data) {
  499. CheckCreated();
  500. Native.enet_peer_disconnect(nativePeer, data);
  501. }
  502. public void DisconnectNow(uint data) {
  503. CheckCreated();
  504. Native.enet_peer_disconnect_now(nativePeer, data);
  505. }
  506. public void DisconnectLater(uint data) {
  507. CheckCreated();
  508. Native.enet_peer_disconnect_later(nativePeer, data);
  509. }
  510. public void Reset() {
  511. CheckCreated();
  512. Native.enet_peer_reset(nativePeer);
  513. }
  514. }
  515. public class ENetException : Exception {
  516. public ENetException(int code, string message) : base(message) {
  517. Code = code;
  518. }
  519. public int Code {
  520. get; private set;
  521. }
  522. }
  523. public static class Library {
  524. public const uint maxChannelCount = 0xFF;
  525. public const uint maxPeers = 0xFFF;
  526. public const uint throttleScale = 32;
  527. public const uint throttleAcceleration = 2;
  528. public const uint throttleDeceleration = 2;
  529. public const uint throttleInterval = 5000;
  530. public const uint timeoutLimit = 32;
  531. public const uint timeoutMinimum = 5000;
  532. public const uint timeoutMaximum = 30000;
  533. public const uint version = (2 << 16) | (0 << 8) | (7);
  534. public static int Initialize() {
  535. return Native.enet_initialize();
  536. }
  537. public static int Initialize(Callbacks inits) {
  538. var nativeCallbacks = inits.NativeData;
  539. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks);
  540. }
  541. public static void Deinitialize() {
  542. Native.enet_deinitialize();
  543. }
  544. public static uint Time {
  545. get {
  546. return Native.enet_time_get();
  547. }
  548. }
  549. }
  550. [SuppressUnmanagedCodeSecurity]
  551. internal static class Native {
  552. #if __IOS__ || UNITY_IOS
  553. private const string nativeLibrary = "__Internal";
  554. #else
  555. private const string nativeLibrary = "enet";
  556. #endif
  557. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  558. internal static extern int enet_initialize();
  559. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  560. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  561. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  562. internal static extern void enet_deinitialize();
  563. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  564. internal static extern uint enet_time_get();
  565. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  566. internal static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
  567. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  568. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  569. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  570. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  571. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  572. internal static extern int enet_packet_get_length(IntPtr packet);
  573. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  574. internal static extern void enet_packet_destroy(IntPtr packet);
  575. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  576. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  577. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  578. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  579. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  580. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  581. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  582. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  583. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  584. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  585. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  586. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  587. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  588. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  589. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  590. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  591. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  592. internal static extern uint enet_host_get_peers_count(IntPtr host);
  593. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  594. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  595. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  596. internal static extern uint enet_host_get_packets_received(IntPtr host);
  597. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  598. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  599. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  600. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  601. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  602. internal static extern void enet_host_flush(IntPtr host);
  603. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  604. internal static extern void enet_host_destroy(IntPtr host);
  605. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  606. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration);
  607. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  608. internal static extern uint enet_peer_get_id(IntPtr peer);
  609. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  610. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  611. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  612. internal static extern ushort enet_peer_get_port(IntPtr peer);
  613. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  614. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  615. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  616. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  617. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  618. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  619. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  620. internal static extern uint enet_peer_get_packets_lost(IntPtr peer);
  621. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  622. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  623. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  624. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  625. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  626. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  627. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  628. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  629. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  630. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  631. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  632. internal static extern void enet_peer_ping(IntPtr peer);
  633. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  634. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  635. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  636. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  637. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  638. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  639. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  640. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  641. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  642. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  643. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  644. internal static extern void enet_peer_reset(IntPtr peer);
  645. }
  646. }