ENet.cs 29 KB

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