ENet.cs 30 KB

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