NetUtils.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #if DEBUG && !UNITY_WP_8_1 && !UNITY_WSA_8_1
  2. #define UNITY
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. #if WINRT && !UNITY_EDITOR
  7. using Windows.Networking;
  8. using Windows.Networking.Connectivity;
  9. #else
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Net.NetworkInformation;
  13. #endif
  14. namespace FlyingWormConsole3.LiteNetLib
  15. {
  16. #if WINRT && !UNITY_EDITOR
  17. public enum ConsoleColor
  18. {
  19. Gray,
  20. Yellow,
  21. Cyan,
  22. DarkCyan,
  23. DarkGreen,
  24. Blue,
  25. DarkRed,
  26. Red,
  27. Green,
  28. DarkYellow
  29. }
  30. #endif
  31. [Flags]
  32. public enum LocalAddrType
  33. {
  34. IPv4 = 1,
  35. IPv6 = 2,
  36. All = 3
  37. }
  38. public static class NetUtils
  39. {
  40. internal static int RelativeSequenceNumber(int number, int expected)
  41. {
  42. return (number - expected + NetConstants.MaxSequence + NetConstants.HalfMaxSequence) % NetConstants.MaxSequence - NetConstants.HalfMaxSequence;
  43. }
  44. internal static int GetDividedPacketsCount(int size, int mtu)
  45. {
  46. return (size / mtu) + (size % mtu == 0 ? 0 : 1);
  47. }
  48. public static void PrintInterfaceInfos()
  49. {
  50. #if !WINRT || UNITY_EDITOR
  51. DebugWriteForce(ConsoleColor.Green, "IPv6Support: {0}", Socket.OSSupportsIPv6);
  52. try
  53. {
  54. foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
  55. {
  56. foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
  57. {
  58. if (ip.Address.AddressFamily == AddressFamily.InterNetwork ||
  59. ip.Address.AddressFamily == AddressFamily.InterNetworkV6)
  60. {
  61. DebugWriteForce(
  62. ConsoleColor.Green,
  63. "Interface: {0}, Type: {1}, Ip: {2}, OpStatus: {3}",
  64. ni.Name,
  65. ni.NetworkInterfaceType.ToString(),
  66. ip.Address.ToString(),
  67. ni.OperationalStatus.ToString());
  68. }
  69. }
  70. }
  71. }
  72. catch (Exception e)
  73. {
  74. DebugWriteForce(ConsoleColor.Red, "Error while getting interface infos: {0}", e.ToString());
  75. }
  76. #endif
  77. }
  78. public static void GetLocalIpList(List<string> targetList, LocalAddrType addrType)
  79. {
  80. bool ipv4 = (addrType & LocalAddrType.IPv4) == LocalAddrType.IPv4;
  81. bool ipv6 = (addrType & LocalAddrType.IPv6) == LocalAddrType.IPv6;
  82. #if WINRT && !UNITY_EDITOR
  83. foreach (HostName localHostName in NetworkInformation.GetHostNames())
  84. {
  85. if (localHostName.IPInformation != null &&
  86. ((ipv4 && localHostName.Type == HostNameType.Ipv4) ||
  87. (ipv6 && localHostName.Type == HostNameType.Ipv6)))
  88. {
  89. targetList.Add(localHostName.ToString());
  90. }
  91. }
  92. #else
  93. try
  94. {
  95. foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
  96. {
  97. //Skip loopback
  98. if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback)
  99. continue;
  100. var ipProps = ni.GetIPProperties();
  101. //Skip address without gateway
  102. if (ipProps.GatewayAddresses.Count == 0)
  103. continue;
  104. foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
  105. {
  106. var address = ip.Address;
  107. if ((ipv4 && address.AddressFamily == AddressFamily.InterNetwork) ||
  108. (ipv6 && address.AddressFamily == AddressFamily.InterNetworkV6))
  109. targetList.Add(address.ToString());
  110. }
  111. }
  112. }
  113. catch
  114. {
  115. //ignored
  116. }
  117. //Fallback mode (unity android)
  118. if (targetList.Count == 0)
  119. {
  120. #if NETCORE
  121. var hostTask = Dns.GetHostEntryAsync(Dns.GetHostName());
  122. hostTask.Wait();
  123. var host = hostTask.Result;
  124. #else
  125. var host = Dns.GetHostEntry(Dns.GetHostName());
  126. #endif
  127. foreach (IPAddress ip in host.AddressList)
  128. {
  129. if((ipv4 && ip.AddressFamily == AddressFamily.InterNetwork) ||
  130. (ipv6 && ip.AddressFamily == AddressFamily.InterNetworkV6))
  131. targetList.Add(ip.ToString());
  132. }
  133. }
  134. #endif
  135. if (targetList.Count == 0)
  136. {
  137. if(ipv4)
  138. targetList.Add("127.0.0.1");
  139. if(ipv6)
  140. targetList.Add("::1");
  141. }
  142. }
  143. private static readonly List<string> IpList = new List<string>();
  144. public static string GetLocalIp(LocalAddrType addrType)
  145. {
  146. lock (IpList)
  147. {
  148. IpList.Clear();
  149. GetLocalIpList(IpList, addrType);
  150. return IpList.Count == 0 ? string.Empty : IpList[0];
  151. }
  152. }
  153. private static readonly object DebugLogLock = new object();
  154. private static void DebugWriteLogic(ConsoleColor color, string str, params object[] args)
  155. {
  156. lock (DebugLogLock)
  157. {
  158. if (NetDebug.Logger == null)
  159. {
  160. #if UNITY
  161. #if !UNITY_4_0
  162. UnityEngine.Debug.LogFormat(str, args);
  163. #endif
  164. #elif WINRT
  165. Debug.WriteLine(str, args);
  166. #else
  167. Console.ForegroundColor = color;
  168. Console.WriteLine(str, args);
  169. Console.ForegroundColor = ConsoleColor.Gray;
  170. #endif
  171. }
  172. else
  173. {
  174. NetDebug.Logger.WriteNet(color, str, args);
  175. }
  176. }
  177. }
  178. [Conditional("DEBUG_MESSAGES")]
  179. internal static void DebugWrite(string str, params object[] args)
  180. {
  181. DebugWriteLogic(ConsoleColor.DarkGreen, str, args);
  182. }
  183. [Conditional("DEBUG_MESSAGES")]
  184. internal static void DebugWrite(ConsoleColor color, string str, params object[] args)
  185. {
  186. DebugWriteLogic(color, str, args);
  187. }
  188. [Conditional("DEBUG_MESSAGES"), Conditional("DEBUG")]
  189. internal static void DebugWriteForce(ConsoleColor color, string str, params object[] args)
  190. {
  191. DebugWriteLogic(color, str, args);
  192. }
  193. [Conditional("DEBUG_MESSAGES"), Conditional("DEBUG")]
  194. internal static void DebugWriteError(string str, params object[] args)
  195. {
  196. DebugWriteLogic(ConsoleColor.Red, str, args);
  197. }
  198. }
  199. }
  200. #endif