Tcp_Util.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.IO;
  3. using ICSharpCode.SharpZipLib.GZip;
  4. using System.Net.Sockets;
  5. using System.Net;
  6. using System.Net.NetworkInformation;
  7. using System.Text;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Runtime.Serialization;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. public class Tcp_Util
  13. {
  14. /// <summary>
  15. /// 获取本机ip地址
  16. /// </summary>
  17. /// <returns>The address I.</returns>
  18. ///
  19. public static string GetAddressIP()
  20. {
  21. string AddressIP = string.Empty;
  22. #if UNITY_EDITOR
  23. foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
  24. {
  25. if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
  26. {
  27. AddressIP = _IPAddress.ToString();
  28. }
  29. }
  30. #else
  31. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  32. foreach (NetworkInterface adapter in adapters) {
  33. if (adapter.Supports (NetworkInterfaceComponent.IPv4)) {
  34. UnicastIPAddressInformationCollection uniCast = adapter.GetIPProperties ().UnicastAddresses;
  35. if (uniCast.Count > 0) {
  36. foreach (UnicastIPAddressInformation uni in uniCast) {
  37. //得到IPv4的地址。 AddressFamily.InterNetwork指的是IPv4
  38. if (uni.Address.AddressFamily == AddressFamily.InterNetwork) {
  39. AddressIP = uni.Address.ToString ();
  40. }
  41. }
  42. }
  43. }
  44. }
  45. #endif
  46. //Debug.Log ("ip:=="+AddressIP);
  47. return AddressIP;
  48. }
  49. public static string GetIP()
  50. {
  51. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  52. foreach (NetworkInterface adater in adapters)
  53. {
  54. if (adater.Supports(NetworkInterfaceComponent.IPv4))
  55. {
  56. UnicastIPAddressInformationCollection UniCast = adater.GetIPProperties().UnicastAddresses;
  57. if (UniCast.Count > 0)
  58. {
  59. foreach (UnicastIPAddressInformation uni in UniCast)
  60. {
  61. if (uni.Address.AddressFamily == AddressFamily.InterNetwork)
  62. {
  63. Debug.Log(uni.Address.ToString());
  64. return uni.Address.ToString();
  65. }
  66. }
  67. }
  68. }
  69. }
  70. return null;
  71. }
  72. /// <summary>
  73. /// 压缩字节数组
  74. /// </summary>
  75. /// <param name="str"></param>
  76. public static byte[] Compress(byte[] inputBytes)
  77. {
  78. // return inputBytes;
  79. // Debug.Log("原始数据长度是: " + inputBytes.Length);
  80. using (MemoryStream ms = new MemoryStream())
  81. {
  82. using (GZipOutputStream gzip = new GZipOutputStream(ms))
  83. {
  84. gzip.Write(inputBytes, 0, inputBytes.Length);
  85. gzip.Close();
  86. // Debug.Log("压缩后的数据长度是: " + ms.ToArray().Length);
  87. return ms.ToArray();
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// 解压缩字节数组
  93. /// </summary>
  94. /// <param name="str"></param>
  95. public static byte[] Decompress(byte[] inputBytes)
  96. {
  97. // return inputBytes;
  98. using (GZipInputStream gzi = new GZipInputStream(new MemoryStream(inputBytes)))
  99. {
  100. using (MemoryStream re = new MemoryStream())
  101. {
  102. int count = 0;
  103. byte[] data = new byte[1024000];
  104. while ((count = gzi.Read(data, 0, data.Length)) != 0)
  105. {
  106. re.Write(data, 0, count);
  107. }
  108. byte[] depress = re.ToArray();
  109. return depress;
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。 和bytesToInt()配套使用
  115. /// </summary>
  116. /// <returns>byte数组.</returns>
  117. /// <param name="value">要转换的int值.</param>
  118. public static byte[] intToBytes(int value)
  119. {
  120. byte[] src = new byte[4];
  121. src[0] = (byte)((value >> 24) & 0xFF);
  122. src[1] = (byte)((value >> 16) & 0xFF);
  123. src[2] = (byte)((value >> 8) & 0xFF);
  124. src[3] = (byte)(value & 0xFF);
  125. return src;
  126. }
  127. /// <summary>
  128. /// byte数组中取int数值,本方法适用于(高位在前,低位在后)的顺序,和和intToBytes()配套使用
  129. /// </summary>
  130. /// <returns>int数值</returns>
  131. /// <param name="src">byte数组 </param>
  132. /// <param name="offset">从数组的第offset位开始 </param>
  133. public static int bytesToInt(byte[] src, int offset = 0)
  134. {
  135. int value;
  136. value = (int)((src[offset + 3] & 0xFF)
  137. | ((src[offset + 2] & 0xFF) << 8)
  138. | ((src[offset + 1] & 0xFF) << 16)
  139. | ((src[offset] & 0xFF) << 24));
  140. return value;
  141. }
  142. public static byte[] StringToBytes(string msg)
  143. {
  144. return Encoding.UTF8.GetBytes(msg);
  145. }
  146. public static string BytesToString(byte[] bytes)
  147. {
  148. return Encoding.UTF8.GetString(bytes);
  149. }
  150. public static byte[] Serialize(object data)
  151. {
  152. BinaryFormatter formatter = getBinaryFormatter();
  153. MemoryStream rems = new MemoryStream();
  154. formatter.Serialize(rems, data);
  155. return rems.GetBuffer();
  156. }
  157. public static object Deserialize(byte[] data)
  158. {
  159. BinaryFormatter formatter = getBinaryFormatter();
  160. MemoryStream rems = new MemoryStream(data);
  161. object res = formatter.Deserialize(rems);
  162. return res;
  163. }
  164. private static BinaryFormatter bf;
  165. public static BinaryFormatter getBinaryFormatter()
  166. {
  167. if (bf == null)
  168. {
  169. bf = new BinaryFormatter();
  170. SurrogateSelector ss = new SurrogateSelector();
  171. bf.SurrogateSelector = ss;
  172. }
  173. return bf;
  174. }
  175. public static MessageData BufferToMessage(byte[] bytes)
  176. {
  177. byte[] recvBytesBody = Tcp_Util.Decompress(bytes);
  178. MessageData msg = (MessageData)Tcp_Util.Deserialize(recvBytesBody);
  179. return msg;
  180. }
  181. public static byte[] MessageToBuffer(MessageData msg)
  182. {
  183. byte[] buffer = Tcp_Util.Compress(Tcp_Util.Serialize(msg));//将类转换为二进制
  184. byte[] head = Tcp_Util.intToBytes(buffer.Length);
  185. List<byte> sendBuffer = new List<byte>();
  186. sendBuffer.AddRange(head);
  187. sendBuffer.AddRange(buffer);
  188. return sendBuffer.ToArray();
  189. }
  190. }