ENet.cs 31 KB

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