ENet.cs 26 KB

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