• unity3d的網(wǎng)絡(luò)套接字SOCKET模塊使用

    2019/6/12??????點(diǎn)擊:

    使用方法簡(jiǎn)單:
    1、新建一個(gè)空物體把NetWork掛載上,2、填上ip和prot,3、調(diào)用Connet方法;

    /*
    UNITY3D 網(wǎng)絡(luò)組件,它是單例模式。
     通過(guò)Network開(kāi)啟和關(guān)閉網(wǎng)絡(luò);
     消息訂閱器MsgEvent管理服務(wù)端發(fā)來(lái)的消息
     */
    using UnityEngine;
    using Assets.Scripts.Events;
    using Assets.Scripts.Util;
    using Assets.Scripts.Net;
    using System;
    
    public enum NetMsgType
    {
        ////// 新消息
        ///newMsg = -4,
        ////// 連接服務(wù)中斷
        ///interrupt = -3,
        ////// 請(qǐng)求錯(cuò)誤
        ///error = -2,
        ////// 連接成功
        ///complete = -1,
        ////// 服務(wù)端握手響應(yīng)
        ///state = 1
    }
    public class Network : MonoBehaviour
    {
        ////// 網(wǎng)絡(luò)消息廣播,當(dāng)服務(wù)端有消息發(fā)來(lái)是通過(guò)它廣播
        ///public static readonly EventDispatchMsgEvent = new EventDispatch();
        public static bool IsConnet { get; private set; }
        public static bool IsActive { get; private set; }
        ////// 網(wǎng)絡(luò)組件實(shí)例
        ///public static Network Server { get; private set; }
    
        private SocketTcp socket;
        public string ip = "";
        public int prot;
        void Awake()
        {
            Server = this;
        }
        void Start()
        {
        }
        ////// 向服務(wù)端發(fā)送數(shù)據(jù)
        //////協(xié)議接口類(lèi)型///預(yù)留///要發(fā)送的數(shù)據(jù)public void outCall(ushort type, ushort error, ByteArray data)
        {
            if (IsConnet)
            {
                socket.Send(type, error, data);
            }
        }
        public void Connet()
        {
            if (IsActive == false)
            {
                IsActive = true;
                socket = new SocketTcp(ip, prot);
                socket.AddListener(NetEvent.CONNETED, Conneted);
                socket.AddListener(NetEvent.CONNET_IOERROT, ConnetIOError);
                socket.AddListener(NetEvent.CONNET_ERROT, ConnetError);
                socket.AddListener(NetEvent.CONNET_MSG, MsgHandler);
    
                socket.Start();
            }
        }
        private void Conneted(EventData data)
        {
            IsConnet = true;
            MsgEvent.Dispatch(NetMsgType.complete);
        }
        void ConnetIOError(EventData data)
        {
            IsConnet = false;
            IsActive = false;
            MsgEvent.Dispatch(NetMsgType.error);
        }
        void ConnetError(EventData data)
        {
            IsConnet = false;
            IsActive = false;
            MsgEvent.Dispatch(NetMsgType.interrupt);
        }
        void MsgHandler(EventData data)
        {
            BucketItem msg = (BucketItem)data.data;
            msg.ByteArray.Position = 0;
            if (msg.Error > 0)
            {
                Debug.Log("網(wǎng)絡(luò)錯(cuò)誤:" + msg.Error);
                return;
            }
            if (Enum.IsDefined(typeof(NetMsgType), msg.Type))
            {
                
                MsgEvent.Dispatch((NetMsgType)msg.Type, msg);
            }
            else
            {
                Debug.Log("未定義網(wǎng)絡(luò)消息:" + msg.Type);
            }
        }
        public void Disconnect()
        {
            IsConnet = false;
            IsActive = false;
            socket.RemoveListener(NetEvent.CONNETED, Conneted);
            socket.RemoveListener(NetEvent.CONNET_IOERROT, ConnetIOError);
            socket.RemoveListener(NetEvent.CONNET_ERROT, ConnetError);
            socket.RemoveListener(NetEvent.CONNET_MSG, MsgHandler);
            socket.Destroy();
            socket = null;
        }
        void FixedUpdate()
        {
    
        }
        void Update()
        {
            if (socket != null) socket.Updata();
        }
        //程序退出則關(guān)閉連接  
        void OnApplicationQuit()
        {
            if (socket != null) socket.Destroy();
        }
    }
    /*
     socketTcp封裝了一個(gè)輕量通信協(xié)議,以8個(gè)字節(jié)為頭部(整型):無(wú)符號(hào)4字節(jié)長(zhǎng)度(包括頭部8字節(jié))、消息類(lèi)型無(wú)符號(hào)2字節(jié)、預(yù)留無(wú)符號(hào)2字節(jié)。
      使用異步方法兼容hololens的uwp平臺(tái)。消息類(lèi)型自定義,協(xié)議的數(shù)據(jù)包格式在注釋有說(shuō)。 */
    using Assets.Scripts.Events;
    using System;
    using System.Collections;
    using Assets.Scripts.Util;
    using UnityEngine;
    using System.Collections.Generic;
    #if !UWP
    using System.Net;
    using System.Net.Sockets;
    #else
    using Windows.Networking.Sockets;
    using Windows.Networking.Connectivity;
    using Windows.Networking;
    #endif
    namespace Assets.Scripts.Net
    {
        public enum NetEvent{
            ////// 連接建立
            ///CONNETED,
            ////// 請(qǐng)求連接服務(wù)器時(shí)發(fā)生錯(cuò)誤
            ///CONNET_IOERROT,
            ////// 連接中斷
            ///CONNET_ERROT,
            ////// 收到消息
            ///CONNET_MSG
        }
        public class SocketTcp : EventDispatch{
            ////// 頭部字節(jié)
            ///public const int head_size = 8;
            ////// 是否使用小端
            ///public const bool IsLittleEndian = true;
            bool _activity = false;
            ////// 是否已啟用
            ///public bool Activity
            {
                get { return _activity; }
            }
            ////// 接受的消息,隊(duì)列
            ///private QueueMsgList;
            private QueuecodeMsg;
            public int port { get; private set; }
            public string ip { get; private set; }
            Socket socket;
            SocketAsyncEventArgs ReceiveSaea;
            SocketAsyncEventArgs sendSaea;
            byte[] sendData;
            ////// 允許單個(gè)數(shù)據(jù)包30720字節(jié)
            /// 
            ///const int RECV_LEN = 30720;
            byte[] recv_buf = new byte[RECV_LEN];
            Bucket bucket;
            bool socketState=false;
            public SocketTcp(string ip, int port)
            {
                this.port = port;
                this.ip = ip;
                bucket = new Bucket();
                codeMsg = new Queue();
                MsgList = new Queue();
            }
            private SocketAsyncEventArgs connetedSaea;
            ////// 發(fā)起鏈接請(qǐng)求
            /// 會(huì)調(diào)度CONNETED或CONNET_IOERROT事件
            ///public void Start()
            {
                if (socket==null)
                {
                    try
                    {
                        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        connetedSaea = new SocketAsyncEventArgs();
                        connetedSaea.Completed += new EventHandler(connetedCall);//設(shè)置回調(diào)方法                                                                             
                        connetedSaea.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);    //設(shè)置遠(yuǎn)端連接節(jié)點(diǎn),一般用于接收消息
                        sendSaea = new SocketAsyncEventArgs();
                        //連接到服務(wù)器
                        socket.ConnectAsync(connetedSaea);
                    }
                    catch (Exception e)
                    {
                        Debug.Log(e);
                        Dispatch(NetEvent.CONNET_IOERROT);
                    }
                    
                }
            }
            private void connetedCall(object sender, SocketAsyncEventArgs e)
            {
                _activity = true;
                codeMsg.Enqueue(NetEvent.CONNETED);
                ReceiveSaea = new SocketAsyncEventArgs();
                ReceiveSaea.SetBuffer(recv_buf, 0, recv_buf.Length);//設(shè)置緩沖區(qū)
                ReceiveSaea.Completed += new EventHandler(ReceiveCall);//設(shè)置回調(diào)方法
                //codeMsg.Enqueue(1);
                //連接后開(kāi)始從服務(wù)器讀取網(wǎng)絡(luò)消息
                socket.ReceiveAsync(ReceiveSaea);
            }
            int fragmentLen;
            byte[] fragment;
            ////// 回調(diào)讀取網(wǎng)絡(luò)數(shù)據(jù)、檢查是否斷線(xiàn)。
            /////////private void ReceiveCall(object sender, SocketAsyncEventArgs e)
            {
                fragmentLen = e.BytesTransferred;//調(diào)用這個(gè)函數(shù)來(lái)結(jié)束本次接收并返回接收到的數(shù)據(jù)長(zhǎng)度。
                if (fragmentLen > 0)
                {
                    fragment = new byte[fragmentLen];
                    Array.Copy(recv_buf, 0, fragment, 0, fragmentLen);
                    Queuearr = bucket.Infuse(fragment);
                    while (arr.Count > 0)
                    {
                        MsgList.Enqueue(arr.Dequeue());
                    }
                    socket.ReceiveAsync(ReceiveSaea);
                }
                else
                {
                    ReceiveSaea.Dispose();
                    bucket.Reset();
                    socket.Shutdown(SocketShutdown.Receive);//這個(gè)函數(shù)用來(lái)關(guān)閉客戶(hù)端連接 
                    _activity = false;
                    socketState = true;
                    Debug.Log("中斷,關(guān)閉連接");
                    return;
                }
            }
            ////// 發(fā)送字節(jié)流
            //////public void Send(ushort type, ushort error, ByteArray data)
            {
                uint len = (uint)data.Length + head_size;
                //清空發(fā)送緩存  
                sendData = new byte[len];
                ByteHelp.WriteNumber(len, ref sendData, 0, IsLittleEndian);
                ByteHelp.WriteNumber(type, ref sendData, 4, IsLittleEndian);
                ByteHelp.WriteNumber(error, ref sendData, 6, IsLittleEndian);
                if (data.Length > 0)
                {
                    Array.Copy(data.Data, 0, sendData, head_size, data.Length);
                }
                //sendData.
                //數(shù)據(jù)類(lèi)型轉(zhuǎn)換  
                //sendData = Encoding.ASCII.GetBytes(sendStr);
                //發(fā)送  
                sendSaea.SetBuffer(sendData, 0, sendData.Length);
                socket.SendAsync(sendSaea);
            }
            ////// 主線(xiàn)程每幀調(diào)用以拿取數(shù)據(jù)
            ///public void Updata()
            {
                while (codeMsg.Count > 0)
                {
                    Dispatch(codeMsg.Dequeue());
                }
                while (MsgList.Count > 0)
                {
                    Dispatch(NetEvent.CONNET_MSG, MsgList.Dequeue());
                }
                if (socketState)
                {
                    //Debug.Log("連接丟失,是否要重連");
                    socketState = false;
                    Dispatch(NetEvent.CONNET_ERROT);
                }
            }
            public void Destroy()
            {
                base.Dispose();
                //后關(guān)閉服務(wù)器  
                if (socket != null && socket.Connected)
                {
                    //this.socket.Disconnect(true);
                    //this.socket.Shutdown(SocketShutdown.Both);
                    //socket.Dispose();
                    socket.Shutdown(SocketShutdown.Receive);
                    socket.Shutdown(SocketShutdown.Send);
                    ReceiveSaea.Dispose();
                }
                bucket.Reset();
                MsgList.Clear();
                codeMsg.Clear();
                socketState = false;
                if (sendSaea != null) connetedSaea = null;
                if(sendSaea!=null) sendSaea.Dispose();
                sendSaea = null;
                if (_activity)
                {
                    _activity = false;
                }
            }
        }
    }