/****************************************************************************
* Copyright 2019 Nreal Techonology Limited. All rights reserved.
*
* This file is part of NRSDK.
*
* https://www.nreal.ai/
*
*****************************************************************************/
namespace NRKernal.Experimental.NetWork
{
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
/// A network utilities.
public static class NetworkUtils
{
/// Get local ipv4, return null if faild.
/// The local IPv4.
public static string GetLocalIPv4()
{
string hostName = Dns.GetHostName(); //得到主机名
IPHostEntry iPEntry = Dns.GetHostEntry(hostName);
for (int i = 0; i < iPEntry.AddressList.Length; i++)
{
//从IP地址列表中筛选出IPv4类型的IP地址
if (iPEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
return iPEntry.AddressList[i].ToString();
}
return null;
}
/// Byte 2 string.
/// The bytes.
/// A string.
public static string Byte2String(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
/// String 2 byte.
/// The string.
/// A byte[].
public static byte[] String2Byte(string str)
{
return Encoding.UTF8.GetBytes(str);
}
}
/// Interface for serializer.
public interface ISerializer
{
/// Serialize this object to the given stream.
/// The object.
/// A byte[].
byte[] Serialize(object obj);
/// Deserialize this object to the given stream.
/// Generic type parameter.
/// The data.
/// A T.
T Deserialize(byte[] data) where T : class;
}
/// An object for persisting JSON data.
public class JsonSerializer : ISerializer
{
/// Deserialize this object to the given stream.
/// Generic type parameter.
/// The data.
/// A T.
public T Deserialize(byte[] data) where T : class
{
return LitJson.JsonMapper.ToObject(Encoding.UTF8.GetString(data));
}
/// Serialize this object to the given stream.
/// The object.
/// A byte[].
public byte[] Serialize(object obj)
{
return Encoding.UTF8.GetBytes(LitJson.JsonMapper.ToJson(obj));
}
}
/// An object for persisting binary data.
public class BinarySerializer : ISerializer
{
/// obj -> bytes, return null if obj not mark as [Serializable].
/// The object.
/// A byte[].
public byte[] Serialize(object obj)
{
//物体不为空且可被序列化
if (obj == null || !obj.GetType().IsSerializable)
return null;
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, obj);
byte[] data = stream.ToArray();
return data;
}
}
/// bytes -> obj, return null if obj not mark as [Serializable].
/// Generic type parameter.
/// The data.
/// A T.
public T Deserialize(byte[] data) where T : class
{
//数据不为空且T是可序列化的类型
if (data == null || !typeof(T).IsSerializable)
return null;
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream(data))
{
object obj = formatter.Deserialize(stream);
return obj as T;
}
}
}
/// A serializer factory.
public static class SerializerFactory
{
/// The serializer.
private static ISerializer _Serializer;
/// Creates a new ISerializer.
/// An ISerializer.
public static ISerializer Create()
{
if (_Serializer == null)
{
_Serializer = new JsonSerializer();
}
return _Serializer;
}
}
}