ENet.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. IsCreated();
  226. return Native.enet_packet_get_data(nativePacket);
  227. }
  228. }
  229. public IntPtr UserData {
  230. get {
  231. IsCreated();
  232. return Native.enet_packet_get_user_data(nativePacket);
  233. }
  234. set {
  235. IsCreated();
  236. Native.enet_packet_set_user_data(nativePacket, value);
  237. }
  238. }
  239. public int Length {
  240. get {
  241. IsCreated();
  242. return Native.enet_packet_get_length(nativePacket);
  243. }
  244. }
  245. public bool HasReferences {
  246. get {
  247. IsCreated();
  248. return Native.enet_packet_check_references(nativePacket) != 0;
  249. }
  250. }
  251. internal void IsCreated() {
  252. if (nativePacket == IntPtr.Zero)
  253. throw new InvalidOperationException("Packet not created");
  254. }
  255. public void SetFreeCallback(IntPtr callback) {
  256. IsCreated();
  257. Native.enet_packet_set_free_callback(nativePacket, callback);
  258. }
  259. public void SetFreeCallback(PacketFreeCallback callback) {
  260. IsCreated();
  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("length");
  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("length");
  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)
  292. throw new ArgumentOutOfRangeException("offset");
  293. if (length < 0 || length > data.Length)
  294. throw new ArgumentOutOfRangeException("length");
  295. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  296. }
  297. public void Create(IntPtr data, int offset, int length, PacketFlags flags) {
  298. if (data == IntPtr.Zero)
  299. throw new ArgumentNullException("data");
  300. if (offset < 0)
  301. throw new ArgumentOutOfRangeException("offset");
  302. if (length < 0)
  303. throw new ArgumentOutOfRangeException("length");
  304. nativePacket = Native.enet_packet_create_offset(data, (IntPtr)length, (IntPtr)offset, flags);
  305. }
  306. public void CopyTo(byte[] destination) {
  307. if (destination == null)
  308. throw new ArgumentNullException("destination");
  309. Marshal.Copy(Data, destination, 0, Length);
  310. }
  311. }
  312. public class Host : IDisposable {
  313. private IntPtr nativeHost;
  314. internal IntPtr NativeData {
  315. get {
  316. return nativeHost;
  317. }
  318. set {
  319. nativeHost = value;
  320. }
  321. }
  322. public void Dispose() {
  323. Dispose(true);
  324. GC.SuppressFinalize(this);
  325. }
  326. protected virtual void Dispose(bool disposing) {
  327. if (nativeHost != IntPtr.Zero) {
  328. Native.enet_host_destroy(nativeHost);
  329. nativeHost = IntPtr.Zero;
  330. }
  331. }
  332. ~Host() {
  333. Dispose(false);
  334. }
  335. public bool IsSet {
  336. get {
  337. return nativeHost != IntPtr.Zero;
  338. }
  339. }
  340. public uint PeersCount {
  341. get {
  342. IsCreated();
  343. return Native.enet_host_get_peers_count(nativeHost);
  344. }
  345. }
  346. public uint PacketsSent {
  347. get {
  348. IsCreated();
  349. return Native.enet_host_get_packets_sent(nativeHost);
  350. }
  351. }
  352. public uint PacketsReceived {
  353. get {
  354. IsCreated();
  355. return Native.enet_host_get_packets_received(nativeHost);
  356. }
  357. }
  358. public uint BytesSent {
  359. get {
  360. IsCreated();
  361. return Native.enet_host_get_bytes_sent(nativeHost);
  362. }
  363. }
  364. public uint BytesReceived {
  365. get {
  366. IsCreated();
  367. return Native.enet_host_get_bytes_received(nativeHost);
  368. }
  369. }
  370. internal void IsCreated() {
  371. if (nativeHost == IntPtr.Zero)
  372. throw new InvalidOperationException("Host not created");
  373. }
  374. private static void IsChannelLimited(int channelLimit) {
  375. if (channelLimit < 0 || channelLimit > Library.maxChannelCount)
  376. throw new ArgumentOutOfRangeException("channelLimit");
  377. }
  378. public void Create() {
  379. Create(null, 1, 0);
  380. }
  381. public void Create(int bufferSize) {
  382. Create(null, 1, 0, 0, 0, bufferSize);
  383. }
  384. public void Create(Address? address, int peerLimit) {
  385. Create(address, peerLimit, 0);
  386. }
  387. public void Create(Address? address, int peerLimit, int channelLimit) {
  388. Create(address, peerLimit, channelLimit, 0, 0, 0);
  389. }
  390. public void Create(int peerLimit, int channelLimit) {
  391. Create(null, peerLimit, channelLimit, 0, 0, 0);
  392. }
  393. public void Create(int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  394. Create(null, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  395. }
  396. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth) {
  397. Create(address, peerLimit, channelLimit, incomingBandwidth, outgoingBandwidth, 0);
  398. }
  399. public void Create(Address? address, int peerLimit, int channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize) {
  400. if (nativeHost != IntPtr.Zero)
  401. throw new InvalidOperationException("Host already created");
  402. if (peerLimit < 0 || peerLimit > Library.maxPeers)
  403. throw new ArgumentOutOfRangeException("peerLimit");
  404. IsChannelLimited(channelLimit);
  405. if (address != null) {
  406. var nativeAddress = address.Value.NativeData;
  407. nativeHost = Native.enet_host_create(ref nativeAddress, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  408. } else {
  409. nativeHost = Native.enet_host_create(IntPtr.Zero, (IntPtr)peerLimit, (IntPtr)channelLimit, incomingBandwidth, outgoingBandwidth, bufferSize);
  410. }
  411. if (nativeHost == IntPtr.Zero)
  412. throw new InvalidOperationException("Host creation call failed");
  413. }
  414. public void PreventConnections(bool state) {
  415. IsCreated();
  416. Native.enet_host_prevent_connections(nativeHost, (byte)(state ? 1 : 0));
  417. }
  418. public void Broadcast(byte channelID, ref Packet packet) {
  419. IsCreated();
  420. packet.IsCreated();
  421. Native.enet_host_broadcast(nativeHost, channelID, packet.NativeData);
  422. packet.NativeData = IntPtr.Zero;
  423. }
  424. public void Broadcast(byte channelID, ref Packet packet, Peer excludedPeer) {
  425. IsCreated();
  426. packet.IsCreated();
  427. Native.enet_host_broadcast_exclude(nativeHost, channelID, packet.NativeData, excludedPeer.NativeData);
  428. packet.NativeData = IntPtr.Zero;
  429. }
  430. public void Broadcast(byte channelID, ref Packet packet, Peer[] peers) {
  431. if (peers == null)
  432. throw new ArgumentNullException("peers");
  433. IsCreated();
  434. packet.IsCreated();
  435. if (peers.Length > 0) {
  436. IntPtr[] nativePeers = ArrayPool.GetPointerBuffer();
  437. int nativeCount = 0;
  438. for (int i = 0; i < peers.Length; i++) {
  439. if (peers[i].NativeData != IntPtr.Zero) {
  440. nativePeers[nativeCount] = peers[i].NativeData;
  441. nativeCount++;
  442. }
  443. }
  444. Native.enet_host_broadcast_selective(nativeHost, channelID, packet.NativeData, nativePeers, (IntPtr)nativeCount);
  445. }
  446. packet.NativeData = IntPtr.Zero;
  447. }
  448. public int CheckEvents(out Event @event) {
  449. IsCreated();
  450. ENetEvent nativeEvent;
  451. var result = Native.enet_host_check_events(nativeHost, out nativeEvent);
  452. if (result <= 0) {
  453. @event = default(Event);
  454. return result;
  455. }
  456. @event = new Event(nativeEvent);
  457. return result;
  458. }
  459. public Peer Connect(Address address) {
  460. return Connect(address, 0, 0);
  461. }
  462. public Peer Connect(Address address, int channelLimit) {
  463. return Connect(address, channelLimit, 0);
  464. }
  465. public Peer Connect(Address address, int channelLimit, uint data) {
  466. IsCreated();
  467. IsChannelLimited(channelLimit);
  468. var nativeAddress = address.NativeData;
  469. var peer = new Peer(Native.enet_host_connect(nativeHost, ref nativeAddress, (IntPtr)channelLimit, data));
  470. if (peer.NativeData == IntPtr.Zero)
  471. throw new InvalidOperationException("Host connect call failed");
  472. return peer;
  473. }
  474. public int Service(int timeout, out Event @event) {
  475. if (timeout < 0)
  476. throw new ArgumentOutOfRangeException("timeout");
  477. IsCreated();
  478. ENetEvent nativeEvent;
  479. var result = Native.enet_host_service(nativeHost, out nativeEvent, (uint)timeout);
  480. if (result <= 0) {
  481. @event = default(Event);
  482. return result;
  483. }
  484. @event = new Event(nativeEvent);
  485. return result;
  486. }
  487. public void SetBandwidthLimit(uint incomingBandwidth, uint outgoingBandwidth) {
  488. IsCreated();
  489. Native.enet_host_bandwidth_limit(nativeHost, incomingBandwidth, outgoingBandwidth);
  490. }
  491. public void SetChannelLimit(int channelLimit) {
  492. IsCreated();
  493. IsChannelLimited(channelLimit);
  494. Native.enet_host_channel_limit(nativeHost, (IntPtr)channelLimit);
  495. }
  496. public void Flush() {
  497. IsCreated();
  498. Native.enet_host_flush(nativeHost);
  499. }
  500. }
  501. public struct Peer {
  502. private IntPtr nativePeer;
  503. private uint nativeID;
  504. internal IntPtr NativeData {
  505. get {
  506. return nativePeer;
  507. }
  508. set {
  509. nativePeer = value;
  510. }
  511. }
  512. internal Peer(IntPtr peer) {
  513. nativePeer = peer;
  514. nativeID = nativePeer != IntPtr.Zero ? Native.enet_peer_get_id(nativePeer) : 0;
  515. }
  516. public bool IsSet {
  517. get {
  518. return nativePeer != IntPtr.Zero;
  519. }
  520. }
  521. public uint ID {
  522. get {
  523. return nativeID;
  524. }
  525. }
  526. public string IP {
  527. get {
  528. IsCreated();
  529. byte[] ip = ArrayPool.GetByteBuffer();
  530. if (Native.enet_peer_get_ip(nativePeer, ip, (IntPtr)ip.Length) == 0)
  531. return Encoding.ASCII.GetString(ip, 0, ip.StringLength());
  532. else
  533. return String.Empty;
  534. }
  535. }
  536. public ushort Port {
  537. get {
  538. IsCreated();
  539. return Native.enet_peer_get_port(nativePeer);
  540. }
  541. }
  542. public uint MTU {
  543. get {
  544. IsCreated();
  545. return Native.enet_peer_get_mtu(nativePeer);
  546. }
  547. }
  548. public PeerState State {
  549. get {
  550. return nativePeer == IntPtr.Zero ? PeerState.Uninitialized : Native.enet_peer_get_state(nativePeer);
  551. }
  552. }
  553. public uint RoundTripTime {
  554. get {
  555. IsCreated();
  556. return Native.enet_peer_get_rtt(nativePeer);
  557. }
  558. }
  559. public uint LastRoundTripTime {
  560. get {
  561. IsCreated();
  562. return Native.enet_peer_get_last_rtt(nativePeer);
  563. }
  564. }
  565. public uint LastSendTime {
  566. get {
  567. IsCreated();
  568. return Native.enet_peer_get_lastsendtime(nativePeer);
  569. }
  570. }
  571. public uint LastReceiveTime {
  572. get {
  573. IsCreated();
  574. return Native.enet_peer_get_lastreceivetime(nativePeer);
  575. }
  576. }
  577. public ulong PacketsSent {
  578. get {
  579. IsCreated();
  580. return Native.enet_peer_get_packets_sent(nativePeer);
  581. }
  582. }
  583. public ulong PacketsLost {
  584. get {
  585. IsCreated();
  586. return Native.enet_peer_get_packets_lost(nativePeer);
  587. }
  588. }
  589. public ulong BytesSent {
  590. get {
  591. IsCreated();
  592. return Native.enet_peer_get_bytes_sent(nativePeer);
  593. }
  594. }
  595. public ulong BytesReceived {
  596. get {
  597. IsCreated();
  598. return Native.enet_peer_get_bytes_received(nativePeer);
  599. }
  600. }
  601. public IntPtr Data {
  602. get {
  603. IsCreated();
  604. return Native.enet_peer_get_data(nativePeer);
  605. }
  606. set {
  607. IsCreated();
  608. Native.enet_peer_set_data(nativePeer, value);
  609. }
  610. }
  611. internal void IsCreated() {
  612. if (nativePeer == IntPtr.Zero)
  613. throw new InvalidOperationException("Peer not created");
  614. }
  615. public void ConfigureThrottle(uint interval, uint acceleration, uint deceleration, uint threshold) {
  616. IsCreated();
  617. Native.enet_peer_throttle_configure(nativePeer, interval, acceleration, deceleration, threshold);
  618. }
  619. public bool Send(byte channelID, ref Packet packet) {
  620. IsCreated();
  621. packet.IsCreated();
  622. return Native.enet_peer_send(nativePeer, channelID, packet.NativeData) == 0;
  623. }
  624. public bool Receive(out byte channelID, out Packet packet) {
  625. IsCreated();
  626. IntPtr nativePacket = Native.enet_peer_receive(nativePeer, out channelID);
  627. if (nativePacket != IntPtr.Zero) {
  628. packet = new Packet(nativePacket);
  629. return true;
  630. }
  631. packet = default(Packet);
  632. return false;
  633. }
  634. public void Ping() {
  635. IsCreated();
  636. Native.enet_peer_ping(nativePeer);
  637. }
  638. public void PingInterval(uint interval) {
  639. IsCreated();
  640. Native.enet_peer_ping_interval(nativePeer, interval);
  641. }
  642. public void Timeout(uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum) {
  643. IsCreated();
  644. Native.enet_peer_timeout(nativePeer, timeoutLimit, timeoutMinimum, timeoutMaximum);
  645. }
  646. public void Disconnect(uint data) {
  647. IsCreated();
  648. Native.enet_peer_disconnect(nativePeer, data);
  649. }
  650. public void DisconnectNow(uint data) {
  651. IsCreated();
  652. Native.enet_peer_disconnect_now(nativePeer, data);
  653. }
  654. public void DisconnectLater(uint data) {
  655. IsCreated();
  656. Native.enet_peer_disconnect_later(nativePeer, data);
  657. }
  658. public void Reset() {
  659. IsCreated();
  660. Native.enet_peer_reset(nativePeer);
  661. }
  662. }
  663. public static class Extensions {
  664. public static int StringLength(this byte[] data) {
  665. if (data == null)
  666. throw new ArgumentNullException("data");
  667. int i;
  668. for (i = 0; i < data.Length && data[i] != 0; i++);
  669. return i;
  670. }
  671. }
  672. public static class Library {
  673. public const uint maxChannelCount = 0xFF;
  674. public const uint maxPeers = 0xFFF;
  675. public const uint maxPacketSize = 32 * 1024 * 1024;
  676. public const uint throttleThreshold = 20;
  677. public const uint throttleScale = 32;
  678. public const uint throttleAcceleration = 2;
  679. public const uint throttleDeceleration = 2;
  680. public const uint throttleInterval = 5000;
  681. public const uint timeoutLimit = 32;
  682. public const uint timeoutMinimum = 5000;
  683. public const uint timeoutMaximum = 30000;
  684. public const uint version = (2 << 16) | (3 << 8) | (7);
  685. public static bool Initialize() {
  686. return Native.enet_initialize() == 0;
  687. }
  688. public static bool Initialize(Callbacks callbacks) {
  689. if (callbacks == null)
  690. throw new ArgumentNullException("callbacks");
  691. ENetCallbacks nativeCallbacks = callbacks.NativeData;
  692. return Native.enet_initialize_with_callbacks(version, ref nativeCallbacks) == 0;
  693. }
  694. public static void Deinitialize() {
  695. Native.enet_deinitialize();
  696. }
  697. public static uint Time {
  698. get {
  699. return Native.enet_time_get();
  700. }
  701. }
  702. }
  703. [SuppressUnmanagedCodeSecurity]
  704. internal static class Native {
  705. #if __IOS__ || UNITY_IOS && !UNITY_EDITOR
  706. private const string nativeLibrary = "__Internal";
  707. #else
  708. private const string nativeLibrary = "enet";
  709. #endif
  710. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  711. internal static extern int enet_initialize();
  712. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  713. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  714. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  715. internal static extern void enet_deinitialize();
  716. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  717. internal static extern uint enet_time_get();
  718. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  719. internal static extern int enet_address_set_ip(ref ENetAddress address, string ip);
  720. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  721. internal static extern int enet_address_set_hostname(ref ENetAddress address, string hostName);
  722. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  723. internal static extern int enet_address_get_ip(ref ENetAddress address, StringBuilder ip, IntPtr ipLength);
  724. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  725. internal static extern int enet_address_get_hostname(ref ENetAddress address, StringBuilder hostName, IntPtr nameLength);
  726. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  727. internal static extern IntPtr enet_packet_create(byte[] data, IntPtr dataLength, PacketFlags flags);
  728. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  729. internal static extern IntPtr enet_packet_create(IntPtr data, IntPtr dataLength, PacketFlags flags);
  730. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  731. internal static extern IntPtr enet_packet_create_offset(byte[] data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  732. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  733. internal static extern IntPtr enet_packet_create_offset(IntPtr data, IntPtr dataLength, IntPtr dataOffset, PacketFlags flags);
  734. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  735. internal static extern int enet_packet_check_references(IntPtr packet);
  736. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  737. internal static extern IntPtr enet_packet_get_data(IntPtr packet);
  738. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  739. internal static extern IntPtr enet_packet_get_user_data(IntPtr packet);
  740. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  741. internal static extern IntPtr enet_packet_set_user_data(IntPtr packet, IntPtr userData);
  742. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  743. internal static extern int enet_packet_get_length(IntPtr packet);
  744. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  745. internal static extern void enet_packet_set_free_callback(IntPtr packet, IntPtr callback);
  746. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  747. internal static extern void enet_packet_dispose(IntPtr packet);
  748. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  749. internal static extern IntPtr enet_host_create(ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  750. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  751. internal static extern IntPtr enet_host_create(IntPtr address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth, int bufferSize);
  752. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  753. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, IntPtr channelCount, uint data);
  754. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  755. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  756. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  757. internal static extern void enet_host_broadcast_exclude(IntPtr host, byte channelID, IntPtr packet, IntPtr excludedPeer);
  758. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  759. internal static extern void enet_host_broadcast_selective(IntPtr host, byte channelID, IntPtr packet, IntPtr[] peers, IntPtr peersLength);
  760. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  761. internal static extern int enet_host_service(IntPtr host, out ENetEvent @event, uint timeout);
  762. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  763. internal static extern int enet_host_check_events(IntPtr host, out ENetEvent @event);
  764. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  765. internal static extern void enet_host_channel_limit(IntPtr host, IntPtr channelLimit);
  766. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  767. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  768. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  769. internal static extern uint enet_host_get_peers_count(IntPtr host);
  770. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  771. internal static extern uint enet_host_get_packets_sent(IntPtr host);
  772. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  773. internal static extern uint enet_host_get_packets_received(IntPtr host);
  774. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  775. internal static extern uint enet_host_get_bytes_sent(IntPtr host);
  776. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  777. internal static extern uint enet_host_get_bytes_received(IntPtr host);
  778. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  779. internal static extern void enet_host_flush(IntPtr host);
  780. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  781. internal static extern void enet_host_destroy(IntPtr host);
  782. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  783. internal static extern void enet_host_prevent_connections(IntPtr host, byte state);
  784. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  785. internal static extern void enet_peer_throttle_configure(IntPtr peer, uint interval, uint acceleration, uint deceleration, uint threshold);
  786. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  787. internal static extern uint enet_peer_get_id(IntPtr peer);
  788. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  789. internal static extern int enet_peer_get_ip(IntPtr peer, byte[] ip, IntPtr ipLength);
  790. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  791. internal static extern ushort enet_peer_get_port(IntPtr peer);
  792. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  793. internal static extern uint enet_peer_get_mtu(IntPtr peer);
  794. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  795. internal static extern PeerState enet_peer_get_state(IntPtr peer);
  796. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  797. internal static extern uint enet_peer_get_rtt(IntPtr peer);
  798. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  799. internal static extern uint enet_peer_get_last_rtt(IntPtr peer);
  800. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  801. internal static extern uint enet_peer_get_lastsendtime(IntPtr peer);
  802. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  803. internal static extern uint enet_peer_get_lastreceivetime(IntPtr peer);
  804. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  805. internal static extern ulong enet_peer_get_packets_sent(IntPtr peer);
  806. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  807. internal static extern ulong enet_peer_get_packets_lost(IntPtr peer);
  808. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  809. internal static extern ulong enet_peer_get_bytes_sent(IntPtr peer);
  810. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  811. internal static extern ulong enet_peer_get_bytes_received(IntPtr peer);
  812. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  813. internal static extern IntPtr enet_peer_get_data(IntPtr peer);
  814. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  815. internal static extern void enet_peer_set_data(IntPtr peer, IntPtr data);
  816. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  817. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  818. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  819. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  820. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  821. internal static extern void enet_peer_ping(IntPtr peer);
  822. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  823. internal static extern void enet_peer_ping_interval(IntPtr peer, uint pingInterval);
  824. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  825. internal static extern void enet_peer_timeout(IntPtr peer, uint timeoutLimit, uint timeoutMinimum, uint timeoutMaximum);
  826. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  827. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  828. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  829. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  830. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  831. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  832. [DllImport(nativeLibrary, CallingConvention = CallingConvention.Cdecl)]
  833. internal static extern void enet_peer_reset(IntPtr peer);
  834. }
  835. }