ENet.cs 31 KB

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