NativeMethods.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 = "ENetCpp.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(
  23. ref ENetAddress address, StringBuilder hostName, uint nameLength);
  24. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  25. internal static extern int enet_address_get_host_ip(
  26. ref ENetAddress address, StringBuilder hostIp, uint ipLength);
  27. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  28. internal static extern void enet_deinitialize();
  29. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  30. internal static extern int enet_initialize();
  31. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  32. internal static extern int enet_initialize_with_callbacks(uint version, ref ENetCallbacks inits);
  33. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  34. internal static extern void enet_enable_crc(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,
  38. uint outgoingBandwidth);
  39. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  40. internal static extern IntPtr enet_host_create(
  41. IntPtr address, uint peerLimit, uint channelLimit, uint incomingBandwidth,
  42. uint outgoingBandwidth);
  43. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  44. internal static extern void enet_host_destroy(IntPtr host);
  45. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  46. internal static extern IntPtr enet_host_connect(
  47. IntPtr host, ref ENetAddress address, uint channelCount, uint data);
  48. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  49. internal static extern void enet_host_broadcast(IntPtr host, byte channelID, IntPtr packet);
  50. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  51. internal static extern void enet_host_compress(IntPtr host, IntPtr compressor);
  52. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  53. internal static extern int enet_host_compress_with_range_coder(IntPtr host);
  54. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  55. internal static extern void enet_host_channel_limit(IntPtr host, uint channelLimit);
  56. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  57. internal static extern void enet_host_bandwidth_limit(
  58. IntPtr host, uint incomingBandwidth, uint outgoingBandwidth);
  59. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  60. internal static extern void enet_host_flush(IntPtr host);
  61. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  62. internal static extern int enet_host_check_events(IntPtr host, ENetEvent ev);
  63. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  64. internal static extern int enet_host_service(IntPtr host, ENetEvent ev, uint timeout);
  65. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  66. internal static extern uint enet_time_get();
  67. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  68. internal static extern void enet_time_set(uint newTimeBase);
  69. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  70. internal static extern IntPtr enet_packet_create(
  71. byte[] data, uint dataLength, PacketFlags flags);
  72. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  73. internal static extern void enet_packet_destroy(IntPtr packet);
  74. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  75. internal static extern int enet_packet_resize(IntPtr packet, uint dataLength);
  76. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  77. internal static extern void enet_peer_throttle_configure(
  78. IntPtr peer, uint interval, uint acceleration, uint deceleration);
  79. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  80. internal static extern int enet_peer_send(IntPtr peer, byte channelID, IntPtr packet);
  81. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  82. internal static extern IntPtr enet_peer_receive(IntPtr peer, out byte channelID);
  83. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  84. internal static extern void enet_peer_reset(IntPtr peer);
  85. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  86. internal static extern void enet_peer_ping(IntPtr peer);
  87. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  88. internal static extern void enet_peer_disconnect_now(IntPtr peer, uint data);
  89. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  90. internal static extern void enet_peer_disconnect(IntPtr peer, uint data);
  91. [DllImport(LIB, CallingConvention = CallingConvention.Cdecl)]
  92. internal static extern void enet_peer_disconnect_later(IntPtr peer, uint data);
  93. public static bool memcmp(byte[] s1, byte[] s2)
  94. {
  95. if (s1 == null || s2 == null)
  96. {
  97. throw new ArgumentNullException();
  98. }
  99. if (s1.Length != s2.Length)
  100. {
  101. return false;
  102. }
  103. for (int i = 0; i < s1.Length; i++)
  104. {
  105. if (s1[i] != s2[i])
  106. {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. public static int strlen(byte[] s)
  113. {
  114. if (s == null)
  115. {
  116. throw new ArgumentNullException();
  117. }
  118. int i;
  119. for (i = 0; i < s.Length && s[i] != 0; i++)
  120. {
  121. ;
  122. }
  123. return i;
  124. }
  125. }
  126. }