开始开发
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.XR.XREAL.Samples.NetWork
|
||||
{
|
||||
/// <summary> A message packer. </summary>
|
||||
public class MessagePacker
|
||||
{
|
||||
/// <summary> The bytes. </summary>
|
||||
private List<byte> bytes = new List<byte>();
|
||||
|
||||
/// <summary> Gets the package. </summary>
|
||||
/// <value> The package. </value>
|
||||
public byte[] Package
|
||||
{
|
||||
get { return bytes.ToArray(); }
|
||||
}
|
||||
|
||||
/// <summary> Adds value. </summary>
|
||||
/// <param name="data"> The data to add.</param>
|
||||
/// <returns> A MessagePacker. </returns>
|
||||
public MessagePacker Add(byte[] data)
|
||||
{
|
||||
bytes.AddRange(data);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary> Adds value. </summary>
|
||||
/// <param name="value"> The value to add.</param>
|
||||
/// <returns> A MessagePacker. </returns>
|
||||
public MessagePacker Add(ushort value)
|
||||
{
|
||||
byte[] data = BitConverter.GetBytes(value);
|
||||
bytes.AddRange(data);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary> Adds value. </summary>
|
||||
/// <param name="value"> The value to add.</param>
|
||||
/// <returns> A MessagePacker. </returns>
|
||||
public MessagePacker Add(uint value)
|
||||
{
|
||||
byte[] data = BitConverter.GetBytes(value);
|
||||
bytes.AddRange(data);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary> Adds value. </summary>
|
||||
/// <param name="value"> The value to add.</param>
|
||||
/// <returns> A MessagePacker. </returns>
|
||||
public MessagePacker Add(ulong value)
|
||||
{
|
||||
byte[] data = BitConverter.GetBytes(value);
|
||||
bytes.AddRange(data);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3ad4b2ef850781438a4531d3c8843d3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.XR.XREAL.Samples.NetWork
|
||||
{
|
||||
/// <summary> A network coroutine. </summary>
|
||||
internal class NetworkCoroutine : MonoBehaviour
|
||||
{
|
||||
/// <summary> Event queue for all listeners interested in applicationQuit events. </summary>
|
||||
private event Action ApplicationQuitEvent;
|
||||
|
||||
/// <summary> The instance. </summary>
|
||||
private static NetworkCoroutine _instance;
|
||||
|
||||
/// <summary> Gets the instance. </summary>
|
||||
/// <value> The instance. </value>
|
||||
public static NetworkCoroutine Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!_instance)
|
||||
{
|
||||
GameObject socketClientObj = new GameObject("NetworkCoroutine");
|
||||
_instance = socketClientObj.AddComponent<NetworkCoroutine>();
|
||||
DontDestroyOnLoad(socketClientObj);
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Sets quit event. </summary>
|
||||
/// <param name="func"> The function.</param>
|
||||
public void SetQuitEvent(Action func)
|
||||
{
|
||||
if (ApplicationQuitEvent != null) return;
|
||||
ApplicationQuitEvent += func;
|
||||
}
|
||||
|
||||
/// <summary> Executes the 'application quit' action. </summary>
|
||||
private void OnApplicationQuit()
|
||||
{
|
||||
ApplicationQuitEvent?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 239487c972f53a549a19b8fe475f3610
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,135 @@
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace Unity.XR.XREAL.Samples.NetWork
|
||||
{
|
||||
/// <summary> A network utilities. </summary>
|
||||
public static class NetworkUtils
|
||||
{
|
||||
/// <summary> Get local ipv4, return null if faild. </summary>
|
||||
/// <returns> The local IPv4. </returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary> Byte 2 string. </summary>
|
||||
/// <param name="bytes"> The bytes.</param>
|
||||
/// <returns> A string. </returns>
|
||||
public static string Byte2String(byte[] bytes)
|
||||
{
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary> String 2 byte. </summary>
|
||||
/// <param name="str"> The string.</param>
|
||||
/// <returns> A byte[]. </returns>
|
||||
public static byte[] String2Byte(string str)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(str);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Interface for serializer. </summary>
|
||||
public interface ISerializer
|
||||
{
|
||||
/// <summary> Serialize this object to the given stream. </summary>
|
||||
/// <param name="obj"> The object.</param>
|
||||
/// <returns> A byte[]. </returns>
|
||||
byte[] Serialize(object obj);
|
||||
|
||||
/// <summary> Deserialize this object to the given stream. </summary>
|
||||
/// <typeparam name="T"> Generic type parameter.</typeparam>
|
||||
/// <param name="data"> The data.</param>
|
||||
/// <returns> A T. </returns>
|
||||
T Deserialize<T>(byte[] data) where T : class;
|
||||
}
|
||||
|
||||
/// <summary> An object for persisting JSON data. </summary>
|
||||
public class JsonSerializer : ISerializer
|
||||
{
|
||||
/// <summary> Deserialize this object to the given stream. </summary>
|
||||
/// <typeparam name="T"> Generic type parameter.</typeparam>
|
||||
/// <param name="data"> The data.</param>
|
||||
/// <returns> A T. </returns>
|
||||
public T Deserialize<T>(byte[] data) where T : class
|
||||
{
|
||||
return LitJson.JsonMapper.ToObject<T>(Encoding.UTF8.GetString(data));
|
||||
}
|
||||
|
||||
/// <summary> Serialize this object to the given stream. </summary>
|
||||
/// <param name="obj"> The object.</param>
|
||||
/// <returns> A byte[]. </returns>
|
||||
public byte[] Serialize(object obj)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(LitJson.JsonMapper.ToJson(obj));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> An object for persisting binary data. </summary>
|
||||
public class BinarySerializer : ISerializer
|
||||
{
|
||||
/// <summary> obj -> bytes, return null if obj not mark as [Serializable]. </summary>
|
||||
/// <param name="obj"> The object.</param>
|
||||
/// <returns> A byte[]. </returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> bytes -> obj, return null if obj not mark as [Serializable]. </summary>
|
||||
/// <typeparam name="T"> Generic type parameter.</typeparam>
|
||||
/// <param name="data"> The data.</param>
|
||||
/// <returns> A T. </returns>
|
||||
public T Deserialize<T>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> A serializer factory. </summary>
|
||||
public static class SerializerFactory
|
||||
{
|
||||
/// <summary> The serializer. </summary>
|
||||
private static ISerializer _Serializer;
|
||||
|
||||
/// <summary> Creates a new ISerializer. </summary>
|
||||
/// <returns> An ISerializer. </returns>
|
||||
public static ISerializer Create()
|
||||
{
|
||||
if (_Serializer == null)
|
||||
{
|
||||
_Serializer = new JsonSerializer();
|
||||
}
|
||||
return _Serializer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b987fb1d08c23e4085e3f56c50c9b00
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user