TCP框架___Unity

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;
using System.Text;

namespace Core
{
    public class Protocal
    {
        public const int Connect = -1;     //连接服务器
        public const int Exception = -2;   //异常掉线
        public const int Disconnect = -3;  //正常断线   
    }

    enum PACKET_TYPE
    {                              // 包格式(由服务器定下的规矩,客户端严格按照格式发送/接收),PackLen用于标识后续包长,Place为无效占位符,Type为包的类型(PACKET_TYPE),Seq作用未知默认为0,Code为错误码,Body为包正文
        PUSH_TYPE = 0,             // PackLen【2byte】,Place【1byte】,Type【1byte】,Body【最大4096byte】
        REQUSET_TYPE,              // PackLen【2byte】,Place【1byte】,Type【1byte】,Seq【2byte】,Body【最大4096byte】
        RESPONSE_TYPE,             // PackLen【2byte】,Place【1byte】,Type【1byte】,Seq【2byte】,Code(错误码)【2byte】,Body【最大4096byte】
        PING_TYPE,                 // PackLen【2byte】,Place【1byte】,Type【1byte】,Seq【1byte】PS:心跳发送
        PONG_TYPE                  // PackLen【2byte】,Place【1byte】,Type【1byte】,Seq【1byte】PS:心跳回包
    };


    //让byte[] 压入成为lua string 而不是数组 userdata
    //也可以使用LuaByteBufferAttribute来标记byte[]
    public struct LuaByteBuffer
    {
        public LuaByteBuffer(IntPtr source, int len)
            : this()
        {
            buffer = new byte[len];
            Length = len;
            Marshal.Copy(source, buffer, 0, len);
        }

        public LuaByteBuffer(byte[] buf)
            : this()
        {
            buffer = buf;
            Length = buf.Length;
        }

        public LuaByteBuffer(byte[] buf, int len)
            : this()
        {
            buffer = buf;
            Length = len;
        }

        public LuaByteBuffer(System.IO.MemoryStream stream)
            : this()
        {
            buffer = stream.GetBuffer();
            Length = (int)stream.Length;
        }

        public static implicit operator LuaByteBuffer(System.IO.MemoryStream stream)
        {
            return new LuaByteBuffer(stream);
        }

        public byte[] buffer;

        public int Length
        {
            get;
            private set;
        }
    }

    // Byte数据结构体,需要注意每次数据写入为有序的
    public class ByteBuffer
    {
        MemoryStream stream = null;
        BinaryWriter writer = null;
        BinaryReader reader = null;

        public ByteBuffer()
        {
            stream = new MemoryStream();
            writer = new BinaryWriter(stream);
        }

        public ByteBuffer(byte[] data)
        {
            if (data != null)
            {
                stream = new MemoryStream(data);
                reader = new BinaryReader(stream);
            }
            else
            {
                stream = new MemoryStream();
                writer = new BinaryWriter(stream);
            }
        }

        public void Close()
        {
            if (writer != null)
            {
                writer.Close();
            }
            if (reader != null)
            {
                reader.Close();
            }

            stream.Close();
            writer = null;
            reader = null;
            stream = null;
        }

        public void WriteByte(byte v)
        {
            writer.Write(v);
        }

        public void WriteInt(int v)
        {
            writer.Write((int)v);
        }

        public void WriteShort(ushort v)
        {
            writer.Write((ushort)v);
        }

        public void WriteLong(long v)
        {
            writer.Write((long)v);
        }

        public void WriteFloat(float v)
        {
            byte[] bytes = BitConverter.GetBytes(v);
            Array.Reverse(bytes);
            writer.Write(BitConverter.ToSingle(bytes, 0));
        }

        public void WriteDouble(double v)
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值