NetworkUtils.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal.Experimental.NetWork
  10. {
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using System.IO;
  14. using System.Runtime.Serialization.Formatters.Binary;
  15. using System.Text;
  16. /// <summary> A network utilities. </summary>
  17. public static class NetworkUtils
  18. {
  19. /// <summary> Get local ipv4, return null if faild. </summary>
  20. /// <returns> The local IPv4. </returns>
  21. public static string GetLocalIPv4()
  22. {
  23. string hostName = Dns.GetHostName(); //得到主机名
  24. IPHostEntry iPEntry = Dns.GetHostEntry(hostName);
  25. for (int i = 0; i < iPEntry.AddressList.Length; i++)
  26. {
  27. //从IP地址列表中筛选出IPv4类型的IP地址
  28. if (iPEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  29. return iPEntry.AddressList[i].ToString();
  30. }
  31. return null;
  32. }
  33. /// <summary> Byte 2 string. </summary>
  34. /// <param name="bytes"> The bytes.</param>
  35. /// <returns> A string. </returns>
  36. public static string Byte2String(byte[] bytes)
  37. {
  38. return Encoding.UTF8.GetString(bytes);
  39. }
  40. /// <summary> String 2 byte. </summary>
  41. /// <param name="str"> The string.</param>
  42. /// <returns> A byte[]. </returns>
  43. public static byte[] String2Byte(string str)
  44. {
  45. return Encoding.UTF8.GetBytes(str);
  46. }
  47. }
  48. /// <summary> Interface for serializer. </summary>
  49. public interface ISerializer
  50. {
  51. /// <summary> Serialize this object to the given stream. </summary>
  52. /// <param name="obj"> The object.</param>
  53. /// <returns> A byte[]. </returns>
  54. byte[] Serialize(object obj);
  55. /// <summary> Deserialize this object to the given stream. </summary>
  56. /// <typeparam name="T"> Generic type parameter.</typeparam>
  57. /// <param name="data"> The data.</param>
  58. /// <returns> A T. </returns>
  59. T Deserialize<T>(byte[] data) where T : class;
  60. }
  61. /// <summary> An object for persisting JSON data. </summary>
  62. public class JsonSerializer : ISerializer
  63. {
  64. /// <summary> Deserialize this object to the given stream. </summary>
  65. /// <typeparam name="T"> Generic type parameter.</typeparam>
  66. /// <param name="data"> The data.</param>
  67. /// <returns> A T. </returns>
  68. public T Deserialize<T>(byte[] data) where T : class
  69. {
  70. return LitJson.JsonMapper.ToObject<T>(Encoding.UTF8.GetString(data));
  71. }
  72. /// <summary> Serialize this object to the given stream. </summary>
  73. /// <param name="obj"> The object.</param>
  74. /// <returns> A byte[]. </returns>
  75. public byte[] Serialize(object obj)
  76. {
  77. return Encoding.UTF8.GetBytes(LitJson.JsonMapper.ToJson(obj));
  78. }
  79. }
  80. /// <summary> An object for persisting binary data. </summary>
  81. public class BinarySerializer : ISerializer
  82. {
  83. /// <summary> obj -> bytes, return null if obj not mark as [Serializable]. </summary>
  84. /// <param name="obj"> The object.</param>
  85. /// <returns> A byte[]. </returns>
  86. public byte[] Serialize(object obj)
  87. {
  88. //物体不为空且可被序列化
  89. if (obj == null || !obj.GetType().IsSerializable)
  90. return null;
  91. BinaryFormatter formatter = new BinaryFormatter();
  92. using (MemoryStream stream = new MemoryStream())
  93. {
  94. formatter.Serialize(stream, obj);
  95. byte[] data = stream.ToArray();
  96. return data;
  97. }
  98. }
  99. /// <summary> bytes -> obj, return null if obj not mark as [Serializable]. </summary>
  100. /// <typeparam name="T"> Generic type parameter.</typeparam>
  101. /// <param name="data"> The data.</param>
  102. /// <returns> A T. </returns>
  103. public T Deserialize<T>(byte[] data) where T : class
  104. {
  105. //数据不为空且T是可序列化的类型
  106. if (data == null || !typeof(T).IsSerializable)
  107. return null;
  108. BinaryFormatter formatter = new BinaryFormatter();
  109. using (MemoryStream stream = new MemoryStream(data))
  110. {
  111. object obj = formatter.Deserialize(stream);
  112. return obj as T;
  113. }
  114. }
  115. }
  116. /// <summary> A serializer factory. </summary>
  117. public static class SerializerFactory
  118. {
  119. /// <summary> The serializer. </summary>
  120. private static ISerializer _Serializer;
  121. /// <summary> Creates a new ISerializer. </summary>
  122. /// <returns> An ISerializer. </returns>
  123. public static ISerializer Create()
  124. {
  125. if (_Serializer == null)
  126. {
  127. _Serializer = new JsonSerializer();
  128. }
  129. return _Serializer;
  130. }
  131. }
  132. }