ENet.cs 30 KB

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