NativeMethods.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Text;
  4. namespace ENet
  5. {
  6. public static class NativeMethods
  7. {
  8. private const string LIB = "ENet.dll";
  9. public const int ENET_PEER_PACKET_THROTTLE_SCALE = 32;
  10. public const int ENET_PEER_PACKET_THROTTLE_ACCELERATION = 2;
  11. public const int ENET_PEER_PACKET_THROTTLE_DECELERATION = 2;
  12. public const int ENET_PEER_PACKET_THROTTLE_INTERVAL = 5000;
  13. public const int ENET_PROTOCOL_MINIMUM_CHANNEL_COUNT = 0x01;
  14. public const int ENET_PROTOCOL_MAXIMUM_CHANNEL_COUNT = 0xff;
  15. public const int ENET_PROTOCOL_MAXIMUM_PEER_ID = 0xfff;
  16. public const uint ENET_VERSION = (1 << 16) | (3 << 8) | (1);
  17. public const uint ENET_HOST_ANY = 0;
  18. public const uint ENET_HOST_BROADCAST = 0xffffffff;
  19. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  20. internal static extern int enet_address_set_host(ref ENetAddress address, string hostName);
  21. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  22. internal static extern int enet_address_get_host(ref ENetAddress address, StringBuilder hostName, uint nameLength);
  23. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  24. internal static extern int enet_address_get_host_ip(ref ENetAddress address, StringBuilder hostIp, uint ipLength);
  25. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  26. internal static extern void enet_deinitialize();
  27. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  28. internal static extern int enet_initialize();
  29. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  30. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  31. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  32. internal static extern void enet_enable_crc(IntPtr host);
  33. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  34. internal static extern int enet_host_compress_with_range_encoder(IntPtr host);
  35. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  36. internal static extern IntPtr enet_host_create(
  37. ref ENetAddress address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  38. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  39. internal static extern IntPtr enet_host_create(
  40. IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth, uint outgoingBandwidth);
  41. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  42. internal static extern void enet_host_destroy(IntPtr host);
  43. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  44. internal static extern IntPtr enet_host_connect(IntPtr host, ref ENetAddress address, uint channelCount, uint data);
  45. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  46. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  47. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  48. internal static extern void enet_host_compress(IntPtr host, IntPtr compressor);
  49. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  50. internal static extern void enet_host_channel_limit(IntPtr host, uint channelLimit);
  51. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  52. internal static extern void enet_host_bandwidth_limit(IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  53. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  54. internal static extern void enet_host_flush(IntPtr host);
  55. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  56. internal static extern int enet_host_check_events(IntPtr host, ENetEvent ev);
  57. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  58. internal static extern int enet_host_service(IntPtr host, ENetEvent ev, uint timeout);
  59. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  60. internal static extern uint enet_time_get();
  61. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  62. internal static extern void enet_time_set(uint newTimeBase);
  63. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  64. internal static extern IntPtr enet_packet_create(byte[] data, uint dataLength, PacketFlags flags);
  65. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  66. internal static extern void enet_packet_destroy(IntPtr packet);
  67. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  68. internal static extern int enet_packet_resize(IntPtr packet, uint dataLength);
  69. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  70. internal static extern void enet_peer_throttle_configure(
  71. IntPtr peer, uint interval, uint acceleration, uint deceleration);
  72. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  73. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  74. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  75. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  76. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  77. internal static extern void enet_peer_reset(IntPtr peer);
  78. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  79. internal static extern void enet_peer_ping(IntPtr peer);
  80. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  81. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  82. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  83. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  84. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  85. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  86. public static bool memcmp(byte[] s1, byte[] s2)
  87. {
  88. if (s1 == null || s2 == null)
  89. {
  90. throw new ArgumentNullException();
  91. }
  92. if (s1.Length != s2.Length)
  93. {
  94. return false;
  95. }
  96. for (int i = 0; i < s1.Length; i++)
  97. {
  98. if (s1[i] != s2[i])
  99. {
  100. return false;
  101. }
  102. }
  103. return true;
  104. }
  105. public static int strlen(byte[] s)
  106. {
  107. if (s == null)
  108. {
  109. throw new ArgumentNullException();
  110. }
  111. int i;
  112. for (i = 0; i < s.Length && s[i] != 0; i++)
  113. {
  114. ;
  115. }
  116. return i;
  117. }
  118. }
  119. }