Unity C# 网络学习(一)

Unity C# 网络学习(一)

一.IP地址和端口类

public class Lesson01 : MonoBehaviour
{
    private void Start()
    {
        var ipByte = new byte[]{128,123,116,21};
        var ipAddress = new IPAddress(ipByte);
        //IP和端口
        var ipEndPoint = new IPEndPoint(ipAddress, 8080);
        var ipV6 = IPAddress.IPv6Any;
    }
}

二.域名解析

public class Lesson02 : MonoBehaviour
{
    private void Start()
    {
        Debug.Log(Dns.GetHostName());
        var ipaddressList = Dns.GetHostAddresses("www.taobao.com");
        foreach (var item in ipaddressList)
            Debug.Log(item);
        Debug.Log("===============================");
        var ipHostEntry = Dns.GetHostEntry("www.taobao.com");
        //Dns服务器名称
        Debug.Log(ipHostEntry.HostName);
        //IP地址
        foreach (var item in ipHostEntry.AddressList)
            Debug.Log(item);
        //主机别名
        foreach (var item in ipHostEntry.Aliases)
            Debug.Log(item);
    }
}

三.序列化为字节数组

  • 之前的博客中有封装过类似的一个类
  • 这里用另一种思路去处理
public class Lesson03 : MonoBehaviour
{
    private void Start()
    {
        var info = new PlayerInfo
        {
            lev = 10,
            name = "zzs",
            atk = 99,
            sex = true
        };
        var length = sizeof(int) +
                     sizeof(int) +
                     Encoding.UTF8.GetBytes(info.name).Length +
                     sizeof(short) +
                     sizeof(bool);
        var buffer = new byte[length];
        var index = 0;
        BitConverter.GetBytes(info.lev).CopyTo(buffer,index);
        index += sizeof(int);
        BitConverter.GetBytes(info.name.Length).CopyTo(buffer,index);
        index += sizeof(int);
        var tmp = Encoding.UTF8.GetBytes(info.name);
        tmp.CopyTo(buffer,index);
        index += tmp.Length;
        BitConverter.GetBytes(info.atk).CopyTo(buffer,index);
        index += sizeof(short);
        BitConverter.GetBytes(info.sex).CopyTo(buffer,index);
    }
}

如过向上文这样去进行字节数组的转换那么在项目中将会非常麻烦,我们可以封装一下,这样可以方便我们去开发

1.先封装一个需要序列化的数据基类

public class DataBase
{
    private int _currIndex;
    public int CurrIndex => _currIndex;
    public virtual int GetLength()
    {
        return 0;
    }
    public virtual byte[] GetBytes()
    {
        return null;
    }
    protected void WriteInt(byte[] buffer, int val)
    {
        BitConverter.GetBytes(val).CopyTo(buffer,_currIndex);
        _currIndex += sizeof(int);
    }
    protected void WriteShort(byte[] buffer, short val)
    {
        BitConverter.GetBytes(val).CopyTo(buffer,_currIndex);
        _currIndex += sizeof(short);
    }
    protected void WriteBool(byte[] buffer, bool val)
    {
        BitConverter.GetBytes(val).CopyTo(buffer,_currIndex);
        _currIndex += sizeof(bool);
    }
    protected void WriteString(byte[] buffer, string val)
    {
        WriteInt(buffer,val.Length);
        var tmpBuffer = Encoding.UTF8.GetBytes(val);
        tmpBuffer.CopyTo(buffer,_currIndex);
        _currIndex += tmpBuffer.Length;
    }
    protected void WriteData<T>(byte[] buffer, T val) where T : DataBase
    {
        val.GetBytes().CopyTo(buffer,_currIndex);
        _currIndex += val.GetLength();
    }
}

2.使用的时候,只需要继承这个类,然后重写里面的方法就好了

public class TestInfo : DataBase
{
    public short lev;
    public Player player;
    public int hp;
    public string name;
    public bool sex;
    public override int GetLength()
    {
        return sizeof(short) +
               player.GetLength() +
               sizeof(int) +
               4 + Encoding.UTF8.GetBytes(name).Length +
               sizeof(bool);
    }
    public override byte[] GetBytes()
    {
        var buffer = new byte[GetLength()];
        WriteShort(buffer,lev);
        WriteData(buffer,player);
        WriteInt(buffer,hp);
        WriteString(buffer,name);
        WriteBool(buffer,sex);
        return buffer;
    }
}
public class Player : DataBase
{
    public int atk;
    public override int GetLength()
    {
        return 4;
    }
    public override byte[] GetBytes()
    {
        var buffer = new byte[GetLength()];
        WriteInt(buffer,atk);
        return buffer;
    }
}

        var testInfo = new TestInfo
        {
            lev = 99,
            player = new Player
            {
                atk = 1000
            },
            hp = 100,
            name = "zzs",
            sex = true
        };
        var buffer1 = testInfo.GetBytes();

之后可以进一步优化这个类,或者使用程序化生成这个类,不过感觉这种方式比较之前博客中介绍的方式性能会有所下降,不过也是提供了一种思路

四.反序列化字节数组

直接处理

public class Lesson04 : MonoBehaviour
{
    private void Start()
    {
        var playerData = new PlayerInfo
        {
            atk = 10,
            sex = true,
            name = "zzs",
            lev = 99
        };
        var index = 0;
        var buffer = playerData.GetBytes();
        Debug.Log(BitConverter.ToInt32(buffer,index));
        index += 4;
        var length = BitConverter.ToInt32(buffer, index);
        index += 4;
        Debug.Log(Encoding.UTF8.GetString(buffer,index,length));
        index += length;
        Debug.Log(BitConverter.ToInt16(buffer,index));
        index += 2;
        Debug.Log(BitConverter.ToBoolean(buffer,index));
    }
}

