ENet.cs 27 KB

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