Native.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #region License
  2. /*
  3. ENet for C#
  4. Copyright (c) 2011 James F. Bellinger <jfb@zer7.com>
  5. Permission to use, copy, modify, and/or distribute this software for any
  6. purpose with or without fee is hereby granted, provided that the above
  7. copyright notice and this permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #endregion
  17. using System;
  18. using System.Runtime.InteropServices;
  19. namespace ENet
  20. {
  21. public static unsafe partial class Native
  22. {
  23. private const string LIB = "ENet.dll";
  24. public const int ENET_PEER_PACKET_THROTTLE_SCALE = 32;
  25. public const int ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2;
  26. public const int ENET_PEER_PACKET_THROTTLE_DECELERATION = 2;
  27. public const int ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000;
  28. public const int ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 0x01;
  29. public const int ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 0xff;
  30. public const int ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xfff;
  31. public const uint ENET_VERSION = (1 << 16) | (3 << 8) | (1);
  32. public const uint ENET_HOST_ANY = 0;
  33. public const uint ENET_HOST_BROADCAST = 0xffffffff;
  34. #region Address Functions
  35. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  36. public static extern int enet_address_set_host(ref ENetAddress address, byte* hostName);
  37. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  38. public static extern int enet_address_set_host(ref ENetAddress address, byte[] hostName);
  39. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  40. public static extern int enet_address_get_host(ref ENetAddress address, byte* hostName, IntPtr nameLength);
  41. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  42. public static extern int enet_address_get_host(ref ENetAddress address, byte[] hostName, IntPtr nameLength);
  43. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  44. public static extern int enet_address_get_host_ip(ref ENetAddress address, byte* hostIP, IntPtr ipLength);
  45. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  46. public static extern int enet_address_get_host_ip(ref ENetAddress address, byte[] hostIP, IntPtr ipLength);
  47. #endregion
  48. #region Global Functions
  49. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  50. public static extern void enet_deinitialize();
  51. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  52. public static extern int enet_initialize();
  53. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  54. public static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  55. #endregion
  56. #region Host Functions
  57. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  58. public static extern int enet_host_compress_with_range_encoder(ENetHost* host);
  59. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  60. public static extern ENetHost* enet_host_create(
  61. ENetAddress* address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  62. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  63. public static extern ENetHost* enet_host_create(
  64. ref ENetAddress address, IntPtr peerLimit, IntPtr channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  65. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  66. public static extern void enet_host_destroy(ENetHost* host);
  67. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  68. public static extern ENetPeer* enet_host_connect(
  69. ENetHost* host, ref ENetAddress address, IntPtr channelCount, uint data);
  70. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  71. public static extern void enet_host_broadcast(ENetHost* host, byte channelID, ENetPacket* packet);
  72. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  73. public static extern void enet_host_compress(ENetHost* host, ENetCompressor* compressor);
  74. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  75. public static extern void enet_host_channel_limit(ENetHost* host, IntPtr channelLimit);
  76. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  77. public static extern void enet_host_bandwidth_limit(ENetHost* host, uint incomingBandwidth, uint outgoingBandwidth);
  78. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  79. public static extern void enet_host_flush(ENetHost* host);
  80. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  81. public static extern int enet_host_check_events(ENetHost* host, out ENetEvent @event);
  82. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  83. public static extern int enet_host_service(ENetHost* host, ENetEvent* @event, uint timeout);
  84. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  85. public static extern int enet_host_service(ENetHost* host, out ENetEvent @event, uint timeout);
  86. #endregion
  87. #region Miscellaneous Functions
  88. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  89. public static extern uint enet_time_get();
  90. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  91. public static extern void enet_time_set(uint newTimeBase);
  92. #endregion
  93. #region Packet Functions
  94. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  95. public static extern ENetPacket* enet_packet_create(void* data, IntPtr dataLength, PacketFlags flags);
  96. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  97. public static extern void enet_packet_destroy(ENetPacket* packet);
  98. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  99. public static extern int enet_packet_resize(ENetPacket* packet, IntPtr dataLength);
  100. #endregion
  101. #region Peer Functions
  102. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  103. public static extern void enet_peer_throttle_configure(
  104. ENetPeer* peer, uint interval, uint acceleration, uint deceleration);
  105. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  106. public static extern int enet_peer_send(ENetPeer* peer, byte channelID, ENetPacket* packet);
  107. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  108. public static extern ENetPacket* enet_peer_receive(ENetPeer* peer, out byte channelID);
  109. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  110. public static extern void enet_peer_reset(ENetPeer* peer);
  111. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  112. public static extern void enet_peer_ping(ENetPeer* peer);
  113. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  114. public static extern void enet_peer_disconnect_now(ENetPeer* peer, uint data);
  115. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  116. public static extern void enet_peer_disconnect(ENetPeer* peer, uint data);
  117. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  118. public static extern void enet_peer_disconnect_later(ENetPeer* peer, uint data);
  119. #endregion
  120. #region C# Utility
  121. public static bool memcmp(byte[] s1, byte[] s2)
  122. {
  123. if (s1 == null || s2 == null)
  124. {
  125. throw new ArgumentNullException();
  126. }
  127. if (s1.Length != s2.Length)
  128. {
  129. return false;
  130. }
  131. for (int i = 0; i < s1.Length; i++)
  132. {
  133. if (s1[i] != s2[i])
  134. {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. public static int strlen(byte[] s)
  141. {
  142. if (s == null)
  143. {
  144. throw new ArgumentNullException();
  145. }
  146. int i;
  147. for (i = 0; i < s.Length && s[i] != 0; i++)
  148. {
  149. ;
  150. }
  151. return i;
  152. }
  153. #endregion
  154. }
  155. }