封装到基类处理

public abstract void Reading(byte[] buffer,int startIndex = 0);
    protected int ReadInt(byte[] buffer,ref int index)
    {
        var res = BitConverter.ToInt32(buffer, index);
        index += 4;
        return res;
    }
    protected short ReadShort(byte[] buffer,ref int index)
    {
        var res = BitConverter.ToInt16(buffer, index);
        index += 2;
        return res;
    }
    protected bool ReadBool(byte[] buffer,ref int index)
    {
        var res = BitConverter.ToBoolean(buffer, index);
        index += 1;
        return res;
    }
    protected string ReadString(byte[] buffer,ref int index)
    {
        var length = ReadInt(buffer,ref index);
        var res = Encoding.UTF8.GetString(buffer, index,length);
        index += length;
        return res;
    }
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 返回读取对象的长度
    protected T ReadData<T>(byte[] buffer,ref int index) where T : DataBase,new()
    {
        var res = new T();
        res.Reading(buffer, index);
        index += res.GetLength();
        return res;
    }

完整代码

public abstract class DataBase
{
    public abstract int GetLength();
    public abstract byte[] GetBytes();
    public abstract void Reading(byte[] buffer,int startIndex = 0);
    protected void WriteInt(byte[] buffer, int val,ref int index)
    {
        BitConverter.GetBytes(val).CopyTo(buffer,index);
        index += sizeof(int);
    }
    protected void WriteShort(byte[] buffer, short val,ref int index)
    {
        BitConverter.GetBytes(val).CopyTo(buffer,index);
        index += sizeof(short);
    }
    protected void WriteBool(byte[] buffer, bool val,ref int index)
    {
        BitConverter.GetBytes(val).CopyTo(buffer,index);
        index += sizeof(bool);
    }
    protected void WriteString(byte[] buffer, string val,ref int index)
    {
        WriteInt(buffer,val.Length,ref index);
        var tmpBuffer = Encoding.UTF8.GetBytes(val);
        tmpBuffer.CopyTo(buffer,index);
        index += tmpBuffer.Length;
    }
    protected void WriteData<T>(byte[] buffer, T val,ref int index) where T : DataBase
    {
        val.GetBytes().CopyTo(buffer,index);
        index += val.GetLength();
    }

    protected int ReadInt(byte[] buffer,ref int index)
    {
        var res = BitConverter.ToInt32(buffer, index);
        index += 4;
        return res;
    }
    protected short ReadShort(byte[] buffer,ref int index)
    {
        var res = BitConverter.ToInt16(buffer, index);
        index += 2;
        return res;
    }
    protected bool ReadBool(byte[] buffer,ref int index)
    {
        var res = BitConverter.ToBoolean(buffer, index);
        index += 1;
        return res;
    }
    protected string ReadString(byte[] buffer,ref int index)
    {
        var length = ReadInt(buffer,ref index);
        var res = Encoding.UTF8.GetString(buffer, index,length);
        index += length;
        return res;
    }
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 返回读取对象的长度
    protected T ReadData<T>(byte[] buffer,ref int index) where T : DataBase,new()
    {
        var res = new T();
        res.Reading(buffer, index);
        index += res.GetLength();
        return res;
    }
}
public class TestInfo : DataBase
{
    public short lev;
    public Player player;
    public int hp;
    public string name;
    public bool sex;
    public override int GetLength()
    {
        return sizeof(short) +
               player.GetLength() +
               sizeof(int) +
               4 + Encoding.UTF8.GetBytes(name).Length +
               sizeof(bool);
    }
    public override byte[] GetBytes()
    {
        var buffer = new byte[GetLength()];
        var index = 0;
        WriteShort(buffer,lev,ref index);
        WriteData(buffer,player,ref index);
        WriteInt(buffer,hp,ref index);
        WriteString(buffer,name,ref index);
        WriteBool(buffer,sex,ref index);
        return buffer;
    }

    public override void Reading(byte[] buffer, int startIndex = 0)
    {
        lev = ReadShort(buffer, ref startIndex);
        player = ReadData<Player>(buffer, ref startIndex);
        hp = ReadInt(buffer, ref startIndex);
        name = ReadString(buffer, ref startIndex);
        sex = ReadBool(buffer, ref startIndex);
    }
}
public class Player : DataBase
{
    public int atk;
    public override int GetLength()
    {
        return 4;
    }
    public override byte[] GetBytes()
    {
        var buffer = new byte[GetLength()];
        var index = 0;
        WriteInt(buffer,atk,ref index);
        return buffer;
    }
    public override void Reading(byte[] buffer, int startIndex = 0)
    {
        atk = ReadInt(buffer, ref startIndex);
    }
}

public class Lesson04 : MonoBehaviour
{
    private void Start()
    {
        var testInfo = new TestInfo
        {
            lev = 99,
            player = new Player
            {
                atk = 1000
            },
            hp = 100,
            name = "zzs",
            sex = true
        };
        var buffer = testInfo.GetBytes();
        
        var newTestInfo = new TestInfo();
        newTestInfo.Reading(buffer);
        Debug.Log(newTestInfo.hp);
        Debug.Log(newTestInfo.lev);
        Debug.Log(newTestInfo.name);
        Debug.Log(newTestInfo.player.atk);
        Debug.Log(newTestInfo.sex);
    }
}

你可能感兴趣的:(Unity,unity,网络)