ENet.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. }
  64. [StructLayout(LayoutKind.Sequential)]
  65. public struct ENetEvent {
  66. public EventType type;
  67. public IntPtr peer;
  68. public byte channelID;
  69. public uint data;
  70. public IntPtr packet;
  71. }
  72. [StructLayout(LayoutKind.Sequential)]
  73. public struct ENetCallbacks {
  74. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  75. public delegate IntPtr malloc_cb(IntPtr size);
  76. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  77. public delegate void free_cb(IntPtr memory);
  78. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  79. public delegate void no_memory_cb();
  80. public IntPtr malloc, free, no_memory;
  81. }
  82. internal static class ArrayPool {
  83. [ThreadStatic]
  84. static byte[] buffer = null;
  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 struct Packet : IDisposable {
  158. private IntPtr nativePacket;
  159. internal IntPtr NativeData {
  160. get {
  161. return nativePacket;
  162. }
  163. set {
  164. nativePacket = value;
  165. }
  166. }
  167. public Packet(IntPtr packet) {
  168. nativePacket = packet;
  169. }
  170. public void Dispose() {
  171. if (nativePacket != IntPtr.Zero) {
  172. Native.enet_packet_destroy(nativePacket);
  173. nativePacket = IntPtr.Zero;
  174. }
  175. }
  176. public bool IsSet {
  177. get {
  178. return nativePacket != IntPtr.Zero;
  179. }
  180. }
  181. public IntPtr Data {
  182. get {
  183. CheckCreated();
  184. return Native.enet_packet_get_data(nativePacket);
  185. }
  186. }
  187. public int Length {
  188. get {
  189. CheckCreated();
  190. return Native.enet_packet_get_length(nativePacket);
  191. }
  192. }
  193. internal void CheckCreated() {
  194. if (nativePacket == IntPtr.Zero)
  195. throw new InvalidOperationException("Packet not created");
  196. }
  197. public void Create(byte[] data) {
  198. if (data == null)
  199. throw new ArgumentNullException("data");
  200. Create(data, data.Length);
  201. }
  202. public void Create(byte[] data, int length) {
  203. Create(data, length, PacketFlags.None);
  204. }
  205. public void Create(byte[] data, int length, PacketFlags flags) {
  206. if (data == null)
  207. throw new ArgumentNullException("data");
  208. if (length < 0 || length > data.Length)
  209. throw new ArgumentOutOfRangeException();
  210. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  211. }
  212. public void CopyTo(byte[] destination) {
  213. if (destination == null)
  214. throw new ArgumentNullException("destination");
  215. Marshal.Copy(Data, destination, 0, Length);
  216. }
  217. }
  218. public class Host : IDisposable {
  219. private IntPtr nativeHost;
  220. internal IntPtr NativeData {
  221. get {
  222. return nativeHost;
  223. }
  224. set {
  225. nativeHost = value;
  226. }
  227. }
  228. public void Dispose() {
  229. Dispose(true);
  230. GC.SuppressFinalize(this);
  231. }
  232. protected virtual void Dispose(bool disposing) {
  233. try {
  234. if (nativeHost != IntPtr.Zero) {
  235. Native.enet_host_destroy(nativeHost);
  236. nativeHost = IntPtr.Zero;
  237. }
  238. }
  239. catch { }
  240. }
  241. ~Host() {
  242. Dispose(false);
  243. }
  244. public bool IsSet {
  245. get {
  246. return nativeHost != IntPtr.Zero;
  247. }
  248. }
  249. public uint PeersCount {
  250. get {
  251. CheckCreated();
  252. return Native.enet_host_get_peers_count(nativeHost);
  253. }
  254. }
  255. public uint PacketsSent {
  256. get {
  257. CheckCreated();
  258. return Native.enet_host_get_packets_sent(nativeHost);
  259. }
  260. }
  261. public uint PacketsReceived {
  262. get {
  263. CheckCreated();
  264. return Native.enet_host_get_packets_received(nativeHost);
  265. }
  266. }
  267. public uint BytesSent {
  268. get {
  269. CheckCreated();
  270. return Native.enet_host_get_bytes_sent(nativeHost);
  271. }
  272. }
  273. public uint BytesReceived {
  274. get {
  275. CheckCreated();
  276. return Native.enet_host_get_bytes_received(nativeHost);
  277. }
  278. }
  279. internal void CheckCreated() {
  280. if (nativeHost == IntPtr.Zero)
  281. throw new InvalidOperationException("Host not created");
  282. }
  283. private void CheckChannelLimit(int channelLimit) {
  284. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  285. throw new ArgumentOutOfRangeException("channelLimit");
  286. }
  287. public void Create() {
  288. Create(null, 1, 0);
  289. }
  290. public void Create(Address? address, int peerLimit) {
  291. Create(address, peerLimit, 0);
  292. }
  293. public void Create(Address? address, int peerLimit, int channelLimit) {
  294. Create(address, peerLimit, channelLimit, 0, 0);
  295. }
  296. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  297. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  298. }
  299. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  300. if (nativeHost != IntPtr.Zero)
  301. throw new InvalidOperationException("Host already created");
  302. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  303. throw new ArgumentOutOfRangeException("peerLimit");
  304. CheckChannelLimit(channelLimit);
  305. if (address != null) {
  306. var nativeAddress = address.Value.NativeData;
  307. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  308. } else {
  309. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  310. }
  311. if (nativeHost == IntPtr.Zero)
  312. throw new ENetException(0, "Host creation call failed");
  313. }
  314. public void Broadcast(byte channelID, ref Packet packet) {
  315. CheckCreated();
  316. packet.CheckCreated();
  317. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  318. packet.NativeData = IntPtr.Zero;
  319. }
  320. public int CheckEvents(out Event @event) {
  321. CheckCreated();
  322. ENetEvent nativeEvent;
  323. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  324. if (result <= 0) {
  325. @event = new Event();
  326. return result;
  327. }
  328. @event = new Event(nativeEvent);
  329. return result;
  330. }
  331. public Peer Connect(Address address) {
  332. return Connect(address, 0, 0);
  333. }
  334. public Peer Connect(Address address, int channelLimit) {
  335. return Connect(address, channelLimit, 0);
  336. }
  337. public Peer Connect(Address address, int channelLimit, uint data) {
  338. CheckCreated();
  339. CheckChannelLimit(channelLimit);
  340. var nativeAddress = address.NativeData;
  341. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  342. if (peer.NativeData == IntPtr.Zero)
  343. throw new ENetException(0, "Host connect call failed");
  344. return peer;
  345. }
  346. public int Service(int timeout, out Event @event) {
  347. if (timeout < 0)
  348. throw new ArgumentOutOfRangeException("timeout");
  349. CheckCreated();
  350. ENetEvent nativeEvent;
  351. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  352. if (result <= 0) {
  353. @event = new Event();
  354. return result;
  355. }
  356. @event = new Event(nativeEvent);
  357. return result;
  358. }
  359. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  360. CheckCreated();
  361. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  362. }
  363. public void SetChannelLimit(int channelLimit) {
  364. CheckCreated();
  365. CheckChannelLimit(channelLimit);
  366. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  367. }
  368. public void Flush() {
  369. CheckCreated();
  370. Native.enet_host_flush(nativeHost);
  371. }
  372. }
  373. public struct Peer {
  374. private IntPtr nativePeer;
  375. internal IntPtr NativeData {
  376. get {
  377. return nativePeer;
  378. }
  379. set {
  380. nativePeer = value;
  381. }
  382. }
  383. public Peer(IntPtr peer) {
  384. nativePeer = peer;
  385. }
  386. public bool IsSet {
  387. get {
  388. return nativePeer != IntPtr.Zero;
  389. }
  390. }
  391. public uint ID {
  392. get {
  393. CheckCreated();
  394. return Native.enet_peer_get_id(nativePeer);
  395. }
  396. }
  397. public string IP {
  398. get {
  399. CheckCreated();
  400. byte[] ip = ArrayPool.GetBuffer();
  401. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
  402. if (Encoding.ASCII.GetString(ip).Remove(7) == "::ffff:")
  403. return Encoding.ASCII.GetString(ip).Substring(7);
  404. return Encoding.ASCII.GetString(ip);
  405. } else {
  406. return String.Empty;
  407. }
  408. }
  409. }
  410. public ushort Port {
  411. get {
  412. CheckCreated();
  413. return Native.enet_peer_get_port(nativePeer);
  414. }
  415. }
  416. public PeerState State {
  417. get {
  418. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  419. }
  420. }
  421. public uint RoundTripTime {
  422. get {
  423. CheckCreated();
  424. return Native.enet_peer_get_rtt(nativePeer);
  425. }
  426. }
  427. public ulong PacketsSent {
  428. get {
  429. CheckCreated();
  430. return Native.enet_peer_get_packets_sent(nativePeer);
  431. }
  432. }
  433. public uint PacketsLost {
  434. get {
  435. CheckCreated();
  436. return Native.enet_peer_get_packets_lost(nativePeer);
  437. }
  438. }
  439. public ulong BytesSent {
  440. get {
  441. CheckCreated();
  442. return Native.enet_peer_get_bytes_sent(nativePeer);
  443. }
  444. }
  445. public ulong BytesReceived {
  446. get {
  447. CheckCreated();
  448. return Native.enet_peer_get_bytes_received(nativePeer);
  449. }
  450. }
  451. public IntPtr Data {
  452. get {
  453. CheckCreated();
  454. return Native.enet_peer_get_data(nativePeer);
  455. }
  456. set {
  457. CheckCreated();
  458. Native.enet_peer_set_data(nativePeer, value);
  459. }
  460. }
  461. internal void CheckCreated() {
  462. if (nativePeer == IntPtr.Zero)
  463. throw new InvalidOperationException("Peer not created");
  464. }
  465. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration) {
  466. CheckCreated();
  467. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration);
  468. }
  469. public bool Send(byte channelID, ref Packet packet) {
  470. CheckCreated();
  471. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) >= 0;
  472. }
  473. public void Ping() {
  474. CheckCreated();
  475. Native.enet_peer_ping(nativePeer);
  476. }
  477. public void PingInterval(uint interval) {
  478. CheckCreated();
  479. Native.enet_peer_ping_interval(nativePeer, interval);
  480. }
  481. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  482. CheckCreated();
  483. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  484. }
  485. public void Disconnect(uint data) {
  486. CheckCreated();
  487. Native.enet_peer_disconnect(nativePeer, data);
  488. }
  489. public void DisconnectNow(uint data) {
  490. CheckCreated();
  491. Native.enet_peer_disconnect_now(nativePeer, data);
  492. }
  493. public void DisconnectLater(uint data) {
  494. CheckCreated();
  495. Native.enet_peer_disconnect_later(nativePeer, data);
  496. }
  497. public void Reset() {
  498. CheckCreated();
  499. Native.enet_peer_reset(nativePeer);
  500. }
  501. }
  502. public class ENetException : Exception {
  503. public ENetException(int code, string message) : base(message) {
  504. Code = code;
  505. }
  506. public int Code {
  507. get; private set;
  508. }
  509. }
  510. public static class Library {
  511. public const uint maxChannelCount = 0xFF;
  512. public const uint maxPeers = 0xFFF;
  513. public const uint throttleScale = 32;
  514. public const uint throttleAcceleration = 2;
  515. public const uint throttleDeceleration = 2;
  516. public const uint throttleInterval = 5000;
  517. public const uint timeoutLimit = 32;
  518. public const uint timeoutMinimum = 5000;
  519. public const uint timeoutMaximum = 30000;
  520. public const uint version = (2 << 16) | (0 << 8) | (6);
  521. public static int Initialize() {
  522. return Native.enet_initialize();
  523. }
  524. public static void Deinitialize() {
  525. Native.enet_deinitialize();
  526. }
  527. public static uint Time {
  528. get {
  529. return Native.enet_time_get();
  530. }
  531. }
  532. }
  533. [SuppressUnmanagedCodeSecurity]
  534. internal static class Native {
  535. private const string nativeLibrary = "enet";
  536. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  537. internal static extern int enet_initialize();
  538. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  539. internal static extern void enet_deinitialize();
  540. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  541. internal static extern uint enet_time_get();
  542. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  543. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  544. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  545. internal static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
  546. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  547. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  548. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  549. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  550. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  551. internal static extern int enet_packet_get_length(IntPtr packet);
  552. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  553. internal static extern void enet_packet_destroy(IntPtr packet);
  554. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  555. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  556. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  557. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  558. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  559. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  560. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  561. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  562. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  563. internal static extern int enet_host_service(IntPtr host, IntPtr @event, uint timeout);
  564. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  565. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  566. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  567. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  568. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  569. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  570. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  571. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  572. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  573. internal static extern uint enet_host_get_peers_count(IntPtr host);
  574. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  575. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  576. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  577. internal static extern uint enet_host_get_packets_received(IntPtr host);
  578. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  579. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  580. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  581. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  582. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  583. internal static extern void enet_host_flush(IntPtr host);
  584. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  585. internal static extern void enet_host_destroy(IntPtr host);
  586. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  587. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration);
  588. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  589. internal static extern uint enet_peer_get_id(IntPtr peer);
  590. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  591. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  592. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  593. internal static extern ushort enet_peer_get_port(IntPtr peer);
  594. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  595. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  596. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  597. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  598. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  599. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  600. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  601. internal static extern uint enet_peer_get_packets_lost(IntPtr peer);
  602. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  603. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  604. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  605. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  606. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  607. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  608. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  609. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  610. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  611. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  612. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  613. internal static extern void enet_peer_ping(IntPtr peer);
  614. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  615. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  616. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  617. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  618. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  619. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  620. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  621. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  622. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  623. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  624. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  625. internal static extern void enet_peer_reset(IntPtr peer);
  626. }
  627. }