示例#1
0
    //将大缓冲的字节转移到此包
    public void Swap()
    {
        m_ReadBytes = PooledClassManager <DataBytes> .CreateClass();

        m_ReadBytes.SetBytes(m_Offset);
        Array.Copy(s_WriteBytes, 0, m_ReadBytes.GetBytes(), 0, m_Offset);
    }
示例#2
0
    public static void SendPacket(NetworkProtol np, ISerializePacket msg, ushort msgType)
    {
        WfPacket pak = PooledClassManager <WfPacket> .CreateClass();

        pak.InitWrite(msgType);
        msg.Serialize(pak);
        pak.Swap();
        if (np == NetworkProtol.Tcp)
        {
            SendPacket(np, pak);
        }
        else if (np == NetworkProtol.Kcp)
        {
            //WfPacket pak = new WfPacket(msgType, 548);
            SendPacket(np, pak);
        }
    }
示例#3
0
    //自己给自己发消息,用于测试
    public static void SendPacketToMe(NetworkProtol np, ISerializePacket msg, ushort msgType)
    {
        WfPacket pak = PooledClassManager <WfPacket> .CreateClass();

        pak.InitWrite(msgType);
        msg.Serialize(pak);
        pak.Swap();
        if (np == NetworkProtol.Tcp)
        {
            GameDll.PacketHandlerManager.ProcessPacket(pak);
        }
        else if (np == NetworkProtol.Kcp)
        {
            //KCPPacketHandlerMgr.GetInstance().ProcessPacket(pak);
        }
        pak.DestroyClass();
        pak = null;
    }
