ENet.cs 26 KB

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