ENet.cs 33 KB

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