Native.cs 6.0 KB

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