ENet.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. {
  32. [Flags]
  33. public enum PacketFlags
  34. {
  35. None = 0,
  36. Reliable = 1 << 0,
  37. Unsequenced = 1 << 1,
  38. NoAllocate = 1 << 2,
  39. UnreliableFragmented = 1 << 3,
  40. Instant = 1 << 4,
  41. Unthrottled = 1 << 5,
  42. Sent = 1 << 8
  43. }
  44. public enum EventType
  45. {
  46. None = 0,
  47. Connect = 1,
  48. Disconnect = 2,
  49. Receive = 3,
  50. Timeout = 4
  51. }
  52. public enum PeerState
  53. {
  54. Uninitialized = -1,
  55. Disconnected = 0,
  56. Connecting = 1,
  57. AcknowledgingConnect = 2,
  58. ConnectionPending = 3,
  59. ConnectionSucceeded = 4,
  60. Connected = 5,
  61. DisconnectLater = 6,
  62. Disconnecting = 7,
  63. AcknowledgingDisconnect = 8,
  64. Zombie = 9
  65. }
  66. [StructLayout(LayoutKind.Explicit, Size = 18)]
  67. internal struct ENetAddress
  68. {
  69. [FieldOffset(16)]
  70. public ushort port;
  71. }
  72. [StructLayout(LayoutKind.Sequential)]
  73. internal struct ENetEvent
  74. {
  75. public EventType type;
  76. public IntPtr peer;
  77. public byte channelID;
  78. public uint data;
  79. public IntPtr packet;
  80. }
  81. [StructLayout(LayoutKind.Sequential)]
  82. internal struct ENetCallbacks
  83. {
  84. public AllocCallback malloc;
  85. public FreeCallback free;
  86. public NoMemoryCallback noMemory;
  87. }
  88. public delegate IntPtr AllocCallback(IntPtr size);
  89. public delegate void FreeCallback(IntPtr memory);
  90. public delegate void NoMemoryCallback();
  91. public delegate void PacketFreeCallback(Packet packet);
  92. public delegate int InterceptCallback(ref Event @event, ref Address address, IntPtr receivedData, int receivedDataLength);
  93. public delegate ulong ChecksumCallback(IntPtr buffers, int bufferCount);
  94. internal static class ArrayPool
  95. {
  96. [ThreadStatic]
  97. private static byte[] byteBuffer;
  98. [ThreadStatic]
  99. private static IntPtr[] pointerBuffer;
  100. public static byte[] GetByteBuffer()
  101. {
  102. if (byteBuffer == null)
  103. byteBuffer = new byte[64];
  104. return byteBuffer;
  105. }
  106. public static IntPtr[] GetPointerBuffer()
  107. {
  108. if (pointerBuffer == null)
  109. pointerBuffer = new IntPtr[Library.maxPeers];
  110. return pointerBuffer;
  111. }
  112. }
  113. public struct Address
  114. {
  115. private ENetAddress nativeAddress;
  116. internal ENetAddress NativeData
  117. {
  118. get
  119. {
  120. return nativeAddress;
  121. }
  122. set
  123. {
  124. nativeAddress = value;
  125. }
  126. }
  127. internal Address(ENetAddress address)
  128. {
  129. nativeAddress = address;
  130. }
  131. public ushort Port
  132. {
  133. get
  134. {
  135. return nativeAddress.port;
  136. }
  137. set
  138. {
  139. nativeAddress.port = value;
  140. }
  141. }
  142. public string GetIP()
  143. {
  144. StringBuilder ip = new StringBuilder(1025);
  145. if (Native.enet_address_get_ip(ref nativeAddress, ip, (IntPtr)ip.Capacity) != 0)
  146. return String.Empty;
  147. return ip.ToString();
  148. }
  149. public bool SetIP(string ip)
  150. {
  151. if (ip == null)
  152. throw new ArgumentNullException("ip");
  153. return Native.enet_address_set_ip(ref nativeAddress, ip) == 0;
  154. }
  155. public string GetHost()
  156. {
  157. StringBuilder hostName = new StringBuilder(1025);
  158. if (Native.enet_address_get_hostname(ref nativeAddress, hostName, (IntPtr)hostName.Capacity) != 0)
  159. return String.Empty;
  160. return hostName.ToString();
  161. }
  162. public bool SetHost(string hostName)
  163. {
  164. if (hostName == null)
  165. throw new ArgumentNullException("hostName");
  166. return Native.enet_address_set_hostname(ref nativeAddress, hostName) == 0;
  167. }
  168. }
  169. public struct Event
  170. {
  171. private ENetEvent nativeEvent;
  172. internal ENetEvent NativeData
  173. {
  174. get
  175. {
  176. return nativeEvent;
  177. }
  178. set
  179. {
  180. nativeEvent = value;
  181. }
  182. }
  183. internal Event(ENetEvent @event)
  184. {
  185. nativeEvent = @event;
  186. }
  187. public EventType Type
  188. {
  189. get
  190. {
  191. return nativeEvent.type;
  192. }
  193. }
  194. public Peer Peer
  195. {
  196. get
  197. {
  198. return new Peer(nativeEvent.peer);
  199. }
  200. }
  201. public byte ChannelID
  202. {
  203. get
  204. {
  205. return nativeEvent.channelID;
  206. }
  207. }
  208. public uint Data
  209. {
  210. get
  211. {
  212. return nativeEvent.data;
  213. }
  214. }
  215. public Packet Packet
  216. {
  217. get
  218. {
  219. return new Packet(nativeEvent.packet);
  220. }
  221. }
  222. }
  223. public class Callbacks
  224. {
  225. private ENetCallbacks nativeCallbacks;
  226. internal ENetCallbacks NativeData
  227. {
  228. get
  229. {
  230. return nativeCallbacks;
  231. }
  232. set
  233. {
  234. nativeCallbacks = value;
  235. }
  236. }
  237. public Callbacks(AllocCallback allocCallback, FreeCallback freeCallback, NoMemoryCallback noMemoryCallback)
  238. {
  239. nativeCallbacks.malloc = allocCallback;
  240. nativeCallbacks.free = freeCallback;
  241. nativeCallbacks.noMemory = noMemoryCallback;
  242. }
  243. }
  244. public struct Packet : IDisposable
  245. {
  246. private IntPtr nativePacket;
  247. internal IntPtr NativeData
  248. {
  249. get
  250. {
  251. return nativePacket;
  252. }
  253. set
  254. {
  255. nativePacket = value;
  256. }
  257. }
  258. internal Packet(IntPtr packet)
  259. {
  260. nativePacket = packet;
  261. }
  262. public void Dispose()
  263. {
  264. if (nativePacket != IntPtr.Zero)
  265. {
  266. Native.enet_packet_dispose(nativePacket);
  267. nativePacket = IntPtr.Zero;
  268. }
  269. }
  270. public bool IsSet
  271. {
  272. get
  273. {
  274. return nativePacket != IntPtr.Zero;
  275. }
  276. }
  277. public IntPtr Data
  278. {
  279. get
  280. {
  281. ThrowIfNotCreated();
  282. return Native.enet_packet_get_data(nativePacket);
  283. }
  284. }
  285. public IntPtr UserData
  286. {
  287. get
  288. {
  289. ThrowIfNotCreated();
  290. return Native.enet_packet_get_user_data(nativePacket);
  291. }
  292. set
  293. {
  294. ThrowIfNotCreated();
  295. Native.enet_packet_set_user_data(nativePacket, value);
  296. }
  297. }
  298. public int Length
  299. {
  300. get
  301. {
  302. ThrowIfNotCreated();
  303. return Native.enet_packet_get_length(nativePacket);
  304. }
  305. }
  306. public bool HasReferences
  307. {
  308. get
  309. {
  310. ThrowIfNotCreated();
  311. return Native.enet_packet_check_references(nativePacket) != 0;
  312. }
  313. }
  314. internal void ThrowIfNotCreated()
  315. {
  316. if (nativePacket == IntPtr.Zero)
  317. throw new InvalidOperationException("Packet not created");
  318. }
  319. public void SetFreeCallback(IntPtr callback)
  320. {
  321. ThrowIfNotCreated();
  322. Native.enet_packet_set_free_callback(nativePacket, callback);
  323. }
  324. public void SetFreeCallback(PacketFreeCallback callback)
  325. {
  326. ThrowIfNotCreated();
  327. Native.enet_packet_set_free_callback(nativePacket, Marshal.GetFunctionPointerForDelegate(callback));
  328. }
  329. public void Create(byte[] data)
  330. {
  331. if (data == null)
  332. throw new ArgumentNullException("data");
  333. Create(data, data.Length);
  334. }
  335. public void Create(byte[] data, int length)
  336. {
  337. Create(data, length, PacketFlags.None);
  338. }
  339. public void Create(byte[] data, PacketFlags flags)
  340. {
  341. Create(data, data.Length, flags);
  342. }
  343. public void Create(byte[] data, int length, PacketFlags flags)
  344. {
  345. if (data == null)
  346. throw new ArgumentNullException("data");
  347. if (length < 0 || length > data.Length)
  348. throw new ArgumentOutOfRangeException("length");
  349. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  350. }
  351. public void Create(IntPtr data, int length, PacketFlags flags)
  352. {
  353. if (data == IntPtr.Zero)
  354. throw new ArgumentNullException("data");
  355. if (length < 0)
  356. throw new ArgumentOutOfRangeException("length");
  357. nativePacket = Native.enet_packet_create(data, (IntPtr)length, flags);
  358. }
  359. public void Create(byte[] data, int offset, int length, PacketFlags flags)
  360. {
  361. if (data == null)
  362. throw new ArgumentNullException("data");
  363. if (offset < 0)
  364. throw new ArgumentOutOfRangeException("offset");
  365. if (length < 0 || length > data.Length)
  366. throw new ArgumentOutOfRangeException("length");
  367. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  368. }
  369. public void Create(IntPtr data, int offset, int length, PacketFlags flags)
  370. {
  371. if (data == IntPtr.Zero)
  372. throw new ArgumentNullException("data");
  373. if (offset < 0)
  374. throw new ArgumentOutOfRangeException("offset");
  375. if (length < 0)
  376. throw new ArgumentOutOfRangeException("length");
  377. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  378. }
  379. public void CopyTo(byte[] destination, int startPos = 0)
  380. {
  381. if (destination == null)
  382. throw new ArgumentNullException("destination");
  383. // Fix by katori, prevents trying to copy a NULL
  384. // from native world (ie. disconnect a client)
  385. if (Data == null)
  386. {
  387. return;
  388. }
  389. Marshal.Copy(Data, destination, startPos, Length);
  390. }
  391. }
  392. public class Host : IDisposable
  393. {
  394. private IntPtr nativeHost;
  395. internal IntPtr NativeData
  396. {
  397. get
  398. {
  399. return nativeHost;
  400. }
  401. set
  402. {
  403. nativeHost = value;
  404. }
  405. }
  406. public void Dispose()
  407. {
  408. Dispose(true);
  409. GC.SuppressFinalize(this);
  410. }
  411. protected virtual void Dispose(bool disposing)
  412. {
  413. if (nativeHost != IntPtr.Zero)
  414. {
  415. Native.enet_host_destroy(nativeHost);
  416. nativeHost = IntPtr.Zero;
  417. }
  418. }
  419. ~Host()
  420. {
  421. Dispose(false);
  422. }
  423. public bool IsSet
  424. {
  425. get
  426. {
  427. return nativeHost != IntPtr.Zero;
  428. }
  429. }
  430. public uint PeersCount
  431. {
  432. get
  433. {
  434. ThrowIfNotCreated();
  435. return Native.enet_host_get_peers_count(nativeHost);
  436. }
  437. }
  438. public uint PacketsSent
  439. {
  440. get
  441. {
  442. ThrowIfNotCreated();
  443. return Native.enet_host_get_packets_sent(nativeHost);
  444. }
  445. }
  446. public uint PacketsReceived
  447. {
  448. get
  449. {
  450. ThrowIfNotCreated();
  451. return Native.enet_host_get_packets_received(nativeHost);
  452. }
  453. }
  454. public uint BytesSent
  455. {
  456. get
  457. {
  458. ThrowIfNotCreated();
  459. return Native.enet_host_get_bytes_sent(nativeHost);
  460. }
  461. }
  462. public uint BytesReceived
  463. {
  464. get
  465. {
  466. ThrowIfNotCreated();
  467. return Native.enet_host_get_bytes_received(nativeHost);
  468. }
  469. }
  470. internal void ThrowIfNotCreated()
  471. {
  472. if (nativeHost == IntPtr.Zero)
  473. throw new InvalidOperationException("Host not created");
  474. }
  475. private static void ThrowIfChannelsExceeded(int channelLimit)
  476. {
  477. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  478. throw new ArgumentOutOfRangeException("channelLimit");
  479. }
  480. public void Create()
  481. {
  482. Create(null, 1, 0);
  483. }
  484. public void Create(int bufferSize)
  485. {
  486. Create(null, 1, 0, 0, 0, bufferSize);
  487. }
  488. public void Create(Address? address, int peerLimit)
  489. {
  490. Create(address, peerLimit, 0);
  491. }
  492. public void Create(Address? address, int peerLimit, int channelLimit)
  493. {
  494. Create(address, peerLimit, channelLimit, 0, 0, 0);
  495. }
  496. public void Create(int peerLimit, int channelLimit)
  497. {
  498. Create(null, peerLimit, channelLimit, 0, 0, 0);
  499. }
  500. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
  501. {
  502. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  503. }
  504. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth)
  505. {
  506. Create(address, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  507. }
  508. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize)
  509. {
  510. if (nativeHost != IntPtr.Zero)
  511. throw new InvalidOperationException("Host already created");
  512. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  513. throw new ArgumentOutOfRangeException("peerLimit");
  514. ThrowIfChannelsExceeded(channelLimit);
  515. if (address != null)
  516. {
  517. var nativeAddress = address.Value.NativeData;
  518. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  519. }
  520. else
  521. {
  522. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  523. }
  524. if (nativeHost == IntPtr.Zero)
  525. throw new InvalidOperationException("Host creation call failed");
  526. }
  527. public void PreventConnections(bool state)
  528. {
  529. ThrowIfNotCreated();
  530. Native.enet_host_prevent_connections(nativeHost, (byte)(state ? 1 : 0));
  531. }
  532. public void Broadcast(byte channelID, ref Packet packet)
  533. {
  534. ThrowIfNotCreated();
  535. packet.ThrowIfNotCreated();
  536. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  537. packet.NativeData = IntPtr.Zero;
  538. }
  539. public void Broadcast(byte channelID, ref Packet packet, Peer excludedPeer)
  540. {
  541. ThrowIfNotCreated();
  542. packet.ThrowIfNotCreated();
  543. Native.enet_host_broadcast_exclude(nativeHost, channelID, packet.NativeData, excludedPeer.NativeData);
  544. packet.NativeData = IntPtr.Zero;
  545. }
  546. public void Broadcast(byte channelID, ref Packet packet, Peer[] peers)
  547. {
  548. if (peers == null)
  549. throw new ArgumentNullException("peers");
  550. ThrowIfNotCreated();
  551. packet.ThrowIfNotCreated();
  552. if (peers.Length > 0)
  553. {
  554. IntPtr[] nativePeers = ArrayPool.GetPointerBuffer();
  555. int nativeCount = 0;
  556. for (int i = 0; i < peers.Length; i++)
  557. {
  558. if (peers[i].NativeData != IntPtr.Zero)
  559. {
  560. nativePeers[nativeCount] = peers[i].NativeData;
  561. nativeCount++;
  562. }
  563. }
  564. Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, nativePeers, (IntPtr)nativeCount);
  565. }
  566. packet.NativeData = IntPtr.Zero;
  567. }
  568. public int CheckEvents(out Event @event)
  569. {
  570. ThrowIfNotCreated();
  571. ENetEvent nativeEvent;
  572. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  573. if (result <= 0)
  574. {
  575. @event = default(Event);
  576. return result;
  577. }
  578. @event = new Event(nativeEvent);
  579. return result;
  580. }
  581. public Peer Connect(Address address)
  582. {
  583. return Connect(address, 0, 0);
  584. }
  585. public Peer Connect(Address address, int channelLimit)
  586. {
  587. return Connect(address, channelLimit, 0);
  588. }
  589. public Peer Connect(Address address, int channelLimit, uint data)
  590. {
  591. ThrowIfNotCreated();
  592. ThrowIfChannelsExceeded(channelLimit);
  593. var nativeAddress = address.NativeData;
  594. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  595. if (peer.NativeData == IntPtr.Zero)
  596. throw new InvalidOperationException("Host connect call failed");
  597. return peer;
  598. }
  599. public int Service(int timeout, out Event @event)
  600. {
  601. if (timeout < 0)
  602. throw new ArgumentOutOfRangeException("timeout");
  603. ThrowIfNotCreated();
  604. ENetEvent nativeEvent;
  605. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  606. if (result <= 0)
  607. {
  608. @event = default(Event);
  609. return result;
  610. }
  611. @event = new Event(nativeEvent);
  612. return result;
  613. }
  614. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth)
  615. {
  616. ThrowIfNotCreated();
  617. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  618. }
  619. public void SetChannelLimit(int channelLimit)
  620. {
  621. ThrowIfNotCreated();
  622. ThrowIfChannelsExceeded(channelLimit);
  623. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  624. }
  625. public void SetMaxDuplicatePeers(ushort number)
  626. {
  627. ThrowIfNotCreated();
  628. Native.enet_host_set_max_duplicate_peers(nativeHost, number);
  629. }
  630. public void SetInterceptCallback(IntPtr callback)
  631. {
  632. ThrowIfNotCreated();
  633. Native.enet_host_set_intercept_callback(nativeHost, callback);
  634. }
  635. public void SetInterceptCallback(InterceptCallback callback)
  636. {
  637. ThrowIfNotCreated();
  638. Native.enet_host_set_intercept_callback(nativeHost, Marshal.GetFunctionPointerForDelegate(callback));
  639. }
  640. public void SetChecksumCallback(IntPtr callback)
  641. {
  642. ThrowIfNotCreated();
  643. Native.enet_host_set_checksum_callback(nativeHost, callback);
  644. }
  645. public void SetChecksumCallback(ChecksumCallback callback)
  646. {
  647. ThrowIfNotCreated();
  648. Native.enet_host_set_checksum_callback(nativeHost, Marshal.GetFunctionPointerForDelegate(callback));
  649. }
  650. public void Flush()
  651. {
  652. ThrowIfNotCreated();
  653. Native.enet_host_flush(nativeHost);
  654. }
  655. }
  656. public struct Peer
  657. {
  658. private IntPtr nativePeer;
  659. private uint nativeID;
  660. internal IntPtr NativeData
  661. {
  662. get
  663. {
  664. return nativePeer;
  665. }
  666. set
  667. {
  668. nativePeer = value;
  669. }
  670. }
  671. internal Peer(IntPtr peer)
  672. {
  673. nativePeer = peer;
  674. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  675. }
  676. public bool IsSet
  677. {
  678. get
  679. {
  680. return nativePeer != IntPtr.Zero;
  681. }
  682. }
  683. public uint ID
  684. {
  685. get
  686. {
  687. return nativeID;
  688. }
  689. }
  690. public string IP
  691. {
  692. get
  693. {
  694. ThrowIfNotCreated();
  695. byte[] ip = ArrayPool.GetByteBuffer();
  696. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0)
  697. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  698. else
  699. return String.Empty;
  700. }
  701. }
  702. public ushort Port
  703. {
  704. get
  705. {
  706. ThrowIfNotCreated();
  707. return Native.enet_peer_get_port(nativePeer);
  708. }
  709. }
  710. public uint MTU
  711. {
  712. get
  713. {
  714. ThrowIfNotCreated();
  715. return Native.enet_peer_get_mtu(nativePeer);
  716. }
  717. }
  718. public PeerState State
  719. {
  720. get
  721. {
  722. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  723. }
  724. }
  725. public uint RoundTripTime
  726. {
  727. get
  728. {
  729. ThrowIfNotCreated();
  730. return Native.enet_peer_get_rtt(nativePeer);
  731. }
  732. }
  733. public uint LastRoundTripTime
  734. {
  735. get
  736. {
  737. ThrowIfNotCreated();
  738. return Native.enet_peer_get_last_rtt(nativePeer);
  739. }
  740. }
  741. public uint LastSendTime
  742. {
  743. get
  744. {
  745. ThrowIfNotCreated();
  746. return Native.enet_peer_get_lastsendtime(nativePeer);
  747. }
  748. }
  749. public uint LastReceiveTime
  750. {
  751. get
  752. {
  753. ThrowIfNotCreated();
  754. return Native.enet_peer_get_lastreceivetime(nativePeer);
  755. }
  756. }
  757. public ulong PacketsSent
  758. {
  759. get
  760. {
  761. ThrowIfNotCreated();
  762. return Native.enet_peer_get_packets_sent(nativePeer);
  763. }
  764. }
  765. public ulong PacketsLost
  766. {
  767. get
  768. {
  769. ThrowIfNotCreated();
  770. return Native.enet_peer_get_packets_lost(nativePeer);
  771. }
  772. }
  773. public float PacketsThrottle
  774. {
  775. get
  776. {
  777. ThrowIfNotCreated();
  778. return Native.enet_peer_get_packets_throttle(nativePeer);
  779. }
  780. }
  781. public ulong BytesSent
  782. {
  783. get
  784. {
  785. ThrowIfNotCreated();
  786. return Native.enet_peer_get_bytes_sent(nativePeer);
  787. }
  788. }
  789. public ulong BytesReceived
  790. {
  791. get
  792. {
  793. ThrowIfNotCreated();
  794. return Native.enet_peer_get_bytes_received(nativePeer);
  795. }
  796. }
  797. public IntPtr Data
  798. {
  799. get
  800. {
  801. ThrowIfNotCreated();
  802. return Native.enet_peer_get_data(nativePeer);
  803. }
  804. set
  805. {
  806. ThrowIfNotCreated();
  807. Native.enet_peer_set_data(nativePeer, value);
  808. }
  809. }
  810. internal void ThrowIfNotCreated()
  811. {
  812. if (nativePeer == IntPtr.Zero)
  813. throw new InvalidOperationException("Peer not created");
  814. }
  815. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration, uint threshold)
  816. {
  817. ThrowIfNotCreated();
  818. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration, threshold);
  819. }
  820. public int Send(byte channelID, ref Packet packet)
  821. {
  822. ThrowIfNotCreated();
  823. packet.ThrowIfNotCreated();
  824. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData);
  825. }
  826. public bool Receive(out byte channelID, out Packet packet)
  827. {
  828. ThrowIfNotCreated();
  829. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  830. if (nativePacket != IntPtr.Zero)
  831. {
  832. packet = new Packet(nativePacket);
  833. return true;
  834. }
  835. packet = default(Packet);
  836. return false;
  837. }
  838. public void Ping()
  839. {
  840. ThrowIfNotCreated();
  841. Native.enet_peer_ping(nativePeer);
  842. }
  843. public void PingInterval(uint interval)
  844. {
  845. ThrowIfNotCreated();
  846. Native.enet_peer_ping_interval(nativePeer, interval);
  847. }
  848. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum)
  849. {
  850. ThrowIfNotCreated();
  851. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  852. }
  853. public void Disconnect(uint data)
  854. {
  855. ThrowIfNotCreated();
  856. Native.enet_peer_disconnect(nativePeer, data);
  857. }
  858. public void DisconnectNow(uint data)
  859. {
  860. ThrowIfNotCreated();
  861. Native.enet_peer_disconnect_now(nativePeer, data);
  862. }
  863. public void DisconnectLater(uint data)
  864. {
  865. ThrowIfNotCreated();
  866. Native.enet_peer_disconnect_later(nativePeer, data);
  867. }
  868. public void Reset()
  869. {
  870. ThrowIfNotCreated();
  871. Native.enet_peer_reset(nativePeer);
  872. }
  873. }
  874. public static class Extensions
  875. {
  876. public static int StringLength(this byte[] data)
  877. {
  878. if (data == null)
  879. throw new ArgumentNullException("data");
  880. int i;
  881. for (i = 0; i < data.Length && data[i] != 0; i++) ;
  882. return i;
  883. }
  884. }
  885. public static class Library
  886. {
  887. public const uint maxChannelCount = 0xFF;
  888. public const uint maxPeers = 0xFFF;
  889. public const uint maxPacketSize = 32 * 1024 * 1024;
  890. public const uint throttleThreshold = 40;
  891. public const uint throttleScale = 32;
  892. public const uint throttleAcceleration = 2;
  893. public const uint throttleDeceleration = 2;
  894. public const uint throttleInterval = 5000;
  895. public const uint timeoutLimit = 32;
  896. public const uint timeoutMinimum = 5000;
  897. public const uint timeoutMaximum = 30000;
  898. public const uint version = (2 << 16) | (4 << 8) | (7);
  899. public static uint Time
  900. {
  901. get
  902. {
  903. return Native.enet_time_get();
  904. }
  905. }
  906. public static bool Initialize()
  907. {
  908. if (Native.enet_linked_version() != version)
  909. throw new InvalidOperationException("ENet native is out of date. Download the latest release from https://github.com/SoftwareGuy/ENet-CSharp/releases");
  910. return Native.enet_initialize() == 0;
  911. }
  912. public static bool Initialize(Callbacks callbacks)
  913. {
  914. if (callbacks == null)
  915. throw new ArgumentNullException("callbacks");
  916. if (Native.enet_linked_version() != version)
  917. throw new InvalidOperationException("ENet native is out of date. Download the latest release from https://github.com/SoftwareGuy/ENet-CSharp/releases");
  918. ENetCallbacks nativeCallbacks = callbacks.NativeData;
  919. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
  920. }
  921. public static void Deinitialize()
  922. {
  923. Native.enet_deinitialize();
  924. }
  925. public static ulong CRC64(IntPtr buffers, int bufferCount)
  926. {
  927. return Native.enet_crc64(buffers, bufferCount);
  928. }
  929. }
  930. [SuppressUnmanagedCodeSecurity]
  931. internal static class Native
  932. {
  933. // This should address Unity usage and bug #66: Platform specific Enet / libenet
  934. // https://github.com/SoftwareGuy/Ignorance/issues/66
  935. #region Editor Specific Native Library Names
  936. #if UNITY_EDITOR
  937. #if UNITY_EDITOR_OSX
  938. // Unity Editor on macOS needs to use libenet.
  939. private const string nativeLibrary = "libenet";
  940. #else
  941. // All other platforms should be using "enet".
  942. private const string nativeLibrary = "enet";
  943. #endif
  944. #endif
  945. #endregion
  946. #region Standalone Specific Native Library Names
  947. #if !UNITY_EDITOR
  948. #if __APPLE__ && !(__IOS__ || UNITY_IOS)
  949. // Use libenet on macOS.
  950. private const string nativeLibrary = "libenet";
  951. #elif __IOS__ || UNITY_IOS
  952. // We're building for a certain mobile fruity OS.
  953. private const string nativeLibrary = "__Internal";
  954. #else
  955. // Assume everything else, Windows et al.
  956. private const string nativeLibrary = "enet";
  957. #endif
  958. #endif
  959. #endregion
  960. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  961. internal static extern int enet_initialize();
  962. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  963. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  964. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  965. internal static extern void enet_deinitialize();
  966. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  967. internal static extern uint enet_linked_version();
  968. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  969. internal static extern uint enet_time_get();
  970. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  971. internal static extern ulong enet_crc64(IntPtr buffers, int bufferCount);
  972. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  973. internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
  974. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  975. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  976. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  977. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  978. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  979. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  980. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  981. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  982. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  983. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  984. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  985. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  986. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  987. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  988. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  989. internal static extern int enet_packet_check_references(IntPtr packet);
  990. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  991. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  992. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  993. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  994. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  995. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  996. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  997. internal static extern int enet_packet_get_length(IntPtr packet);
  998. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  999. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  1000. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1001. internal static extern void enet_packet_dispose(IntPtr packet);
  1002. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1003. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  1004. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1005. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  1006. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1007. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  1008. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1009. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  1010. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1011. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  1012. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1013. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  1014. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1015. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  1016. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1017. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  1018. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1019. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  1020. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1021. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  1022. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1023. internal static extern uint enet_host_get_peers_count(IntPtr host);
  1024. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1025. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  1026. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1027. internal static extern uint enet_host_get_packets_received(IntPtr host);
  1028. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1029. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  1030. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1031. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  1032. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1033. internal static extern void enet_host_set_max_duplicate_peers(IntPtr host, ushort number);
  1034. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1035. internal static extern void enet_host_set_intercept_callback(IntPtr host, IntPtr callback);
  1036. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1037. internal static extern void enet_host_set_checksum_callback(IntPtr host, IntPtr callback);
  1038. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1039. internal static extern void enet_host_flush(IntPtr host);
  1040. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1041. internal static extern void enet_host_destroy(IntPtr host);
  1042. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1043. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  1044. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1045. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  1046. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1047. internal static extern uint enet_peer_get_id(IntPtr peer);
  1048. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1049. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  1050. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1051. internal static extern ushort enet_peer_get_port(IntPtr peer);
  1052. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1053. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  1054. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1055. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  1056. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1057. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  1058. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1059. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  1060. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1061. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  1062. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1063. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  1064. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1065. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  1066. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1067. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  1068. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1069. internal static extern float enet_peer_get_packets_throttle(IntPtr peer);
  1070. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1071. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  1072. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1073. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  1074. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1075. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  1076. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1077. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  1078. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1079. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  1080. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1081. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  1082. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1083. internal static extern void enet_peer_ping(IntPtr peer);
  1084. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1085. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  1086. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1087. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  1088. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1089. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  1090. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1091. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  1092. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1093. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  1094. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  1095. internal static extern void enet_peer_reset(IntPtr peer);
  1096. #if UNITY_EDITOR
  1097. public static string nativeLibraryName { get { return nativeLibrary; } }
  1098. #endif
  1099. }
  1100. }