ENet.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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, int startPos = 0) {
  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, startPos, 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. if (Native.enet_linked_version() != version)
  701. throw new InvalidOperationException("You're trying to use an incompatible version of Enet with this Managed Library.");
  702. return Native.enet_initialize() == 0;
  703. }
  704. public static bool Initialize(Callbacks callbacks) {
  705. if(callbacks == null)
  706. throw new ArgumentNullException("callbacks");
  707. if (Native.enet_linked_version() != version)
  708. throw new InvalidOperationException("You're trying to use an incompatible version of Enet with this Managed Library.");
  709. ENetCallbacks nativeCallbacks = callbacks.NativeData;
  710. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
  711. }
  712. public static void Deinitialize() {
  713. Native.enet_deinitialize();
  714. }
  715. public static uint Time {
  716. get {
  717. return Native.enet_time_get();
  718. }
  719. }
  720. }
  721. [SuppressUnmanagedCodeSecurity]
  722. internal static class Native {
  723. // This should address Unity usage and bug #66: Platform specific Enet / libenet
  724. // https://github.com/SoftwareGuy/Ignorance/issues/66
  725. #if UNITY_EDITOR
  726. // We are inside the Unity Editor.
  727. #if UNITY_EDITOR_OSX
  728. // Unity Editor on macOS needs to use libenet.
  729. private const string nativeLibrary = "libenet";
  730. #else
  731. // TODO: Check if Linux requires 'libenet' too. (Apparently not?)
  732. private const string nativeLibrary = "enet";
  733. #endif
  734. #endif
  735. #if !UNITY_EDITOR
  736. // We're not inside the Unity Editor.
  737. #if __APPLE__ && !(__IOS__ || UNITY_IOS)
  738. // Use libenet on macOS.
  739. private const string nativeLibrary = "libenet";
  740. #elif __IOS__ || UNITY_IOS
  741. // We're building for a certain mobile fruity OS.
  742. private const string nativeLibrary = "__Internal";
  743. #else
  744. // Assume everything else, Windows et al... TODO: Linux check
  745. private const string nativeLibrary = "enet";
  746. #endif
  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 int enet_address_set_ip(ref ENetAddress address, string ip);
  760. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  761. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  762. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  763. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  764. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  765. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  766. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  767. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  768. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  769. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  770. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  771. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  772. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  773. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  774. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  775. internal static extern int enet_packet_check_references(IntPtr packet);
  776. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  777. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  778. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  779. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  780. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  781. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  782. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  783. internal static extern int enet_packet_get_length(IntPtr packet);
  784. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  785. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  786. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  787. internal static extern void enet_packet_dispose(IntPtr packet);
  788. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  789. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  790. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  791. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  792. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  793. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  794. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  795. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  796. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  797. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  798. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  799. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  800. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  801. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  802. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  803. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  804. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  805. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  806. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  807. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  808. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  809. internal static extern uint enet_host_get_peers_count(IntPtr host);
  810. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  811. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  812. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  813. internal static extern uint enet_host_get_packets_received(IntPtr host);
  814. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  815. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  816. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  817. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  818. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  819. internal static extern void enet_host_flush(IntPtr host);
  820. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  821. internal static extern void enet_host_destroy(IntPtr host);
  822. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  823. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  824. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  825. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  826. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  827. internal static extern uint enet_peer_get_id(IntPtr peer);
  828. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  829. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  830. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  831. internal static extern ushort enet_peer_get_port(IntPtr peer);
  832. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  833. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  834. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  835. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  836. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  837. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  838. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  839. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  840. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  841. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  842. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  843. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  844. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  845. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  846. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  847. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  848. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  849. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  850. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  851. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  852. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  853. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  854. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  855. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  856. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  857. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  858. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  859. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  860. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  861. internal static extern void enet_peer_ping(IntPtr peer);
  862. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  863. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  864. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  865. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  866. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  867. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  868. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  869. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  870. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  871. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  872. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  873. internal static extern void enet_peer_reset(IntPtr peer);
  874. }
  875. }