ENet.cs 26 KB

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