ENet.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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. Unthrottled = 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. public delegate int InterceptCallback(ref Event @event, IntPtr receivedData, int receivedDataLength);
  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. IsCreated();
  228. return Native.enet_packet_get_data(nativePacket);
  229. }
  230. }
  231. public IntPtr UserData {
  232. get {
  233. IsCreated();
  234. return Native.enet_packet_get_user_data(nativePacket);
  235. }
  236. set {
  237. IsCreated();
  238. Native.enet_packet_set_user_data(nativePacket, value);
  239. }
  240. }
  241. public int Length {
  242. get {
  243. IsCreated();
  244. return Native.enet_packet_get_length(nativePacket);
  245. }
  246. }
  247. public bool HasReferences {
  248. get {
  249. IsCreated();
  250. return Native.enet_packet_check_references(nativePacket) != 0;
  251. }
  252. }
  253. internal void IsCreated() {
  254. if (nativePacket == IntPtr.Zero)
  255. throw new InvalidOperationException("Packet not created");
  256. }
  257. public void SetFreeCallback(IntPtr callback) {
  258. IsCreated();
  259. Native.enet_packet_set_free_callback(nativePacket, callback);
  260. }
  261. public void SetFreeCallback(PacketFreeCallback callback) {
  262. IsCreated();
  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();
  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();
  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 || length < 0 || length > data.Length)
  294. throw new ArgumentOutOfRangeException();
  295. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  296. }
  297. public void Create(IntPtr data, int offset, int length, PacketFlags flags) {
  298. if (data == IntPtr.Zero)
  299. throw new ArgumentNullException("data");
  300. if (offset < 0 || length < 0)
  301. throw new ArgumentOutOfRangeException();
  302. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  303. }
  304. public void CopyTo(byte[] destination, int startPos = 0) {
  305. if (destination == null)
  306. throw new ArgumentNullException("destination");
  307. // Fix by katori, prevents trying to copy a NULL
  308. // from native world (ie. disconnect a client)
  309. if (Data == null)
  310. {
  311. return;
  312. }
  313. Marshal.Copy(Data, destination, startPos, Length);
  314. }
  315. }
  316. public class Host : IDisposable {
  317. private IntPtr nativeHost;
  318. internal IntPtr NativeData {
  319. get {
  320. return nativeHost;
  321. }
  322. set {
  323. nativeHost = value;
  324. }
  325. }
  326. public void Dispose() {
  327. Dispose(true);
  328. GC.SuppressFinalize(this);
  329. }
  330. protected virtual void Dispose(bool disposing) {
  331. if (nativeHost != IntPtr.Zero) {
  332. Native.enet_host_destroy(nativeHost);
  333. nativeHost = IntPtr.Zero;
  334. }
  335. }
  336. ~Host() {
  337. Dispose(false);
  338. }
  339. public bool IsSet {
  340. get {
  341. return nativeHost != IntPtr.Zero;
  342. }
  343. }
  344. public uint PeersCount {
  345. get {
  346. IsCreated();
  347. return Native.enet_host_get_peers_count(nativeHost);
  348. }
  349. }
  350. public uint PacketsSent {
  351. get {
  352. IsCreated();
  353. return Native.enet_host_get_packets_sent(nativeHost);
  354. }
  355. }
  356. public uint PacketsReceived {
  357. get {
  358. IsCreated();
  359. return Native.enet_host_get_packets_received(nativeHost);
  360. }
  361. }
  362. public uint BytesSent {
  363. get {
  364. IsCreated();
  365. return Native.enet_host_get_bytes_sent(nativeHost);
  366. }
  367. }
  368. public uint BytesReceived {
  369. get {
  370. IsCreated();
  371. return Native.enet_host_get_bytes_received(nativeHost);
  372. }
  373. }
  374. internal void IsCreated() {
  375. if (nativeHost == IntPtr.Zero)
  376. throw new InvalidOperationException("Host not created");
  377. }
  378. private void IsChannelLimited(int channelLimit) {
  379. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  380. throw new ArgumentOutOfRangeException("channelLimit");
  381. }
  382. public void Create() {
  383. Create(null, 1, 0);
  384. }
  385. public void Create(int bufferSize) {
  386. Create(null, 1, 0, 0, 0, bufferSize);
  387. }
  388. public void Create(Address? address, int peerLimit) {
  389. Create(address, peerLimit, 0);
  390. }
  391. public void Create(Address? address, int peerLimit, int channelLimit) {
  392. Create(address, peerLimit, channelLimit, 0, 0, 0);
  393. }
  394. public void Create(int peerLimit, int channelLimit) {
  395. Create(null, peerLimit, channelLimit, 0, 0, 0);
  396. }
  397. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  398. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  399. }
  400. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  401. Create(address, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  402. }
  403. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize) {
  404. if (nativeHost != IntPtr.Zero)
  405. throw new InvalidOperationException("Host already created");
  406. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  407. throw new ArgumentOutOfRangeException("peerLimit");
  408. IsChannelLimited(channelLimit);
  409. if (address != null) {
  410. var nativeAddress = address.Value.NativeData;
  411. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  412. } else {
  413. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  414. }
  415. if (nativeHost == IntPtr.Zero)
  416. throw new InvalidOperationException("Host creation call failed");
  417. }
  418. public void PreventConnections(bool state) {
  419. IsCreated();
  420. Native.enet_host_prevent_connections(nativeHost, (byte)(state ? 1 : 0));
  421. }
  422. public void Broadcast(byte channelID, ref Packet packet) {
  423. IsCreated();
  424. packet.IsCreated();
  425. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  426. packet.NativeData = IntPtr.Zero;
  427. }
  428. public void Broadcast(byte channelID, ref Packet packet, Peer excludedPeer) {
  429. IsCreated();
  430. packet.IsCreated();
  431. Native.enet_host_broadcast_exclude(nativeHost, channelID, packet.NativeData, excludedPeer.NativeData);
  432. packet.NativeData = IntPtr.Zero;
  433. }
  434. public void Broadcast(byte channelID, ref Packet packet, Peer[] peers) {
  435. IsCreated();
  436. packet.IsCreated();
  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. IsCreated();
  452. ENetEvent nativeEvent;
  453. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  454. if (result <= 0) {
  455. @event = default;
  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. IsCreated();
  469. IsChannelLimited(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. IsCreated();
  480. ENetEvent nativeEvent;
  481. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  482. if (result <= 0) {
  483. @event = default;
  484. return result;
  485. }
  486. @event = new Event(nativeEvent);
  487. return result;
  488. }
  489. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  490. IsCreated();
  491. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  492. }
  493. public void SetChannelLimit(int channelLimit) {
  494. IsCreated();
  495. IsChannelLimited(channelLimit);
  496. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  497. }
  498. public void SetInterceptCallback(IntPtr callback)
  499. {
  500. IsCreated();
  501. Native.enet_host_set_intercept_callback(nativeHost, callback);
  502. }
  503. public void SetInterceptCallback(InterceptCallback callback)
  504. {
  505. IsCreated();
  506. Native.enet_host_set_intercept_callback(nativeHost, Marshal.GetFunctionPointerForDelegate(callback));
  507. }
  508. public void Flush() {
  509. IsCreated();
  510. Native.enet_host_flush(nativeHost);
  511. }
  512. }
  513. public struct Peer {
  514. private IntPtr nativePeer;
  515. private uint nativeID;
  516. internal IntPtr NativeData {
  517. get {
  518. return nativePeer;
  519. }
  520. set {
  521. nativePeer = value;
  522. }
  523. }
  524. internal Peer(IntPtr peer) {
  525. nativePeer = peer;
  526. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  527. }
  528. public bool IsSet {
  529. get {
  530. return nativePeer != IntPtr.Zero;
  531. }
  532. }
  533. public uint ID {
  534. get {
  535. return nativeID;
  536. }
  537. }
  538. public string IP {
  539. get {
  540. IsCreated();
  541. byte[] ip = ArrayPool.GetByteBuffer();
  542. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0)
  543. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  544. else
  545. return String.Empty;
  546. }
  547. }
  548. public ushort Port {
  549. get {
  550. IsCreated();
  551. return Native.enet_peer_get_port(nativePeer);
  552. }
  553. }
  554. public uint MTU {
  555. get {
  556. IsCreated();
  557. return Native.enet_peer_get_mtu(nativePeer);
  558. }
  559. }
  560. public PeerState State {
  561. get {
  562. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  563. }
  564. }
  565. public uint RoundTripTime {
  566. get {
  567. IsCreated();
  568. return Native.enet_peer_get_rtt(nativePeer);
  569. }
  570. }
  571. public uint LastRoundTripTime {
  572. get {
  573. IsCreated();
  574. return Native.enet_peer_get_last_rtt(nativePeer);
  575. }
  576. }
  577. public uint LastSendTime {
  578. get {
  579. IsCreated();
  580. return Native.enet_peer_get_lastsendtime(nativePeer);
  581. }
  582. }
  583. public uint LastReceiveTime {
  584. get {
  585. IsCreated();
  586. return Native.enet_peer_get_lastreceivetime(nativePeer);
  587. }
  588. }
  589. public ulong PacketsSent {
  590. get {
  591. IsCreated();
  592. return Native.enet_peer_get_packets_sent(nativePeer);
  593. }
  594. }
  595. public ulong PacketsLost {
  596. get {
  597. IsCreated();
  598. return Native.enet_peer_get_packets_lost(nativePeer);
  599. }
  600. }
  601. public float PacketsThrottle
  602. {
  603. get
  604. {
  605. IsCreated();
  606. return Native.enet_peer_get_packets_throttle(nativePeer);
  607. }
  608. }
  609. public ulong BytesSent {
  610. get {
  611. IsCreated();
  612. return Native.enet_peer_get_bytes_sent(nativePeer);
  613. }
  614. }
  615. public ulong BytesReceived {
  616. get {
  617. IsCreated();
  618. return Native.enet_peer_get_bytes_received(nativePeer);
  619. }
  620. }
  621. public IntPtr Data {
  622. get {
  623. IsCreated();
  624. return Native.enet_peer_get_data(nativePeer);
  625. }
  626. set {
  627. IsCreated();
  628. Native.enet_peer_set_data(nativePeer, value);
  629. }
  630. }
  631. internal void IsCreated() {
  632. if (nativePeer == IntPtr.Zero)
  633. throw new InvalidOperationException("Peer not created");
  634. }
  635. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration, uint threshold) {
  636. IsCreated();
  637. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration, threshold);
  638. }
  639. public bool Send(byte channelID, ref Packet packet) {
  640. IsCreated();
  641. packet.IsCreated();
  642. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  643. }
  644. public bool Receive(out byte channelID, out Packet packet) {
  645. IsCreated();
  646. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  647. if (nativePacket != IntPtr.Zero) {
  648. packet = new Packet(nativePacket);
  649. return true;
  650. }
  651. packet = default;
  652. return false;
  653. }
  654. public void Ping() {
  655. IsCreated();
  656. Native.enet_peer_ping(nativePeer);
  657. }
  658. public void PingInterval(uint interval) {
  659. IsCreated();
  660. Native.enet_peer_ping_interval(nativePeer, interval);
  661. }
  662. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  663. IsCreated();
  664. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  665. }
  666. public void Disconnect(uint data) {
  667. IsCreated();
  668. Native.enet_peer_disconnect(nativePeer, data);
  669. }
  670. public void DisconnectNow(uint data) {
  671. IsCreated();
  672. Native.enet_peer_disconnect_now(nativePeer, data);
  673. }
  674. public void DisconnectLater(uint data) {
  675. IsCreated();
  676. Native.enet_peer_disconnect_later(nativePeer, data);
  677. }
  678. public void Reset() {
  679. IsCreated();
  680. Native.enet_peer_reset(nativePeer);
  681. }
  682. // == ADDITIONS NOT AVAILABLE IN UPSTREAM REPOSITORY == //
  683. // These are placed here to ensure that merge conflicts aren't a
  684. // pain in the ass.
  685. // SendAndReturnStatusCode returns either 0 if the send was successful,
  686. // or the ENET return code if not. Sometimes a bool is not enough to determine
  687. // the root cause of a issue.
  688. public int SendAndReturnStatusCode(byte channelID, ref Packet packet)
  689. {
  690. IsCreated();
  691. packet.IsCreated();
  692. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData);
  693. }
  694. }
  695. public static class Extensions {
  696. public static int StringLength(this byte[] data) {
  697. if (data == null)
  698. throw new ArgumentNullException("data");
  699. int i;
  700. for (i = 0; i < data.Length && data[i] != 0; i++);
  701. return i;
  702. }
  703. }
  704. public static class Library {
  705. public const uint maxChannelCount = 0xFF;
  706. public const uint maxPeers = 0xFFF;
  707. public const uint maxPacketSize = 32 * 1024 * 1024;
  708. public const uint throttleThreshold = 40;
  709. public const uint throttleScale = 32;
  710. public const uint throttleAcceleration = 2;
  711. public const uint throttleDeceleration = 2;
  712. public const uint throttleInterval = 5000;
  713. public const uint timeoutLimit = 32;
  714. public const uint timeoutMinimum = 5000;
  715. public const uint timeoutMaximum = 30000;
  716. // Lock our version to 2.4.0, to avoid confusion with upstream.
  717. public const uint version = (2 << 16) | (4 << 8) | (0);
  718. public static bool Initialize() {
  719. if (Native.enet_linked_version() != version)
  720. throw new InvalidOperationException("You're trying to use an incompatible version of Enet with this Managed Library.");
  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("You're trying to use an incompatible version of Enet with this Managed Library.");
  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 uint Time {
  735. get {
  736. return Native.enet_time_get();
  737. }
  738. }
  739. }
  740. [SuppressUnmanagedCodeSecurity]
  741. internal static class Native {
  742. // This should address Unity usage and bug #66: Platform specific Enet / libenet
  743. // https://github.com/SoftwareGuy/Ignorance/issues/66
  744. #if UNITY_EDITOR
  745. // We are inside the Unity Editor.
  746. #if UNITY_EDITOR_OSX
  747. // Unity Editor on macOS needs to use libenet.
  748. private const string nativeLibrary = "libenet";
  749. #else
  750. // TODO: Check if Linux requires 'libenet' too. (Apparently not?)
  751. private const string nativeLibrary = "enet";
  752. #endif
  753. #endif
  754. #if !UNITY_EDITOR
  755. // We're not inside the Unity Editor.
  756. #if __APPLE__ && !(__IOS__ || UNITY_IOS)
  757. // Use libenet on macOS.
  758. private const string nativeLibrary = "libenet";
  759. #elif __IOS__ || UNITY_IOS
  760. // We're building for a certain mobile fruity OS.
  761. private const string nativeLibrary = "__Internal";
  762. #else
  763. // Assume everything else, Windows et al... TODO: Linux check
  764. private const string nativeLibrary = "enet";
  765. #endif
  766. #endif
  767. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  768. internal static extern int enet_initialize();
  769. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  770. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  771. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  772. internal static extern void enet_deinitialize();
  773. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  774. internal static extern uint enet_linked_version();
  775. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  776. internal static extern uint enet_time_get();
  777. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  778. internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
  779. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  780. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  781. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  782. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  783. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  784. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  785. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  786. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  787. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  788. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  789. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  790. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  791. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  792. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  793. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  794. internal static extern int enet_packet_check_references(IntPtr packet);
  795. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  796. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  797. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  798. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  799. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  800. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  801. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  802. internal static extern int enet_packet_get_length(IntPtr packet);
  803. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  804. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  805. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  806. internal static extern void enet_packet_dispose(IntPtr packet);
  807. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  808. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  809. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  810. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  811. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  812. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  813. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  814. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  815. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  816. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  817. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  818. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  819. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  820. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  821. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  822. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  823. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  824. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  825. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  826. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  827. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  828. internal static extern uint enet_host_get_peers_count(IntPtr host);
  829. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  830. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  831. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  832. internal static extern uint enet_host_get_packets_received(IntPtr host);
  833. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  834. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  835. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  836. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  837. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  838. internal static extern void enet_host_set_intercept_callback(IntPtr host, IntPtr callback);
  839. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  840. internal static extern void enet_host_flush(IntPtr host);
  841. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  842. internal static extern void enet_host_destroy(IntPtr host);
  843. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  844. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  845. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  846. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  847. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  848. internal static extern uint enet_peer_get_id(IntPtr peer);
  849. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  850. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  851. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  852. internal static extern ushort enet_peer_get_port(IntPtr peer);
  853. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  854. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  855. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  856. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  857. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  858. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  859. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  860. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  861. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  862. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  863. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  864. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  865. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  866. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  867. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  868. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  869. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  870. internal static extern float enet_peer_get_packets_throttle(IntPtr peer);
  871. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  872. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  873. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  874. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  875. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  876. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  877. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  878. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  879. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  880. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  881. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  882. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  883. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  884. internal static extern void enet_peer_ping(IntPtr peer);
  885. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  886. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  887. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  888. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  889. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  890. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  891. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  892. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  893. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  894. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  895. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  896. internal static extern void enet_peer_reset(IntPtr peer);
  897. }
  898. }