示例#4
0
    private void WfNetworkThread()
    {
        HeaderBytes headbytes = PooledClassManager <HeaderBytes> .CreateClass();

        DataBytes databytes  = null;
        int       recvedNum  = 0;
        WfPacket  sendPacket = null;
        int       sendedNum  = 0;

        while (m_Thread.IsAlive)
        {
            try
            {
                Thread.Sleep(10);
                if (m_ConnectState == EConnectState.PostConnecting)
                {
                    databytes = null;
                    recvedNum = 0;
                    if (sendPacket != null)
                    {
                        sendPacket.DestroyClass();
                        sendPacket = null;
                    }
                    sendedNum = 0;
                    ConnectServer();
                }
                if (m_ConnectState == EConnectState.Connected)
                {
                    string errorText = null;
                    bool   bWaiting  = false;
                    do
                    {
                        errorText = RecvAll(ref headbytes, ref databytes, ref recvedNum, ref bWaiting);
                    }while (errorText == null && !bWaiting);
                    if (errorText == null)
                    {
                        object privateLockObject;
                        Monitor.Enter(privateLockObject = m_PrivateLockObject);
                        try
                        {
                            while (m_SendQueue.Count > 0 || sendPacket != null)
                            {
                                if (sendPacket != null)
                                {
                                    errorText = SendAll(sendPacket, ref sendedNum);
                                    if (sendedNum == sendPacket.GetOffset())
                                    {
                                        sendPacket.DestroyClass();
                                        sendPacket = null;
                                    }
                                }
                                if (errorText != null || sendPacket != null || m_SendQueue.Count <= 0)
                                {
                                    break;
                                }
                                sendedNum  = 0;
                                sendPacket = m_SendQueue.Dequeue();
                                sendPacket.SetHeadLength();
                            }
                        }
                        finally
                        {
                            Monitor.Exit(privateLockObject);
                        }
                    }
                    if (errorText != null)
                    {
                        Debug.LogError(errorText);
                        object privateLockObject;
                        Monitor.Enter(privateLockObject = m_PrivateLockObject);
                        try
                        {
                            if (m_ConnectState != EConnectState.PostConnecting)
                            {
                                m_ConnectState     = EConnectState.Disconnected;
                                m_bNetStateChanged = true;
                                //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                            }
                        }
                        finally
                        {
                            Monitor.Exit(privateLockObject);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(ex.Message + ex.StackTrace);
                object privateLockObject;
                Monitor.Enter(privateLockObject = m_PrivateLockObject);
                try
                {
                    if (m_ConnectState != EConnectState.PostConnecting)
                    {
                        m_ConnectState     = EConnectState.Disconnected;
                        m_bNetStateChanged = true;
                        //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                    }
                }
                finally
                {
                    Monitor.Exit(privateLockObject);
                }
            }
        }
        m_Thread.Abort();
        m_Thread.Join();
    }
示例#5
0
    private string RecvAll(ref HeaderBytes msgHeader, ref DataBytes msgData, ref int nRecved, ref bool bWaiting)
    {
        string result;

        if (msgData == null)
        {
            SocketError socketError;
            int         recveNum = m_Socket.Receive(msgHeader.Bytes, nRecved, msgHeader.Bytes.Length - nRecved, SocketFlags.None, out socketError);
            if (recveNum < 0)
            {
                result = "消息头小于0";
                return(result);
            }
            if (socketError != SocketError.Success && socketError != SocketError.WouldBlock)
            {
                result             = "网络错误:" + socketError.ToString();
                m_ConnectState     = EConnectState.PostConnecting;
                m_bNetStateChanged = true;
                //GameDll.CGameProcedure.s_EventManager.OnNetStateChanged.SafeInvoke((int)NetworkProtol.Tcp, (int)m_ConnectState);
                return(result);
            }
            if (recveNum == 0)
            {
                bWaiting = true;
                result   = null;
                return(result);
            }
            nRecved += recveNum;
            if (nRecved == msgHeader.Bytes.Length)
            {
                ushort msgType   = 0;
                ushort msgLength = 0;
                if (!WfPacket.ParseHeader(msgHeader.Bytes, ref msgType, ref msgLength))
                {
                    result = string.Concat(new object[]
                    {
                        "error ParseHeader type:",
                        msgType,
                        "len:",
                        msgLength
                    });
                    return(result);
                }
                if (msgLength < msgHeader.Bytes.Length)
                {
                    result = string.Concat(new object[]
                    {
                        "error ParseHeader < msglen:", msgLength, "headerLength:", msgHeader.Bytes.Length
                    });
                    return(result);
                }
                if (msgLength == msgHeader.Bytes.Length)
                {
                    //这里虽然只有一个是一个很简单的只带有消息头的消息,例如心跳,但是我依然用了完整消息大小
                    msgData = PooledClassManager <DataBytes> .CreateClass();

                    msgData.SetBytes(msgLength + 1);
                    Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length);
                    WfPacket item = PooledClassManager <WfPacket> .CreateClass();

                    item.InitRead(msgData);
                    msgData = null;
                    nRecved = 0;
                    object privateLockObject;
                    Monitor.Enter(privateLockObject = m_PrivateLockObject);
                    try
                    {
                        m_RecvQueue.Enqueue(item);
                    }
                    finally
                    {
                        Monitor.Exit(privateLockObject);
                    }
                }
                else
                {
                    msgData = PooledClassManager <DataBytes> .CreateClass();

                    msgData.SetBytes(msgLength);
                    Array.Copy(msgHeader.Bytes, 0, msgData.GetBytes(), 0, msgHeader.Bytes.Length);
                    nRecved = msgHeader.Bytes.Length;
                }
            }
        }
        if (msgData != null)
        {
            SocketError socketError;
            int         recveNum = m_Socket.Receive(msgData.GetBytes(), nRecved, msgData.GetLength() - nRecved, SocketFlags.None, out socketError);
            //Debug.Log("底层函数接收数据:" + socketError.ToString());
            if (recveNum < 0)
            {
                result = "ReceiveData < 0";
                return(result);
            }
            if (socketError != SocketError.Success && socketError != SocketError.WouldBlock)
            {
                result = "ReceiveData Failed";
                return(result);
            }
            if (recveNum == 0)
            {
                bWaiting = true;
                result   = null;
                return(result);
            }
            nRecved += recveNum;
            if (nRecved > msgData.GetLength())
            {
                result = "ReceiveData IO error";
                return(result);
            }
            if (nRecved == msgData.GetLength())
            {
                WfPacket item = PooledClassManager <WfPacket> .CreateClass();

                item.InitRead(msgData);
                msgData = null;
                nRecved = 0;
                object privateLockObject;
                Monitor.Enter(privateLockObject = m_PrivateLockObject);
                try
                {
                    m_RecvQueue.Enqueue(item);
                }
                finally
                {
                    Monitor.Exit(privateLockObject);
                }
            }
            else
            {
                bWaiting = true;
            }
        }
        result = null;
        return(result);
    }
示例#6
0
    IEnumerator OnLoadAsset(Type t, string abName)
    {
        //为了确保异步,强制添加一个返回
        yield return(null);

        List <LoadAssetRequest> list = null;

        AssetBundleInfo bundleInfo = GetLoadedAssetBundle(abName);

        if (bundleInfo == null)
        {
            yield return(StartCoroutine(OnLoadAssetBundle(abName, t)));

            bundleInfo = GetLoadedAssetBundle(abName);

            if (bundleInfo == null)
            {
                //加载资源失败了
                if (m_LoadRequests.TryGetValue(abName, out list))
                {
                    if (list != null)
                    {
                        int count = list.Count;
                        for (int i = 0; i < count; ++i)
                        {
                            LoadAssetRequest r = list[i];
                            if (r.sharpFunc != null)
                            {
                                //r.sharpFunc(null);
                                //r.sharpFunc = null;
                                r.Error   = true;
                                r.ObjList = null;
                                m_CallBackList.Add(list[i]);
                                //m_CallBackList.Sort(OnSortCallList);
                            }
                        }
                    }
                    m_LoadRequests.Remove(abName);
                }
                Debug.LogError("OnLoadAsset--->>>" + abName);
                yield break;
            }
        }

        //这个位置的异常处理可能有问题,没有合理通知逻辑层资源有问题。
        if (!m_LoadRequests.TryGetValue(abName, out list))
        {
            yield break;
        }
        else
        {
            if (list == null)
            {
                m_LoadRequests.Remove(abName);
                yield break;
            }
        }


        for (int i = 0; i < list.Count; i++)
        {
            string[] assetNames = list[i].assetNames;
            //List<UnityEngine.Object> result = new List<UnityEngine.Object>();
            ABObject result = PooledClassManager <ABObject> .CreateClass();

            AssetBundle ab = bundleInfo.m_AssetBundle;

            for (int j = 0; j < assetNames.Length; j++)
            {
                string assetPath = assetNames[j];

                AssetBundleRequest request = ab.LoadAssetAsync(assetPath, list[i].assetType);
                yield return(request);

                UnityEngine.Object obj = request.asset;
#if UNITY_EDITOR
                if (obj is GameObject)
                {
                    RecoveryShader((GameObject)obj);
                }
#endif
                result.m_UObjectList.Add(obj);
            }
            //Debug.Log("OnLoadAsset " + abName);
            bundleInfo.m_ReferencedCount++;
            //处理依赖
            string[] deps = null;
            if (m_Dependencies.TryGetValue(abName, out deps))
            {
                foreach (var dependency in deps)
                {
                    AssetBundleInfo depABInfo = GetLoadedAssetBundle(dependency);
                    depABInfo.m_ReferencedCount++;
                }
            }

            //将加载完成的id和abName绑定
            m_LoadedIdABName.Add(list[i].LoadIndex, abName);


            //这段代码不能去掉,因为我们的AssetBundleManifest依赖它来回调
            if (list[i].sharpFunc != null)
            {
                //list[i].sharpFunc(result);
                //list[i].sharpFunc = null;
                list[i].ObjList = result;
                m_CallBackList.Add(list[i]);
                //m_CallBackList.Sort(OnSortCallList);
            }
        }

        m_LoadRequests.Remove(abName);

        string[] Dependencies = null;
        if (m_Dependencies.TryGetValue(abName, out Dependencies))
        {
            foreach (var dependency in Dependencies)
            {
                //释放依赖AB
                //AssetBundleInfo depABInfo = GetLoadedAssetBundle(dependency);
                //depABInfo.m_ReferencedCount++;
                m_LoadRequests.Remove(dependency);
            }
        }
    }
示例#7
0
 public static WfPacket CreatePacket()
 {
     return(PooledClassManager <WfPacket> .CreateClass());
 }
示例#8
0
 public static Timer CreateClass()
 {
     return(PooledClassManager <Timer> .CreateClass());
 }