ENet.cs 31 KB

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