ENet.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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(Address? address, int peerLimit) {
  367. Create(address, peerLimit, 0);
  368. }
  369. public void Create(Address? address, int peerLimit, int channelLimit) {
  370. Create(address, peerLimit, channelLimit, 0, 0);
  371. }
  372. public void Create(int peerLimit, int channelLimit) {
  373. Create(null, peerLimit, channelLimit, 0, 0);
  374. }
  375. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  376. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth);
  377. }
  378. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  379. if (nativeHost != IntPtr.Zero)
  380. throw new InvalidOperationException("Host already created");
  381. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  382. throw new ArgumentOutOfRangeException("peerLimit");
  383. CheckChannelLimit(channelLimit);
  384. if (address != null) {
  385. var nativeAddress = address.Value.NativeData;
  386. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  387. } else {
  388. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth);
  389. }
  390. if (nativeHost == IntPtr.Zero)
  391. throw new InvalidOperationException("Host creation call failed");
  392. }
  393. public void EnableCompression() {
  394. CheckCreated();
  395. Native.enet_host_enable_compression(nativeHost);
  396. }
  397. public void PreventConnections(bool state) {
  398. CheckCreated();
  399. Native.enet_host_prevent_connections(nativeHost, (byte)(state ? 1 : 0));
  400. }
  401. public void Broadcast(byte channelID, ref Packet packet) {
  402. CheckCreated();
  403. packet.CheckCreated();
  404. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  405. packet.NativeData = IntPtr.Zero;
  406. }
  407. public void Broadcast(byte channelID, ref Packet packet, Peer excludedPeer) {
  408. CheckCreated();
  409. packet.CheckCreated();
  410. Native.enet_host_broadcast_excluding(nativeHost, channelID, packet.NativeData, excludedPeer.NativeData);
  411. packet.NativeData = IntPtr.Zero;
  412. }
  413. public void Broadcast(byte channelID, ref Packet packet, Peer[] peers) {
  414. CheckCreated();
  415. packet.CheckCreated();
  416. if (peers.Length > 0) {
  417. IntPtr[] nativePeers = ArrayPool.GetPointerBuffer();
  418. int nativeCount = 0;
  419. for (int i = 0; i < peers.Length; i++) {
  420. if (peers[i].NativeData != IntPtr.Zero) {
  421. nativePeers[nativeCount] = peers[i].NativeData;
  422. nativeCount++;
  423. }
  424. }
  425. Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, nativePeers, (IntPtr)nativeCount);
  426. }
  427. packet.NativeData = IntPtr.Zero;
  428. }
  429. public int CheckEvents(out Event @event) {
  430. CheckCreated();
  431. ENetEvent nativeEvent;
  432. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  433. if (result <= 0) {
  434. @event = new Event();
  435. return result;
  436. }
  437. @event = new Event(nativeEvent);
  438. return result;
  439. }
  440. public Peer Connect(Address address) {
  441. return Connect(address, 0, 0);
  442. }
  443. public Peer Connect(Address address, int channelLimit) {
  444. return Connect(address, channelLimit, 0);
  445. }
  446. public Peer Connect(Address address, int channelLimit, uint data) {
  447. CheckCreated();
  448. CheckChannelLimit(channelLimit);
  449. var nativeAddress = address.NativeData;
  450. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  451. if (peer.NativeData == IntPtr.Zero)
  452. throw new InvalidOperationException("Host connect call failed");
  453. return peer;
  454. }
  455. public int Service(int timeout, out Event @event) {
  456. if (timeout < 0)
  457. throw new ArgumentOutOfRangeException("timeout");
  458. CheckCreated();
  459. ENetEvent nativeEvent;
  460. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  461. if (result <= 0) {
  462. @event = new Event();
  463. return result;
  464. }
  465. @event = new Event(nativeEvent);
  466. return result;
  467. }
  468. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  469. CheckCreated();
  470. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  471. }
  472. public void SetChannelLimit(int channelLimit) {
  473. CheckCreated();
  474. CheckChannelLimit(channelLimit);
  475. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  476. }
  477. public void Flush() {
  478. CheckCreated();
  479. Native.enet_host_flush(nativeHost);
  480. }
  481. }
  482. public struct Peer {
  483. private IntPtr nativePeer;
  484. private uint nativeID;
  485. internal IntPtr NativeData {
  486. get {
  487. return nativePeer;
  488. }
  489. set {
  490. nativePeer = value;
  491. }
  492. }
  493. public Peer(IntPtr peer) {
  494. nativePeer = peer;
  495. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  496. }
  497. public bool IsSet {
  498. get {
  499. return nativePeer != IntPtr.Zero;
  500. }
  501. }
  502. public uint ID {
  503. get {
  504. return nativeID;
  505. }
  506. }
  507. public string IP {
  508. get {
  509. CheckCreated();
  510. byte[] ip = ArrayPool.GetByteBuffer();
  511. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0) {
  512. if (Encoding.ASCII.GetString(ip).Remove(7) != "::ffff:")
  513. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  514. else
  515. return Encoding.ASCII.GetString(ip, 0, ip.StringLength()).Substring(7);
  516. } else {
  517. return String.Empty;
  518. }
  519. }
  520. }
  521. public ushort Port {
  522. get {
  523. CheckCreated();
  524. return Native.enet_peer_get_port(nativePeer);
  525. }
  526. }
  527. public uint MTU {
  528. get {
  529. CheckCreated();
  530. return Native.enet_peer_get_mtu(nativePeer);
  531. }
  532. }
  533. public PeerState State {
  534. get {
  535. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  536. }
  537. }
  538. public uint RoundTripTime {
  539. get {
  540. CheckCreated();
  541. return Native.enet_peer_get_rtt(nativePeer);
  542. }
  543. }
  544. public uint LastSendTime {
  545. get {
  546. CheckCreated();
  547. return Native.enet_peer_get_lastsendtime(nativePeer);
  548. }
  549. }
  550. public uint LastReceiveTime {
  551. get {
  552. CheckCreated();
  553. return Native.enet_peer_get_lastreceivetime(nativePeer);
  554. }
  555. }
  556. public ulong PacketsSent {
  557. get {
  558. CheckCreated();
  559. return Native.enet_peer_get_packets_sent(nativePeer);
  560. }
  561. }
  562. public ulong PacketsLost {
  563. get {
  564. CheckCreated();
  565. return Native.enet_peer_get_packets_lost(nativePeer);
  566. }
  567. }
  568. public ulong BytesSent {
  569. get {
  570. CheckCreated();
  571. return Native.enet_peer_get_bytes_sent(nativePeer);
  572. }
  573. }
  574. public ulong BytesReceived {
  575. get {
  576. CheckCreated();
  577. return Native.enet_peer_get_bytes_received(nativePeer);
  578. }
  579. }
  580. public IntPtr Data {
  581. get {
  582. CheckCreated();
  583. return Native.enet_peer_get_data(nativePeer);
  584. }
  585. set {
  586. CheckCreated();
  587. Native.enet_peer_set_data(nativePeer, value);
  588. }
  589. }
  590. internal void CheckCreated() {
  591. if (nativePeer == IntPtr.Zero)
  592. throw new InvalidOperationException("Peer not created");
  593. }
  594. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration) {
  595. CheckCreated();
  596. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration);
  597. }
  598. public bool Send(byte channelID, ref Packet packet) {
  599. CheckCreated();
  600. packet.CheckCreated();
  601. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  602. }
  603. public bool Receive(out byte channelID, out Packet packet) {
  604. CheckCreated();
  605. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  606. if (nativePacket != IntPtr.Zero) {
  607. packet = new Packet(nativePacket);
  608. return true;
  609. }
  610. packet = default(Packet);
  611. return false;
  612. }
  613. public void Ping() {
  614. CheckCreated();
  615. Native.enet_peer_ping(nativePeer);
  616. }
  617. public void PingInterval(uint interval) {
  618. CheckCreated();
  619. Native.enet_peer_ping_interval(nativePeer, interval);
  620. }
  621. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  622. CheckCreated();
  623. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  624. }
  625. public void Disconnect(uint data) {
  626. CheckCreated();
  627. Native.enet_peer_disconnect(nativePeer, data);
  628. }
  629. public void DisconnectNow(uint data) {
  630. CheckCreated();
  631. Native.enet_peer_disconnect_now(nativePeer, data);
  632. }
  633. public void DisconnectLater(uint data) {
  634. CheckCreated();
  635. Native.enet_peer_disconnect_later(nativePeer, data);
  636. }
  637. public void Reset() {
  638. CheckCreated();
  639. Native.enet_peer_reset(nativePeer);
  640. }
  641. }
  642. public static class Extensions {
  643. public static int StringLength(this byte[] data) {
  644. if (data == null)
  645. throw new ArgumentNullException("data");
  646. int i;
  647. for (i = 0; i < data.Length && data[i] != 0; i++);
  648. return i;
  649. }
  650. }
  651. public static class Library {
  652. public const uint maxChannelCount = 0xFF;
  653. public const uint maxPeers = 0xFFF;
  654. public const uint maxPacketSize = 32 * 1024 * 1024;
  655. public const uint throttleScale = 32;
  656. public const uint throttleAcceleration = 2;
  657. public const uint throttleDeceleration = 2;
  658. public const uint throttleInterval = 5000;
  659. public const uint timeoutLimit = 32;
  660. public const uint timeoutMinimum = 5000;
  661. public const uint timeoutMaximum = 30000;
  662. public const uint version = (2 << 16) | (2 << 8) | (6);
  663. public static bool Initialize() {
  664. return Native.enet_initialize() == 0;
  665. }
  666. public static bool Initialize(Callbacks inits) {
  667. return Native.enet_initialize_with_callbacks(version, inits.NativeData) == 0;
  668. }
  669. public static void Deinitialize() {
  670. Native.enet_deinitialize();
  671. }
  672. public static uint Time {
  673. get {
  674. return Native.enet_time_get();
  675. }
  676. }
  677. }
  678. [SuppressUnmanagedCodeSecurity]
  679. internal static class Native {
  680. #if __IOS__ || UNITY_IOS && !UNITY_EDITOR
  681. private const string nativeLibrary = "__Internal";
  682. #else
  683. private const string nativeLibrary = "enet";
  684. #endif
  685. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  686. internal static extern int enet_initialize();
  687. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  688. internal static extern int enet_initialize_with_callbacks(uint version, ENetCallbacks inits);
  689. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  690. internal static extern void enet_deinitialize();
  691. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  692. internal static extern uint enet_time_get();
  693. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  694. internal static extern int enet_address_set_host_ip(ref ENetAddress address, string ip);
  695. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  696. internal static extern int enet_address_set_host(ref ENetAddress address, string hostName);
  697. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  698. internal static extern int enet_address_get_host_ip(ENetAddress address, StringBuilder ip, IntPtr ipLength);
  699. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  700. internal static extern int enet_address_get_host(ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  701. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  702. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  703. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  704. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  705. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  706. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  707. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  708. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  709. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  710. internal static extern int enet_packet_check_references(IntPtr packet);
  711. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  712. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  713. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  714. internal static extern int enet_packet_get_length(IntPtr packet);
  715. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  716. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  717. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  718. internal static extern void enet_packet_dispose(IntPtr packet);
  719. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  720. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  721. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  722. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  723. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  724. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  725. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  726. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  727. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  728. internal static extern void enet_host_broadcast_excluding(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  729. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  730. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  731. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  732. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  733. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  734. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  735. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  736. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  737. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  738. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  739. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  740. internal static extern uint enet_host_get_peers_count(IntPtr host);
  741. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  742. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  743. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  744. internal static extern uint enet_host_get_packets_received(IntPtr host);
  745. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  746. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  747. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  748. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  749. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  750. internal static extern void enet_host_flush(IntPtr host);
  751. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  752. internal static extern void enet_host_destroy(IntPtr host);
  753. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  754. internal static extern void enet_host_enable_compression(IntPtr host);
  755. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  756. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  757. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  758. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration);
  759. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  760. internal static extern uint enet_peer_get_id(IntPtr peer);
  761. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  762. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  763. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  764. internal static extern ushort enet_peer_get_port(IntPtr peer);
  765. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  766. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  767. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  768. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  769. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  770. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  771. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  772. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  773. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  774. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  775. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  776. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  777. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  778. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  779. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  780. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  781. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  782. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  783. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  784. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  785. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  786. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  787. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  788. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  789. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  790. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  791. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  792. internal static extern void enet_peer_ping(IntPtr peer);
  793. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  794. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  795. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  796. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  797. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  798. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  799. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  800. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  801. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  802. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  803. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  804. internal static extern void enet_peer_reset(IntPtr peer);
  805. }
  806. }