2
0

ENet.cs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Managed C# wrapper for an extended version of ENet
  3. * This is a fork from upstream and is available at http://github.com/SoftwareGuy/ENet-CSharp
  4. *
  5. * Copyright (c) 2019 Matt Coburn (SoftwareGuy/Coburn64), Chris Burns (c6burns)
  6. * Copyright (c) 2013 James Bellinger, 2016 Nate Shoffner, 2018 Stanislav Denisov
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. using System;
  27. using System.Runtime.InteropServices;
  28. using System.Security;
  29. using System.Text;
  30. namespace ENet {
  31. [Flags]
  32. public enum PacketFlags {
  33. None = 0,
  34. Reliable = 1 << 0,
  35. Unsequenced = 1 << 1,
  36. NoAllocate = 1 << 2,
  37. UnreliableFragmented = 1 << 3,
  38. Instant = 1 << 4,
  39. Crucial = 1 << 5,
  40. Sent = 1 << 8
  41. }
  42. public enum EventType {
  43. None = 0,
  44. Connect = 1,
  45. Disconnect = 2,
  46. Receive = 3,
  47. Timeout = 4
  48. }
  49. public enum PeerState {
  50. Uninitialized = -1,
  51. Disconnected = 0,
  52. Connecting = 1,
  53. AcknowledgingConnect = 2,
  54. ConnectionPending = 3,
  55. ConnectionSucceeded = 4,
  56. Connected = 5,
  57. DisconnectLater = 6,
  58. Disconnecting = 7,
  59. AcknowledgingDisconnect = 8,
  60. Zombie = 9
  61. }
  62. [StructLayout(LayoutKind.Explicit, Size = 18)]
  63. internal struct ENetAddress {
  64. [FieldOffset(16)]
  65. public ushort port;
  66. }
  67. [StructLayout(LayoutKind.Sequential)]
  68. internal struct ENetEvent {
  69. public EventType type;
  70. public IntPtr peer;
  71. public byte channelID;
  72. public uint data;
  73. public IntPtr packet;
  74. }
  75. internal static class ArrayPool {
  76. [ThreadStatic]
  77. private static byte[] byteBuffer;
  78. [ThreadStatic]
  79. private static IntPtr[] pointerBuffer;
  80. public static byte[] GetByteBuffer() {
  81. if (byteBuffer == null)
  82. byteBuffer = new byte[64];
  83. return byteBuffer;
  84. }
  85. public static IntPtr[] GetPointerBuffer() {
  86. if (pointerBuffer == null)
  87. pointerBuffer = new IntPtr[Library.maxPeers];
  88. return pointerBuffer;
  89. }
  90. }
  91. public struct Address {
  92. private ENetAddress nativeAddress;
  93. internal ENetAddress NativeData {
  94. get {
  95. return nativeAddress;
  96. }
  97. set {
  98. nativeAddress = value;
  99. }
  100. }
  101. internal Address(ENetAddress address) {
  102. nativeAddress = address;
  103. }
  104. public ushort Port {
  105. get {
  106. return nativeAddress.port;
  107. }
  108. set {
  109. nativeAddress.port = value;
  110. }
  111. }
  112. public string GetIP() {
  113. StringBuilder ip = new StringBuilder(1025);
  114. if (Native.enet_address_get_ip(ref nativeAddress, ip, (IntPtr)ip.Capacity) != 0)
  115. return String.Empty;
  116. return ip.ToString();
  117. }
  118. public bool SetIP(string ip) {
  119. if (ip == null)
  120. throw new ArgumentNullException("ip");
  121. return Native.enet_address_set_ip(ref nativeAddress, ip) == 0;
  122. }
  123. public string GetHost() {
  124. StringBuilder hostName = new StringBuilder(1025);
  125. if (Native.enet_address_get_hostname(ref nativeAddress, hostName, (IntPtr)hostName.Capacity) != 0)
  126. return String.Empty;
  127. return hostName.ToString();
  128. }
  129. public bool SetHost(string hostName) {
  130. if (hostName == null)
  131. throw new ArgumentNullException("hostName");
  132. return Native.enet_address_set_hostname(ref nativeAddress, hostName) == 0;
  133. }
  134. }
  135. public struct Event {
  136. private ENetEvent nativeEvent;
  137. internal ENetEvent NativeData {
  138. get {
  139. return nativeEvent;
  140. }
  141. set {
  142. nativeEvent = value;
  143. }
  144. }
  145. internal Event(ENetEvent @event) {
  146. nativeEvent = @event;
  147. }
  148. public EventType Type {
  149. get {
  150. return nativeEvent.type;
  151. }
  152. }
  153. public Peer Peer {
  154. get {
  155. return new Peer(nativeEvent.peer);
  156. }
  157. }
  158. public byte ChannelID {
  159. get {
  160. return nativeEvent.channelID;
  161. }
  162. }
  163. public uint Data {
  164. get {
  165. return nativeEvent.data;
  166. }
  167. }
  168. public Packet Packet {
  169. get {
  170. return new Packet(nativeEvent.packet);
  171. }
  172. }
  173. }
  174. public struct Packet : IDisposable {
  175. private IntPtr nativePacket;
  176. internal IntPtr NativeData {
  177. get {
  178. return nativePacket;
  179. }
  180. set {
  181. nativePacket = value;
  182. }
  183. }
  184. internal Packet(IntPtr packet) {
  185. nativePacket = packet;
  186. }
  187. public void Dispose() {
  188. if (nativePacket != IntPtr.Zero) {
  189. Native.enet_packet_dispose(nativePacket);
  190. nativePacket = IntPtr.Zero;
  191. }
  192. }
  193. public bool IsSet {
  194. get {
  195. return nativePacket != IntPtr.Zero;
  196. }
  197. }
  198. public IntPtr Data {
  199. get {
  200. IsCreated();
  201. return Native.enet_packet_get_data(nativePacket);
  202. }
  203. }
  204. public IntPtr UserData {
  205. get {
  206. IsCreated();
  207. return Native.enet_packet_get_user_data(nativePacket);
  208. }
  209. set {
  210. IsCreated();
  211. Native.enet_packet_set_user_data(nativePacket, value);
  212. }
  213. }
  214. public int Length {
  215. get {
  216. IsCreated();
  217. return Native.enet_packet_get_length(nativePacket);
  218. }
  219. }
  220. public bool HasReferences {
  221. get {
  222. IsCreated();
  223. return Native.enet_packet_check_references(nativePacket) != 0;
  224. }
  225. }
  226. internal void IsCreated() {
  227. if (nativePacket == IntPtr.Zero)
  228. throw new InvalidOperationException("Packet not created");
  229. }
  230. public void Create(byte[] data) {
  231. if (data == null)
  232. throw new ArgumentNullException("data");
  233. Create(data, data.Length);
  234. }
  235. public void Create(byte[] data, int length) {
  236. Create(data, length, PacketFlags.None);
  237. }
  238. public void Create(byte[] data, PacketFlags flags) {
  239. Create(data, data.Length, flags);
  240. }
  241. public void Create(byte[] data, int length, PacketFlags flags) {
  242. if (data == null)
  243. throw new ArgumentNullException("data");
  244. if (length < 0 || length > data.Length)
  245. throw new ArgumentOutOfRangeException();
  246. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  247. }
  248. public void Create(IntPtr data, int length, PacketFlags flags) {
  249. if (data == IntPtr.Zero)
  250. throw new ArgumentNullException("data");
  251. if (length < 0)
  252. throw new ArgumentOutOfRangeException();
  253. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  254. }
  255. public void Create(byte[] data, int offset, int length, PacketFlags flags) {
  256. if (data == null)
  257. throw new ArgumentNullException("data");
  258. if (offset < 0 || length < 0 || length > data.Length)
  259. throw new ArgumentOutOfRangeException();
  260. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  261. }
  262. public void Create(IntPtr data, int offset, int length, PacketFlags flags) {
  263. if (data == IntPtr.Zero)
  264. throw new ArgumentNullException("data");
  265. if (offset < 0 || length < 0)
  266. throw new ArgumentOutOfRangeException();
  267. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  268. }
  269. public void CopyTo(byte[] destination) {
  270. if (destination == null)
  271. throw new ArgumentNullException("destination");
  272. // Fix by katori, prevents trying to copy a NULL
  273. // from native world (ie. disconnect a client)
  274. if (Data == null)
  275. {
  276. return;
  277. }
  278. Marshal.Copy(Data, destination, 0, Length);
  279. }
  280. }
  281. public class Host : IDisposable {
  282. private IntPtr nativeHost;
  283. internal IntPtr NativeData {
  284. get {
  285. return nativeHost;
  286. }
  287. set {
  288. nativeHost = value;
  289. }
  290. }
  291. public void Dispose() {
  292. Dispose(true);
  293. GC.SuppressFinalize(this);
  294. }
  295. protected virtual void Dispose(bool disposing) {
  296. if (nativeHost != IntPtr.Zero) {
  297. Native.enet_host_destroy(nativeHost);
  298. nativeHost = IntPtr.Zero;
  299. }
  300. }
  301. ~Host() {
  302. Dispose(false);
  303. }
  304. public bool IsSet {
  305. get {
  306. return nativeHost != IntPtr.Zero;
  307. }
  308. }
  309. public uint PeersCount {
  310. get {
  311. IsCreated();
  312. return Native.enet_host_get_peers_count(nativeHost);
  313. }
  314. }
  315. public uint PacketsSent {
  316. get {
  317. IsCreated();
  318. return Native.enet_host_get_packets_sent(nativeHost);
  319. }
  320. }
  321. public uint PacketsReceived {
  322. get {
  323. IsCreated();
  324. return Native.enet_host_get_packets_received(nativeHost);
  325. }
  326. }
  327. public uint BytesSent {
  328. get {
  329. IsCreated();
  330. return Native.enet_host_get_bytes_sent(nativeHost);
  331. }
  332. }
  333. public uint BytesReceived {
  334. get {
  335. IsCreated();
  336. return Native.enet_host_get_bytes_received(nativeHost);
  337. }
  338. }
  339. internal void IsCreated() {
  340. if (nativeHost == IntPtr.Zero)
  341. throw new InvalidOperationException("Host not created");
  342. }
  343. private void IsChannelLimited(int channelLimit) {
  344. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  345. throw new ArgumentOutOfRangeException("channelLimit");
  346. }
  347. public void Create() {
  348. Create(null, 1, 0);
  349. }
  350. public void Create(int bufferSize) {
  351. Create(null, 1, 0, 0, 0, bufferSize);
  352. }
  353. public void Create(Address? address, int peerLimit) {
  354. Create(address, peerLimit, 0);
  355. }
  356. public void Create(Address? address, int peerLimit, int channelLimit) {
  357. Create(address, peerLimit, channelLimit, 0, 0, 0);
  358. }
  359. public void Create(int peerLimit, int channelLimit) {
  360. Create(null, peerLimit, channelLimit, 0, 0, 0);
  361. }
  362. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  363. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  364. }
  365. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  366. Create(address, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  367. }
  368. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize) {
  369. if (nativeHost != IntPtr.Zero)
  370. throw new InvalidOperationException("Host already created");
  371. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  372. throw new ArgumentOutOfRangeException("peerLimit");
  373. IsChannelLimited(channelLimit);
  374. if (address != null) {
  375. var nativeAddress = address.Value.NativeData;
  376. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  377. } else {
  378. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  379. }
  380. if (nativeHost == IntPtr.Zero)
  381. throw new InvalidOperationException("Host creation call failed");
  382. }
  383. public void PreventConnections(bool state) {
  384. IsCreated();
  385. Native.enet_host_prevent_connections(nativeHost, (byte)(state ? 1 : 0));
  386. }
  387. public void Broadcast(byte channelID, ref Packet packet) {
  388. IsCreated();
  389. packet.IsCreated();
  390. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  391. packet.NativeData = IntPtr.Zero;
  392. }
  393. public void Broadcast(byte channelID, ref Packet packet, Peer excludedPeer) {
  394. IsCreated();
  395. packet.IsCreated();
  396. Native.enet_host_broadcast_exclude(nativeHost, channelID, packet.NativeData, excludedPeer.NativeData);
  397. packet.NativeData = IntPtr.Zero;
  398. }
  399. public void Broadcast(byte channelID, ref Packet packet, Peer[] peers) {
  400. IsCreated();
  401. packet.IsCreated();
  402. if (peers.Length > 0) {
  403. IntPtr[] nativePeers = ArrayPool.GetPointerBuffer();
  404. int nativeCount = 0;
  405. for (int i = 0; i < peers.Length; i++) {
  406. if (peers[i].NativeData != IntPtr.Zero) {
  407. nativePeers[nativeCount] = peers[i].NativeData;
  408. nativeCount++;
  409. }
  410. }
  411. Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, nativePeers, (IntPtr)nativeCount);
  412. }
  413. packet.NativeData = IntPtr.Zero;
  414. }
  415. public int CheckEvents(out Event @event) {
  416. IsCreated();
  417. ENetEvent nativeEvent;
  418. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  419. if (result <= 0) {
  420. @event = default;
  421. return result;
  422. }
  423. @event = new Event(nativeEvent);
  424. return result;
  425. }
  426. public Peer Connect(Address address) {
  427. return Connect(address, 0, 0);
  428. }
  429. public Peer Connect(Address address, int channelLimit) {
  430. return Connect(address, channelLimit, 0);
  431. }
  432. public Peer Connect(Address address, int channelLimit, uint data) {
  433. IsCreated();
  434. IsChannelLimited(channelLimit);
  435. var nativeAddress = address.NativeData;
  436. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  437. if (peer.NativeData == IntPtr.Zero)
  438. throw new InvalidOperationException("Host connect call failed");
  439. return peer;
  440. }
  441. public int Service(int timeout, out Event @event) {
  442. if (timeout < 0)
  443. throw new ArgumentOutOfRangeException("timeout");
  444. IsCreated();
  445. ENetEvent nativeEvent;
  446. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  447. if (result <= 0) {
  448. @event = default;
  449. return result;
  450. }
  451. @event = new Event(nativeEvent);
  452. return result;
  453. }
  454. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  455. IsCreated();
  456. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  457. }
  458. public void SetChannelLimit(int channelLimit) {
  459. IsCreated();
  460. IsChannelLimited(channelLimit);
  461. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  462. }
  463. public void Flush() {
  464. IsCreated();
  465. Native.enet_host_flush(nativeHost);
  466. }
  467. }
  468. public struct Peer {
  469. private IntPtr nativePeer;
  470. private uint nativeID;
  471. internal IntPtr NativeData {
  472. get {
  473. return nativePeer;
  474. }
  475. set {
  476. nativePeer = value;
  477. }
  478. }
  479. internal Peer(IntPtr peer) {
  480. nativePeer = peer;
  481. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  482. }
  483. public bool IsSet {
  484. get {
  485. return nativePeer != IntPtr.Zero;
  486. }
  487. }
  488. public uint ID {
  489. get {
  490. return nativeID;
  491. }
  492. }
  493. public string IP {
  494. get {
  495. IsCreated();
  496. byte[] ip = ArrayPool.GetByteBuffer();
  497. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0)
  498. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  499. else
  500. return String.Empty;
  501. }
  502. }
  503. public ushort Port {
  504. get {
  505. IsCreated();
  506. return Native.enet_peer_get_port(nativePeer);
  507. }
  508. }
  509. public uint MTU {
  510. get {
  511. IsCreated();
  512. return Native.enet_peer_get_mtu(nativePeer);
  513. }
  514. }
  515. public PeerState State {
  516. get {
  517. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  518. }
  519. }
  520. public uint RoundTripTime {
  521. get {
  522. IsCreated();
  523. return Native.enet_peer_get_rtt(nativePeer);
  524. }
  525. }
  526. public uint LastRoundTripTime {
  527. get {
  528. IsCreated();
  529. return Native.enet_peer_get_last_rtt(nativePeer);
  530. }
  531. }
  532. public uint LastSendTime {
  533. get {
  534. IsCreated();
  535. return Native.enet_peer_get_lastsendtime(nativePeer);
  536. }
  537. }
  538. public uint LastReceiveTime {
  539. get {
  540. IsCreated();
  541. return Native.enet_peer_get_lastreceivetime(nativePeer);
  542. }
  543. }
  544. public ulong PacketsSent {
  545. get {
  546. IsCreated();
  547. return Native.enet_peer_get_packets_sent(nativePeer);
  548. }
  549. }
  550. public ulong PacketsLost {
  551. get {
  552. IsCreated();
  553. return Native.enet_peer_get_packets_lost(nativePeer);
  554. }
  555. }
  556. public ulong BytesSent {
  557. get {
  558. IsCreated();
  559. return Native.enet_peer_get_bytes_sent(nativePeer);
  560. }
  561. }
  562. public ulong BytesReceived {
  563. get {
  564. IsCreated();
  565. return Native.enet_peer_get_bytes_received(nativePeer);
  566. }
  567. }
  568. public IntPtr Data {
  569. get {
  570. IsCreated();
  571. return Native.enet_peer_get_data(nativePeer);
  572. }
  573. set {
  574. IsCreated();
  575. Native.enet_peer_set_data(nativePeer, value);
  576. }
  577. }
  578. internal void IsCreated() {
  579. if (nativePeer == IntPtr.Zero)
  580. throw new InvalidOperationException("Peer not created");
  581. }
  582. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration, uint threshold) {
  583. IsCreated();
  584. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration, threshold);
  585. }
  586. public bool Send(byte channelID, ref Packet packet) {
  587. IsCreated();
  588. packet.IsCreated();
  589. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  590. }
  591. public bool Receive(out byte channelID, out Packet packet) {
  592. IsCreated();
  593. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  594. if (nativePacket != IntPtr.Zero) {
  595. packet = new Packet(nativePacket);
  596. return true;
  597. }
  598. packet = default;
  599. return false;
  600. }
  601. public void Ping() {
  602. IsCreated();
  603. Native.enet_peer_ping(nativePeer);
  604. }
  605. public void PingInterval(uint interval) {
  606. IsCreated();
  607. Native.enet_peer_ping_interval(nativePeer, interval);
  608. }
  609. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  610. IsCreated();
  611. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  612. }
  613. public void Disconnect(uint data) {
  614. IsCreated();
  615. Native.enet_peer_disconnect(nativePeer, data);
  616. }
  617. public void DisconnectNow(uint data) {
  618. IsCreated();
  619. Native.enet_peer_disconnect_now(nativePeer, data);
  620. }
  621. public void DisconnectLater(uint data) {
  622. IsCreated();
  623. Native.enet_peer_disconnect_later(nativePeer, data);
  624. }
  625. public void Reset() {
  626. IsCreated();
  627. Native.enet_peer_reset(nativePeer);
  628. }
  629. // == ADDITIONS NOT AVAILABLE IN UPSTREAM REPOSITORY == //
  630. // These are placed here to ensure that merge conflicts aren't a
  631. // pain in the ass.
  632. // SendAndReturnStatusCode returns either 0 if the send was successful,
  633. // or the ENET return code if not. Sometimes a bool is not enough to determine
  634. // the root cause of a issue.
  635. public int SendAndReturnStatusCode(byte channelID, ref Packet packet)
  636. {
  637. IsCreated();
  638. packet.IsCreated();
  639. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData);
  640. }
  641. }
  642. public static class Extensions {
  643. public static int StringLength(this byte[] data) {
  644. if (data == null)
  645. throw new ArgumentNullException("data");
  646. int i;
  647. for (i = 0; i < data.Length && data[i] != 0; i++);
  648. return i;
  649. }
  650. }
  651. public static class Library {
  652. public const uint maxChannelCount = 0xFF;
  653. public const uint maxPeers = 0xFFF;
  654. public const uint maxPacketSize = 32 * 1024 * 1024;
  655. public const uint throttleThreshold = 20;
  656. public const uint throttleScale = 32;
  657. public const uint throttleAcceleration = 2;
  658. public const uint throttleDeceleration = 2;
  659. public const uint throttleInterval = 5000;
  660. public const uint timeoutLimit = 32;
  661. public const uint timeoutMinimum = 5000;
  662. public const uint timeoutMaximum = 30000;
  663. // Lock our version to 2.4.0, to avoid confusion with upstream.
  664. public const uint version = (2 << 16) | (4 << 8) | (0);
  665. public static bool Initialize() {
  666. return Native.enet_initialize() == 0;
  667. }
  668. public static void Deinitialize() {
  669. Native.enet_deinitialize();
  670. }
  671. // Supports for mimalloc memory allocator.
  672. public static IntPtr Malloc(int size) => Malloc((ulong)size);
  673. public static IntPtr Malloc(ulong size)
  674. {
  675. return Native.enet_mem_acquire(size);
  676. }
  677. public static void Free(IntPtr alloc)
  678. {
  679. Native.enet_mem_release(alloc);
  680. }
  681. public static uint Time {
  682. get {
  683. return Native.enet_time_get();
  684. }
  685. }
  686. }
  687. [SuppressUnmanagedCodeSecurity]
  688. internal static class Native {
  689. #if __IOS__ || UNITY_IOS && !UNITY_EDITOR
  690. // We're building for a certain mobile fruity OS.
  691. private const string nativeLibrary = "__Internal";
  692. #elif __APPLE__ || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
  693. // We're building for a certain fruity OS.
  694. private const string nativeLibrary = "libenet";
  695. #else
  696. // Assume everything else, Windows et al...
  697. private const string nativeLibrary = "enet";
  698. #endif
  699. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  700. internal static extern int enet_initialize();
  701. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  702. internal static extern IntPtr enet_mem_acquire(ulong sz);
  703. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  704. internal static extern void enet_mem_release(IntPtr alloc);
  705. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  706. internal static extern void enet_deinitialize();
  707. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  708. internal static extern uint enet_time_get();
  709. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  710. internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
  711. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  712. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  713. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  714. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  715. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  716. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  717. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  718. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  719. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  720. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  721. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  722. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  723. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  724. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  725. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  726. internal static extern int enet_packet_check_references(IntPtr packet);
  727. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  728. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  729. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  730. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  731. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  732. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  733. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  734. internal static extern int enet_packet_get_length(IntPtr packet);
  735. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  736. internal static extern void enet_packet_dispose(IntPtr packet);
  737. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  738. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  739. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  740. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  741. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  742. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  743. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  744. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  745. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  746. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  747. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  748. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  749. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  750. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  751. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  752. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  753. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  754. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  755. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  756. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  757. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  758. internal static extern uint enet_host_get_peers_count(IntPtr host);
  759. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  760. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  761. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  762. internal static extern uint enet_host_get_packets_received(IntPtr host);
  763. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  764. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  765. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  766. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  767. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  768. internal static extern void enet_host_flush(IntPtr host);
  769. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  770. internal static extern void enet_host_destroy(IntPtr host);
  771. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  772. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  773. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  774. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  775. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  776. internal static extern uint enet_peer_get_id(IntPtr peer);
  777. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  778. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  779. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  780. internal static extern ushort enet_peer_get_port(IntPtr peer);
  781. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  782. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  783. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  784. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  785. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  786. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  787. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  788. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  789. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  790. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  791. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  792. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  793. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  794. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  795. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  796. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  797. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  798. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  799. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  800. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  801. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  802. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  803. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  804. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  805. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  806. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  807. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  808. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  809. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  810. internal static extern void enet_peer_ping(IntPtr peer);
  811. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  812. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  813. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  814. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  815. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  816. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  817. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  818. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  819. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  820. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  821. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  822. internal static extern void enet_peer_reset(IntPtr peer);
  823. }
  824. }