ENet.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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 NoMemoryCallback();
  82. public delegate void PacketFreeCallback(Packet packet);
  83. internal static class ArrayPool {
  84. [ThreadStatic]
  85. private static byte[] buffer;
  86. public static byte[] GetBuffer() {
  87. if (buffer == null)
  88. buffer = new byte[64];
  89. return buffer;
  90. }
  91. }
  92. public struct Address {
  93. private ENetAddress nativeAddress;
  94. internal ENetAddress NativeData {
  95. get {
  96. return nativeAddress;
  97. }
  98. set {
  99. nativeAddress = value;
  100. }
  101. }
  102. public Address(ENetAddress address) {
  103. nativeAddress = address;
  104. }
  105. public ushort Port {
  106. get {
  107. return nativeAddress.port;
  108. }
  109. set {
  110. nativeAddress.port = value;
  111. }
  112. }
  113. public bool SetHost(string hostName) {
  114. if (hostName == null)
  115. throw new ArgumentNullException("hostName");
  116. return Native.enet_address_set_host(ref nativeAddress, Encoding.ASCII.GetBytes(hostName)) == 0;
  117. }
  118. }
  119. public struct Event {
  120. private ENetEvent nativeEvent;
  121. internal ENetEvent NativeData {
  122. get {
  123. return nativeEvent;
  124. }
  125. set {
  126. nativeEvent = value;
  127. }
  128. }
  129. public Event(ENetEvent @event) {
  130. nativeEvent = @event;
  131. }
  132. public EventType Type {
  133. get {
  134. return nativeEvent.type;
  135. }
  136. }
  137. public Peer Peer {
  138. get {
  139. return new Peer(nativeEvent.peer);
  140. }
  141. }
  142. public byte ChannelID {
  143. get {
  144. return nativeEvent.channelID;
  145. }
  146. }
  147. public uint Data {
  148. get {
  149. return nativeEvent.data;
  150. }
  151. }
  152. public Packet Packet {
  153. get {
  154. return new Packet(nativeEvent.packet);
  155. }
  156. }
  157. }
  158. public class Callbacks {
  159. private ENetCallbacks nativeCallbacks;
  160. internal ENetCallbacks NativeData {
  161. get {
  162. return nativeCallbacks;
  163. }
  164. set {
  165. nativeCallbacks = value;
  166. }
  167. }
  168. public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, NoMemoryCallback noMemoryCallback) {
  169. nativeCallbacks.malloc = Marshal.GetFunctionPointerForDelegate(allocCallback);
  170. nativeCallbacks.free = Marshal.GetFunctionPointerForDelegate(freeCallback);
  171. nativeCallbacks.no_memory = Marshal.GetFunctionPointerForDelegate(noMemoryCallback);
  172. }
  173. }
  174. public struct Packet : IDisposable {
  175. private IntPtr nativePacket;
  176. internal IntPtr NativeData {
  177. get {
  178. return nativePacket;
  179. }
  180. set {
  181. nativePacket = value;
  182. }
  183. }
  184. public Packet(IntPtr packet) {
  185. nativePacket = packet;
  186. }
  187. public void Dispose() {
  188. if (nativePacket != IntPtr.Zero) {
  189. Native.enet_packet_dispose(nativePacket);
  190. nativePacket = IntPtr.Zero;
  191. }
  192. }
  193. public bool IsSet {
  194. get {
  195. return nativePacket != IntPtr.Zero;
  196. }
  197. }
  198. public IntPtr Data {
  199. get {
  200. CheckCreated();
  201. return Native.enet_packet_get_data(nativePacket);
  202. }
  203. }
  204. public int Length {
  205. get {
  206. CheckCreated();
  207. return Native.enet_packet_get_length(nativePacket);
  208. }
  209. }
  210. internal void CheckCreated() {
  211. if (nativePacket == IntPtr.Zero)
  212. throw new InvalidOperationException("Packet not created");
  213. }
  214. public void SetFreeCallback(PacketFreeCallback callback) {
  215. CheckCreated();
  216. Native.enet_packet_set_free_callback(nativePacket, Marshal.GetFunctionPointerForDelegate(callback));
  217. }
  218. public void Create(byte[] data) {
  219. if (data == null)
  220. throw new ArgumentNullException("data");
  221. Create(data, data.Length);
  222. }
  223. public void Create(byte[] data, int length) {
  224. Create(data, length, PacketFlags.None);
  225. }
  226. public void Create(byte[] data, PacketFlags flags) {
  227. Create(data, data.Length, flags);
  228. }
  229. public void Create(byte[] data, int length, PacketFlags flags) {
  230. if (data == null)
  231. throw new ArgumentNullException("data");
  232. if (length < 0 || length > data.Length)
  233. throw new ArgumentOutOfRangeException();
  234. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  235. }
  236. public void Create(IntPtr data, int length, PacketFlags flags) {
  237. if (data == IntPtr.Zero)
  238. throw new ArgumentNullException("data");
  239. if (length < 0)
  240. throw new ArgumentOutOfRangeException();
  241. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  242. }
  243. public void CopyTo(byte[] destination) {
  244. if (destination == null)
  245. throw new ArgumentNullException("destination");
  246. Marshal.Copy(Data, destination, 0, Length);
  247. }
  248. }
  249. public class Host : IDisposable {
  250. private IntPtr nativeHost;
  251. internal IntPtr NativeData {
  252. get {
  253. return nativeHost;
  254. }
  255. set {
  256. nativeHost = value;
  257. }
  258. }
  259. public void Dispose() {
  260. Dispose(true);
  261. GC.SuppressFinalize(this);
  262. }
  263. protected virtual void Dispose(bool disposing) {
  264. if (nativeHost != IntPtr.Zero) {
  265. Native.enet_host_destroy(nativeHost);
  266. nativeHost = IntPtr.Zero;
  267. }
  268. }
  269. ~Host() {
  270. Dispose(false);
  271. }
  272. public bool IsSet {
  273. get {
  274. return nativeHost != IntPtr.Zero;
  275. }
  276. }
  277. public uint PeersCount {
  278. get {
  279. CheckCreated();
  280. return Native.enet_host_get_peers_count(nativeHost);
  281. }
  282. }
  283. public uint PacketsSent {
  284. get {
  285. CheckCreated();
  286. return Native.enet_host_get_packets_sent(nativeHost);
  287. }
  288. }
  289. public uint PacketsReceived {
  290. get {
  291. CheckCreated();
  292. return Native.enet_host_get_packets_received(nativeHost);
  293. }
  294. }
  295. public uint BytesSent {
  296. get {
  297. CheckCreated();
  298. return Native.enet_host_get_bytes_sent(nativeHost);
  299. }
  300. }
  301. public uint BytesReceived {
  302. get {
  303. CheckCreated();
  304. return Native.enet_host_get_bytes_received(nativeHost);
  305. }
  306. }
  307. internal void CheckCreated() {
  308. if (nativeHost == IntPtr.Zero)
  309. throw new InvalidOperationException("Host not created");
  310. }
  311. private void CheckChannelLimit(int channelLimit) {
  312. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  313. throw new ArgumentOutOfRangeException("channelLimit");
  314. }
  315. public void Create() {
  316. Create(null, 1, 0);
  317. }
  318. public void Create(Address? address, int peerLimit) {
  319. Create(address, peerLimit, 0);
  320. }
  321. public void Create(Address? address, int peerLimit, int channelLimit) {
  322. Create(address, peerLimit, channelLimit, 0, 0);
  323. }
  324. public void Create(int peerLimit, int channelLimit) {
  325. Create(null, peerLimit, channelLimit, 0, 0);
  326. }
  327. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  328. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  329. }
  330. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  331. if (nativeHost != IntPtr.Zero)
  332. throw new InvalidOperationException("Host already created");
  333. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  334. throw new ArgumentOutOfRangeException("peerLimit");
  335. CheckChannelLimit(channelLimit);
  336. if (address != null) {
  337. var nativeAddress = address.Value.NativeData;
  338. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  339. } else {
  340. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  341. }
  342. if (nativeHost == IntPtr.Zero)
  343. throw new InvalidOperationException("Host creation call failed");
  344. }
  345. public void EnableCompression() {
  346. CheckCreated();
  347. Native.enet_host_enable_compression(nativeHost);
  348. }
  349. public void PreventConnections(bool state) {
  350. CheckCreated();
  351. Native.enet_host_prevent_connections(nativeHost, (byte)(state ? 1 : 0));
  352. }
  353. public void Broadcast(byte channelID, ref Packet packet) {
  354. CheckCreated();
  355. packet.CheckCreated();
  356. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  357. packet.NativeData = IntPtr.Zero;
  358. }
  359. public void Broadcast(byte channelID, ref Packet packet, ref Peer[] peers) {
  360. CheckCreated();
  361. packet.CheckCreated();
  362. if (peers.Length > 0)
  363. Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, ref peers, (IntPtr)peers.Length);
  364. packet.NativeData = IntPtr.Zero;
  365. }
  366. public int CheckEvents(out Event @event) {
  367. CheckCreated();
  368. ENetEvent nativeEvent;
  369. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  370. if (result <= 0) {
  371. @event = new Event();
  372. return result;
  373. }
  374. @event = new Event(nativeEvent);
  375. return result;
  376. }
  377. public Peer Connect(Address address) {
  378. return Connect(address, 0, 0);
  379. }
  380. public Peer Connect(Address address, int channelLimit) {
  381. return Connect(address, channelLimit, 0);
  382. }
  383. public Peer Connect(Address address, int channelLimit, uint data) {
  384. CheckCreated();
  385. CheckChannelLimit(channelLimit);
  386. var nativeAddress = address.NativeData;
  387. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  388. if (peer.NativeData == IntPtr.Zero)
  389. throw new InvalidOperationException("Host connect call failed");
  390. return peer;
  391. }
  392. public int Service(int timeout, out Event @event) {
  393. if (timeout < 0)
  394. throw new ArgumentOutOfRangeException("timeout");
  395. CheckCreated();
  396. ENetEvent nativeEvent;
  397. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  398. if (result <= 0) {
  399. @event = new Event();
  400. return result;
  401. }
  402. @event = new Event(nativeEvent);
  403. return result;
  404. }
  405. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  406. CheckCreated();
  407. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  408. }
  409. public void SetChannelLimit(int channelLimit) {
  410. CheckCreated();
  411. CheckChannelLimit(channelLimit);
  412. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  413. }
  414. public void Flush() {
  415. CheckCreated();
  416. Native.enet_host_flush(nativeHost);
  417. }
  418. }
  419. public struct Peer {
  420. private IntPtr nativePeer;
  421. private uint nativeID;
  422. internal IntPtr NativeData {
  423. get {
  424. return nativePeer;
  425. }
  426. set {
  427. nativePeer = value;
  428. }
  429. }
  430. public Peer(IntPtr peer) {
  431. nativePeer = peer;
  432. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  433. }
  434. public bool IsSet {
  435. get {
  436. return nativePeer != IntPtr.Zero;
  437. }
  438. }
  439. public uint ID {
  440. get {
  441. return nativeID;
  442. }
  443. }
  444. public string IP {
  445. get {
  446. CheckCreated();
  447. byte[] ip = ArrayPool.GetBuffer();
  448. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
  449. if (Encoding.ASCII.GetString(ip).Remove(7) != "::ffff:")
  450. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  451. else
  452. return Encoding.ASCII.GetString(ip, 0, ip.StringLength()).Substring(7);
  453. } else {
  454. return String.Empty;
  455. }
  456. }
  457. }
  458. public ushort Port {
  459. get {
  460. CheckCreated();
  461. return Native.enet_peer_get_port(nativePeer);
  462. }
  463. }
  464. public uint MTU {
  465. get {
  466. CheckCreated();
  467. return Native.enet_peer_get_mtu(nativePeer);
  468. }
  469. }
  470. public PeerState State {
  471. get {
  472. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  473. }
  474. }
  475. public uint RoundTripTime {
  476. get {
  477. CheckCreated();
  478. return Native.enet_peer_get_rtt(nativePeer);
  479. }
  480. }
  481. public uint LastSendTime {
  482. get {
  483. CheckCreated();
  484. return Native.enet_peer_get_lastsendtime(nativePeer);
  485. }
  486. }
  487. public uint LastReceiveTime {
  488. get {
  489. CheckCreated();
  490. return Native.enet_peer_get_lastreceivetime(nativePeer);
  491. }
  492. }
  493. public ulong PacketsSent {
  494. get {
  495. CheckCreated();
  496. return Native.enet_peer_get_packets_sent(nativePeer);
  497. }
  498. }
  499. public uint PacketsLost {
  500. get {
  501. CheckCreated();
  502. return Native.enet_peer_get_packets_lost(nativePeer);
  503. }
  504. }
  505. public ulong BytesSent {
  506. get {
  507. CheckCreated();
  508. return Native.enet_peer_get_bytes_sent(nativePeer);
  509. }
  510. }
  511. public ulong BytesReceived {
  512. get {
  513. CheckCreated();
  514. return Native.enet_peer_get_bytes_received(nativePeer);
  515. }
  516. }
  517. public IntPtr Data {
  518. get {
  519. CheckCreated();
  520. return Native.enet_peer_get_data(nativePeer);
  521. }
  522. set {
  523. CheckCreated();
  524. Native.enet_peer_set_data(nativePeer, value);
  525. }
  526. }
  527. internal void CheckCreated() {
  528. if (nativePeer == IntPtr.Zero)
  529. throw new InvalidOperationException("Peer not created");
  530. }
  531. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration) {
  532. CheckCreated();
  533. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration);
  534. }
  535. public bool Send(byte channelID, ref Packet packet) {
  536. CheckCreated();
  537. packet.CheckCreated();
  538. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  539. }
  540. public void Ping() {
  541. CheckCreated();
  542. Native.enet_peer_ping(nativePeer);
  543. }
  544. public void PingInterval(uint interval) {
  545. CheckCreated();
  546. Native.enet_peer_ping_interval(nativePeer, interval);
  547. }
  548. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  549. CheckCreated();
  550. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  551. }
  552. public void Disconnect(uint data) {
  553. CheckCreated();
  554. Native.enet_peer_disconnect(nativePeer, data);
  555. }
  556. public void DisconnectNow(uint data) {
  557. CheckCreated();
  558. Native.enet_peer_disconnect_now(nativePeer, data);
  559. }
  560. public void DisconnectLater(uint data) {
  561. CheckCreated();
  562. Native.enet_peer_disconnect_later(nativePeer, data);
  563. }
  564. public void Reset() {
  565. CheckCreated();
  566. Native.enet_peer_reset(nativePeer);
  567. }
  568. }
  569. public static class Extensions {
  570. public static int StringLength(this byte[] data) {
  571. if (data == null)
  572. throw new ArgumentNullException("data");
  573. int i;
  574. for (i = 0; i < data.Length && data[i] != 0; i++);
  575. return i;
  576. }
  577. }
  578. public static class Library {
  579. public const uint maxChannelCount = 0xFF;
  580. public const uint maxPeers = 0xFFF;
  581. public const uint maxPacketSize = 32 * 1024 * 1024;
  582. public const uint throttleScale = 32;
  583. public const uint throttleAcceleration = 2;
  584. public const uint throttleDeceleration = 2;
  585. public const uint throttleInterval = 5000;
  586. public const uint timeoutLimit = 32;
  587. public const uint timeoutMinimum = 5000;
  588. public const uint timeoutMaximum = 30000;
  589. public const uint version = (2 << 16) | (1 << 8) | (4);
  590. public static bool Initialize() {
  591. return Native.enet_initialize() == 0;
  592. }
  593. public static bool Initialize(Callbacks inits) {
  594. var nativeCallbacks = inits.NativeData;
  595. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
  596. }
  597. public static void Deinitialize() {
  598. Native.enet_deinitialize();
  599. }
  600. public static uint Time {
  601. get {
  602. return Native.enet_time_get();
  603. }
  604. }
  605. }
  606. [SuppressUnmanagedCodeSecurity]
  607. internal static class Native {
  608. #if __IOS__ || UNITY_IOS && !UNITY_EDITOR
  609. private const string nativeLibrary = "__Internal";
  610. #else
  611. private const string nativeLibrary = "enet";
  612. #endif
  613. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  614. internal static extern int enet_initialize();
  615. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  616. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  617. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  618. internal static extern void enet_deinitialize();
  619. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  620. internal static extern uint enet_time_get();
  621. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  622. internal static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
  623. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  624. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  625. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  626. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  627. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  628. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  629. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  630. internal static extern int enet_packet_get_length(IntPtr packet);
  631. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  632. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  633. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  634. internal static extern void enet_packet_dispose(IntPtr packet);
  635. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  636. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  637. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  638. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  639. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  640. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  641. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  642. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  643. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  644. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, ref Peer[] peers, IntPtr peersLength);
  645. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  646. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  647. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  648. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  649. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  650. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  651. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  652. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  653. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  654. internal static extern uint enet_host_get_peers_count(IntPtr host);
  655. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  656. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  657. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  658. internal static extern uint enet_host_get_packets_received(IntPtr host);
  659. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  660. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  661. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  662. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  663. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  664. internal static extern void enet_host_flush(IntPtr host);
  665. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  666. internal static extern void enet_host_destroy(IntPtr host);
  667. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  668. internal static extern void enet_host_enable_compression(IntPtr host);
  669. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  670. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  671. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  672. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration);
  673. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  674. internal static extern uint enet_peer_get_id(IntPtr peer);
  675. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  676. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  677. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  678. internal static extern ushort enet_peer_get_port(IntPtr peer);
  679. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  680. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  681. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  682. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  683. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  684. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  685. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  686. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  687. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  688. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  689. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  690. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  691. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  692. internal static extern uint enet_peer_get_packets_lost(IntPtr peer);
  693. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  694. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  695. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  696. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  697. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  698. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  699. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  700. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  701. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  702. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  703. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  704. internal static extern void enet_peer_ping(IntPtr peer);
  705. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  706. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  707. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  708. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  709. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  710. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  711. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  712. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  713. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  714. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  715. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  716. internal static extern void enet_peer_reset(IntPtr peer);
  717. }
  718. }