ENet.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 LastRoundTripTime {
  554. get {
  555. CheckCreated();
  556. return Native.enet_peer_get_last_rtt(nativePeer);
  557. }
  558. }
  559. public uint LastSendTime {
  560. get {
  561. CheckCreated();
  562. return Native.enet_peer_get_lastsendtime(nativePeer);
  563. }
  564. }
  565. public uint LastReceiveTime {
  566. get {
  567. CheckCreated();
  568. return Native.enet_peer_get_lastreceivetime(nativePeer);
  569. }
  570. }
  571. public ulong PacketsSent {
  572. get {
  573. CheckCreated();
  574. return Native.enet_peer_get_packets_sent(nativePeer);
  575. }
  576. }
  577. public ulong PacketsLost {
  578. get {
  579. CheckCreated();
  580. return Native.enet_peer_get_packets_lost(nativePeer);
  581. }
  582. }
  583. public ulong BytesSent {
  584. get {
  585. CheckCreated();
  586. return Native.enet_peer_get_bytes_sent(nativePeer);
  587. }
  588. }
  589. public ulong BytesReceived {
  590. get {
  591. CheckCreated();
  592. return Native.enet_peer_get_bytes_received(nativePeer);
  593. }
  594. }
  595. public IntPtr Data {
  596. get {
  597. CheckCreated();
  598. return Native.enet_peer_get_data(nativePeer);
  599. }
  600. set {
  601. CheckCreated();
  602. Native.enet_peer_set_data(nativePeer, value);
  603. }
  604. }
  605. internal void CheckCreated() {
  606. if (nativePeer == IntPtr.Zero)
  607. throw new InvalidOperationException("Peer not created");
  608. }
  609. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration, uint threshold) {
  610. CheckCreated();
  611. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration, threshold);
  612. }
  613. public bool Send(byte channelID, ref Packet packet) {
  614. CheckCreated();
  615. packet.CheckCreated();
  616. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  617. }
  618. public bool Receive(out byte channelID, out Packet packet) {
  619. CheckCreated();
  620. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  621. if (nativePacket != IntPtr.Zero) {
  622. packet = new Packet(nativePacket);
  623. return true;
  624. }
  625. packet = default(Packet);
  626. return false;
  627. }
  628. public void Ping() {
  629. CheckCreated();
  630. Native.enet_peer_ping(nativePeer);
  631. }
  632. public void PingInterval(uint interval) {
  633. CheckCreated();
  634. Native.enet_peer_ping_interval(nativePeer, interval);
  635. }
  636. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  637. CheckCreated();
  638. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  639. }
  640. public void Disconnect(uint data) {
  641. CheckCreated();
  642. Native.enet_peer_disconnect(nativePeer, data);
  643. }
  644. public void DisconnectNow(uint data) {
  645. CheckCreated();
  646. Native.enet_peer_disconnect_now(nativePeer, data);
  647. }
  648. public void DisconnectLater(uint data) {
  649. CheckCreated();
  650. Native.enet_peer_disconnect_later(nativePeer, data);
  651. }
  652. public void Reset() {
  653. CheckCreated();
  654. Native.enet_peer_reset(nativePeer);
  655. }
  656. }
  657. public static class Extensions {
  658. public static int StringLength(this byte[] data) {
  659. if (data == null)
  660. throw new ArgumentNullException("data");
  661. int i;
  662. for (i = 0; i < data.Length && data[i] != 0; i++);
  663. return i;
  664. }
  665. }
  666. public static class Library {
  667. public const uint maxChannelCount = 0xFF;
  668. public const uint maxPeers = 0xFFF;
  669. public const uint maxPacketSize = 32 * 1024 * 1024;
  670. public const uint throttleThreshold = 20;
  671. public const uint throttleScale = 32;
  672. public const uint throttleAcceleration = 2;
  673. public const uint throttleDeceleration = 2;
  674. public const uint throttleInterval = 5000;
  675. public const uint timeoutLimit = 32;
  676. public const uint timeoutMinimum = 5000;
  677. public const uint timeoutMaximum = 30000;
  678. public const uint version = (2 << 16) | (3 << 8) | (7);
  679. public static bool Initialize() {
  680. return Native.enet_initialize() == 0;
  681. }
  682. public static bool Initialize(Callbacks callbacks) {
  683. ENetCallbacks nativeCallbacks = callbacks.NativeData;
  684. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
  685. }
  686. public static void Deinitialize() {
  687. Native.enet_deinitialize();
  688. }
  689. public static uint Time {
  690. get {
  691. return Native.enet_time_get();
  692. }
  693. }
  694. }
  695. [SuppressUnmanagedCodeSecurity]
  696. internal static class Native {
  697. #if __IOS__ || UNITY_IOS && !UNITY_EDITOR
  698. private const string nativeLibrary = "__Internal";
  699. #else
  700. private const string nativeLibrary = "enet";
  701. #endif
  702. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  703. internal static extern int enet_initialize();
  704. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  705. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  706. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  707. internal static extern void enet_deinitialize();
  708. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  709. internal static extern uint enet_time_get();
  710. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  711. internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
  712. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  713. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  714. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  715. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  716. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  717. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  718. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  719. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  720. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  721. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  722. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  723. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  724. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  725. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  726. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  727. internal static extern int enet_packet_check_references(IntPtr packet);
  728. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  729. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  730. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  731. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  732. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  733. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  734. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  735. internal static extern int enet_packet_get_length(IntPtr packet);
  736. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  737. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  738. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  739. internal static extern void enet_packet_dispose(IntPtr packet);
  740. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  741. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  742. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  743. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  744. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  745. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  746. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  747. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  748. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  749. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  750. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  751. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  752. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  753. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  754. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  755. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  756. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  757. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  758. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  759. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  760. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  761. internal static extern uint enet_host_get_peers_count(IntPtr host);
  762. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  763. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  764. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  765. internal static extern uint enet_host_get_packets_received(IntPtr host);
  766. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  767. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  768. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  769. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  770. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  771. internal static extern void enet_host_flush(IntPtr host);
  772. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  773. internal static extern void enet_host_destroy(IntPtr host);
  774. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  775. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  776. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  777. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  778. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  779. internal static extern uint enet_peer_get_id(IntPtr peer);
  780. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  781. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  782. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  783. internal static extern ushort enet_peer_get_port(IntPtr peer);
  784. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  785. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  786. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  787. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  788. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  789. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  790. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  791. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  792. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  793. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  794. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  795. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  796. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  797. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  798. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  799. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  800. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  801. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  802. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  803. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  804. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  805. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  806. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  807. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  808. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  809. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  810. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  811. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  812. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  813. internal static extern void enet_peer_ping(IntPtr peer);
  814. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  815. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  816. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  817. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  818. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  819. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  820. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  821. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  822. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  823. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  824. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  825. internal static extern void enet_peer_reset(IntPtr peer);
  826. }
  827. }