ENet.cs 27 KB

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