ENet.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. }
  448. packet.NativeData = IntPtr.Zero;
  449. }
  450. public int CheckEvents(out Event @event) {
  451. ThrowIfNotCreated();
  452. ENetEvent nativeEvent;
  453. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  454. if (result <= 0) {
  455. @event = default(Event);
  456. return result;
  457. }
  458. @event = new Event(nativeEvent);
  459. return result;
  460. }
  461. public Peer Connect(Address address) {
  462. return Connect(address, 0, 0);
  463. }
  464. public Peer Connect(Address address, int channelLimit) {
  465. return Connect(address, channelLimit, 0);
  466. }
  467. public Peer Connect(Address address, int channelLimit, uint data) {
  468. ThrowIfNotCreated();
  469. ThrowIfChannelsExceeded(channelLimit);
  470. var nativeAddress = address.NativeData;
  471. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  472. if (peer.NativeData == IntPtr.Zero)
  473. throw new InvalidOperationException("Host connect call failed");
  474. return peer;
  475. }
  476. public int Service(int timeout, out Event @event) {
  477. if (timeout < 0)
  478. throw new ArgumentOutOfRangeException("timeout");
  479. ThrowIfNotCreated();
  480. ENetEvent nativeEvent;
  481. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  482. if (result <= 0) {
  483. @event = default(Event);
  484. return result;
  485. }
  486. @event = new Event(nativeEvent);
  487. return result;
  488. }
  489. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  490. ThrowIfNotCreated();
  491. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  492. }
  493. public void SetChannelLimit(int channelLimit) {
  494. ThrowIfNotCreated();
  495. ThrowIfChannelsExceeded(channelLimit);
  496. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  497. }
  498. public void SetMaxDuplicatePeers(ushort number) {
  499. ThrowIfNotCreated();
  500. Native.enet_host_set_max_duplicate_peers(nativeHost, number);
  501. }
  502. public void SetInterceptCallback(IntPtr callback) {
  503. ThrowIfNotCreated();
  504. Native.enet_host_set_intercept_callback(nativeHost, callback);
  505. }
  506. public void SetInterceptCallback(InterceptCallback callback) {
  507. ThrowIfNotCreated();
  508. Native.enet_host_set_intercept_callback(nativeHost, Marshal.GetFunctionPointerForDelegate(callback));
  509. }
  510. public void SetChecksumCallback(IntPtr callback) {
  511. ThrowIfNotCreated();
  512. Native.enet_host_set_checksum_callback(nativeHost, callback);
  513. }
  514. public void SetChecksumCallback(ChecksumCallback callback) {
  515. ThrowIfNotCreated();
  516. Native.enet_host_set_checksum_callback(nativeHost, Marshal.GetFunctionPointerForDelegate(callback));
  517. }
  518. public void Flush() {
  519. ThrowIfNotCreated();
  520. Native.enet_host_flush(nativeHost);
  521. }
  522. }
  523. public struct Peer {
  524. private IntPtr nativePeer;
  525. private uint nativeID;
  526. internal IntPtr NativeData {
  527. get {
  528. return nativePeer;
  529. }
  530. set {
  531. nativePeer = value;
  532. }
  533. }
  534. internal Peer(IntPtr peer) {
  535. nativePeer = peer;
  536. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  537. }
  538. public bool IsSet {
  539. get {
  540. return nativePeer != IntPtr.Zero;
  541. }
  542. }
  543. public uint ID {
  544. get {
  545. return nativeID;
  546. }
  547. }
  548. public string IP {
  549. get {
  550. ThrowIfNotCreated();
  551. byte[] ip = ArrayPool.GetByteBuffer();
  552. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0)
  553. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  554. else
  555. return String.Empty;
  556. }
  557. }
  558. public ushort Port {
  559. get {
  560. ThrowIfNotCreated();
  561. return Native.enet_peer_get_port(nativePeer);
  562. }
  563. }
  564. public uint MTU {
  565. get {
  566. ThrowIfNotCreated();
  567. return Native.enet_peer_get_mtu(nativePeer);
  568. }
  569. }
  570. public PeerState State {
  571. get {
  572. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  573. }
  574. }
  575. public uint RoundTripTime {
  576. get {
  577. ThrowIfNotCreated();
  578. return Native.enet_peer_get_rtt(nativePeer);
  579. }
  580. }
  581. public uint LastRoundTripTime {
  582. get {
  583. ThrowIfNotCreated();
  584. return Native.enet_peer_get_last_rtt(nativePeer);
  585. }
  586. }
  587. public uint LastSendTime {
  588. get {
  589. ThrowIfNotCreated();
  590. return Native.enet_peer_get_lastsendtime(nativePeer);
  591. }
  592. }
  593. public uint LastReceiveTime {
  594. get {
  595. ThrowIfNotCreated();
  596. return Native.enet_peer_get_lastreceivetime(nativePeer);
  597. }
  598. }
  599. public ulong PacketsSent {
  600. get {
  601. ThrowIfNotCreated();
  602. return Native.enet_peer_get_packets_sent(nativePeer);
  603. }
  604. }
  605. public ulong PacketsLost {
  606. get {
  607. ThrowIfNotCreated();
  608. return Native.enet_peer_get_packets_lost(nativePeer);
  609. }
  610. }
  611. public float PacketsThrottle {
  612. get {
  613. ThrowIfNotCreated();
  614. return Native.enet_peer_get_packets_throttle(nativePeer);
  615. }
  616. }
  617. public ulong BytesSent {
  618. get {
  619. ThrowIfNotCreated();
  620. return Native.enet_peer_get_bytes_sent(nativePeer);
  621. }
  622. }
  623. public ulong BytesReceived {
  624. get {
  625. ThrowIfNotCreated();
  626. return Native.enet_peer_get_bytes_received(nativePeer);
  627. }
  628. }
  629. public IntPtr Data {
  630. get {
  631. ThrowIfNotCreated();
  632. return Native.enet_peer_get_data(nativePeer);
  633. }
  634. set {
  635. ThrowIfNotCreated();
  636. Native.enet_peer_set_data(nativePeer, value);
  637. }
  638. }
  639. internal void ThrowIfNotCreated() {
  640. if (nativePeer == IntPtr.Zero)
  641. throw new InvalidOperationException("Peer not created");
  642. }
  643. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration, uint threshold) {
  644. ThrowIfNotCreated();
  645. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration, threshold);
  646. }
  647. public bool Send(byte channelID, ref Packet packet) {
  648. ThrowIfNotCreated();
  649. packet.ThrowIfNotCreated();
  650. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  651. }
  652. public bool Receive(out byte channelID, out Packet packet) {
  653. ThrowIfNotCreated();
  654. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  655. if (nativePacket != IntPtr.Zero) {
  656. packet = new Packet(nativePacket);
  657. return true;
  658. }
  659. packet = default(Packet);
  660. return false;
  661. }
  662. public void Ping() {
  663. ThrowIfNotCreated();
  664. Native.enet_peer_ping(nativePeer);
  665. }
  666. public void PingInterval(uint interval) {
  667. ThrowIfNotCreated();
  668. Native.enet_peer_ping_interval(nativePeer, interval);
  669. }
  670. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  671. ThrowIfNotCreated();
  672. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  673. }
  674. public void Disconnect(uint data) {
  675. ThrowIfNotCreated();
  676. Native.enet_peer_disconnect(nativePeer, data);
  677. }
  678. public void DisconnectNow(uint data) {
  679. ThrowIfNotCreated();
  680. Native.enet_peer_disconnect_now(nativePeer, data);
  681. }
  682. public void DisconnectLater(uint data) {
  683. ThrowIfNotCreated();
  684. Native.enet_peer_disconnect_later(nativePeer, data);
  685. }
  686. public void Reset() {
  687. ThrowIfNotCreated();
  688. Native.enet_peer_reset(nativePeer);
  689. }
  690. }
  691. public static class Extensions {
  692. public static int StringLength(this byte[] data) {
  693. if (data == null)
  694. throw new ArgumentNullException("data");
  695. int i;
  696. for (i = 0; i < data.Length && data[i] != 0; i++);
  697. return i;
  698. }
  699. }
  700. public static class Library {
  701. public const uint maxChannelCount = 0xFF;
  702. public const uint maxPeers = 0xFFF;
  703. public const uint maxPacketSize = 32 * 1024 * 1024;
  704. public const uint throttleThreshold = 40;
  705. public const uint throttleScale = 32;
  706. public const uint throttleAcceleration = 2;
  707. public const uint throttleDeceleration = 2;
  708. public const uint throttleInterval = 5000;
  709. public const uint timeoutLimit = 32;
  710. public const uint timeoutMinimum = 5000;
  711. public const uint timeoutMaximum = 30000;
  712. public const uint version = (2 << 16) | (4 << 8) | (7);
  713. public static uint Time {
  714. get {
  715. return Native.enet_time_get();
  716. }
  717. }
  718. public static bool Initialize() {
  719. if (Native.enet_linked_version() != version)
  720. throw new InvalidOperationException("Incompatatible version");
  721. return Native.enet_initialize() == 0;
  722. }
  723. public static bool Initialize(Callbacks callbacks) {
  724. if (callbacks == null)
  725. throw new ArgumentNullException("callbacks");
  726. if (Native.enet_linked_version() != version)
  727. throw new InvalidOperationException("Incompatatible version");
  728. ENetCallbacks nativeCallbacks = callbacks.NativeData;
  729. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
  730. }
  731. public static void Deinitialize() {
  732. Native.enet_deinitialize();
  733. }
  734. public static ulong CRC64(IntPtr buffers, int bufferCount) {
  735. return Native.enet_crc64(buffers, bufferCount);
  736. }
  737. }
  738. [SuppressUnmanagedCodeSecurity]
  739. internal static class Native {
  740. #if __IOS__ || UNITY_IOS && !UNITY_EDITOR
  741. private const string nativeLibrary = "__Internal";
  742. #else
  743. private const string nativeLibrary = "enet";
  744. #endif
  745. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  746. internal static extern int enet_initialize();
  747. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  748. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  749. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  750. internal static extern void enet_deinitialize();
  751. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  752. internal static extern uint enet_linked_version();
  753. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  754. internal static extern uint enet_time_get();
  755. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  756. internal static extern ulong enet_crc64(IntPtr buffers, int bufferCount);
  757. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  758. internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
  759. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  760. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  761. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  762. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  763. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  764. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  765. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  766. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  767. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  768. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  769. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  770. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  771. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  772. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  773. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  774. internal static extern int enet_packet_check_references(IntPtr packet);
  775. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  776. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  777. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  778. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  779. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  780. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  781. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  782. internal static extern int enet_packet_get_length(IntPtr packet);
  783. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  784. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  785. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  786. internal static extern void enet_packet_dispose(IntPtr packet);
  787. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  788. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  789. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  790. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  791. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  792. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  793. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  794. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  795. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  796. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  797. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  798. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  799. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  800. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  801. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  802. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  803. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  804. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  805. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  806. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  807. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  808. internal static extern uint enet_host_get_peers_count(IntPtr host);
  809. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  810. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  811. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  812. internal static extern uint enet_host_get_packets_received(IntPtr host);
  813. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  814. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  815. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  816. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  817. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  818. internal static extern void enet_host_set_max_duplicate_peers(IntPtr host, ushort number);
  819. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  820. internal static extern void enet_host_set_intercept_callback(IntPtr host, IntPtr callback);
  821. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  822. internal static extern void enet_host_set_checksum_callback(IntPtr host, IntPtr callback);
  823. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  824. internal static extern void enet_host_flush(IntPtr host);
  825. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  826. internal static extern void enet_host_destroy(IntPtr host);
  827. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  828. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  829. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  830. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  831. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  832. internal static extern uint enet_peer_get_id(IntPtr peer);
  833. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  834. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  835. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  836. internal static extern ushort enet_peer_get_port(IntPtr peer);
  837. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  838. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  839. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  840. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  841. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  842. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  843. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  844. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  845. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  846. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  847. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  848. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  849. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  850. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  851. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  852. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  853. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  854. internal static extern float enet_peer_get_packets_throttle(IntPtr peer);
  855. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  856. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  857. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  858. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  859. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  860. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  861. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  862. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  863. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  864. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  865. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  866. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  867. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  868. internal static extern void enet_peer_ping(IntPtr peer);
  869. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  870. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  871. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  872. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  873. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  874. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  875. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  876. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  877. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  878. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  879. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  880. internal static extern void enet_peer_reset(IntPtr peer);
  881. }
  882. }