Example #1
0
        public void Tick()
        {
            FileDataBuff buff = PopRecvData();

            while (buff != null)
            {
                if (!Invoke(buff))
                {
                    LogManager.Debug("TcpSession::Tick => invoke error");
                    Stop();
                    return;
                }
                buff.Clear();
                buff = PopRecvData();
            }
        }
        public override IEnumerator DoWork()
        {
            float beginTime = Time.unscaledTime;

            while (_levelName == null)
            {
                if (Time.unscaledTime - beginTime > 5.0f)
                {
                    LogManager.Debug("[LevelLoadingUnit::DoWork]未指定场景");
                    yield break;
                }
                else
                {
                    yield return(null);
                }
            }

            _asyncOp = UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(_levelName);
            yield return(_asyncOp);
        }
Example #3
0
        public void Start(string ip, int port)
        {
            Stop();
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            if (_socket == null)
            {
                LogManager.Debug("TcpSession::Start => fail to create socket");
                return;
            }

            try
            {
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
                _socket.BeginConnect(endPoint, OnConnectCallback, this);
            }
            catch
            {
                _socket = null;
                LogManager.Debug("TcpSession::Start => fail to connect");
            }
        }
Example #4
0
        protected void RecvBytes()
        {
            if (!IsConnected())
            {
                LogManager.Debug("TcpSession::SendBytes => not connected");
                return;
            }

            if (_bRecving)
            {
                return;
            }

            try
            {
                DataBuff buff = null;
                lock (_recvLocker)
                {
                    if (_recvBuffList.Count > 0)
                    {
                        int index = _recvBuffList.Count - 1;
                        if (_recvBuffList[index] != null && _recvBuffList[index].GetUnwriteLen() > 0)
                        {
                            buff = _recvBuffList[index];
                            _recvBuffList.RemoveAt(index);
                        }
                    }
                }

                if (buff == null)
                {
                    buff = ObjectPool <DataBuff> .Pop();
                }

                _socket.BeginReceive(buff._data, 0, (int)buff.GetUnwriteLen(), SocketFlags.None, OnRecvBytesCallback, buff);
                _bRecving = true;
            }
            catch (SocketException e) { LogManager.Debug(string.Format("TcpSession::RecvBytes => error [{0}] [{1}]", e.ErrorCode, e.Message)); }
            catch { LogManager.Debug("TcpSession::RecvBytes => unknown error"); }
        }
Example #5
0
        public IEnumerator LoadAssetBundle(string name)
        {
            AssetBundleRef abRef = null;

            if (mRefDict.TryGetValue(name, out abRef))
            {
                abRef.IncRef();
                while (abRef.mReq != null && abRef.mAB == null)
                {
                    yield return(null);
                }
                yield break;
            }

            abRef = new AssetBundleRef();
            if (abRef == null)
            {
                yield break;
            }
            abRef.IncRef();
            mRefDict.Add(name, abRef);

            abRef.mReq = AssetBundle.LoadFromFileAsync(VersionManager.Instance.GetFilePath("AssetBundles", name));
            yield return(abRef.mReq);

            // 创建失败或者被强行中断
            if (abRef.mReq == null)
            {
                yield break;
            }
            // 加载失败
            if (abRef.mReq.assetBundle == null)
            {
                LogManager.Debug(string.Format("{0}::{1} => can't load {2}", "AssetBundleManager", "LoadAssetBundle", name));
                yield break;
            }

            abRef.mAB  = abRef.mReq.assetBundle;
            abRef.mReq = null;
        }
Example #6
0
        protected void SendBytes()
        {
            if (!IsConnected())
            {
                LogManager.Debug("TcpSession::SendBytes => not connected");
                return;
            }

            if (_bSending)
            {
                return;
            }

            DataBuff buff = null;

            lock (_sendLocker)
            {
                if (_sendBuffList.Count <= 0)
                {
                    return;
                }

                buff = _sendBuffList[0];
                _sendBuffList.RemoveAt(0);
            }

            if (buff == null)
            {
                return;
            }

            try
            {
                _socket.BeginSend(buff._data, 0, (int)buff._dataLen, SocketFlags.None, OnSendBytesCallback, buff);
                _bSending = true;
            }
            catch (SocketException e) { LogManager.Debug(string.Format("TcpSession::SendBytes => error [{0}] [{1}]", e.ErrorCode, e.Message)); }
            catch { LogManager.Debug("TcpSession::SendBytes => unknown error"); }
        }
Example #7
0
        public void Stop()
        {
            if (IsConnected())
            {
                bool bSocketClosed = false;
                try { _socket.Shutdown(SocketShutdown.Both); }
                catch (ObjectDisposedException e)
                {
                    bSocketClosed = true;
                    LogManager.Debug(string.Format("TcpSession::Stop => socket disposed (ok) [{0}]", e.Message));
                }
                catch (SocketException e) { LogManager.Debug(string.Format("TcpSession::Stop => error [{0}] [{1}]", e.ErrorCode, e.Message)); }
                catch { LogManager.Debug("TcpSession::Stop => unknown error"); }

                if (!bSocketClosed)
                {
                    try { _socket.Close(); }
                    catch { LogManager.Debug("TcpSession::Stop => close socket error"); }
                }
            }

            _socket = null;
        }
Example #8
0
        protected void OnSendBytesCallback(IAsyncResult res)
        {
            try
            {
                int len = _socket.EndSend(res);
                _bSending = false;

                DataBuff buff = res.AsyncState as DataBuff;
                if (buff == null || len != buff._dataLen)
                {
                    LogManager.Debug("TcpSession::OnSendBytesCallback => send error");
                    Stop();
                    return;
                }

                buff.Clear();
                ObjectPool <DataBuff> .Push(buff);

                SendBytes();
            }
            catch (SocketException e) { LogManager.Debug(string.Format("TcpSession::OnSendBytesCallback => error [{0}] [{1}]", e.ErrorCode, e.Message)); }
            catch { LogManager.Debug("TcpSession::OnSendBytesCallback => unknown error"); }
        }
Example #9
0
        public IEnumerator LoadAsset(string name, System.Type type = null)
        {
            AssetRef assetRef = null;

            if (mRefDict.TryGetValue(name, out assetRef))
            {
                while (!assetRef.mLoaded && assetRef.mAsset == null)
                {
                    yield return(null);
                }
                yield break;
            }

            string bundleName;

            if (!mAsset2BundleDict.TryGetValue(name, out bundleName))
            {
                LogManager.Debug("AssetManager::LoadAsset => can't find bundle name for asset " + name);
                yield break;
            }

            assetRef = new AssetRef();
            if (assetRef == null)
            {
                yield break;
            }
            mRefDict.Add(name, assetRef);

            string[] dependencies = AssetBundleManager.Instance.GetAssetBundleDependencies(bundleName);

            Coroutine[] crtList = new Coroutine[dependencies.Length + 1];
            crtList[crtList.Length - 1] = StartCoroutine(AssetBundleManager.Instance.LoadAssetBundle(bundleName));
            for (int i = 0; i < dependencies.Length; ++i)
            {
                crtList[i] = StartCoroutine(AssetBundleManager.Instance.LoadAssetBundle(dependencies[i]));
            }

            int waitIndex = 0;

            while (waitIndex < crtList.Length)
            {
                yield return(crtList[waitIndex]);

                ++waitIndex;
            }

            AssetBundle ab = AssetBundleManager.Instance.GetAssetBundle(bundleName);

            if (ab != null)
            {
                AssetBundleRequest req = ab.LoadAssetAsync(name, type == null ? typeof(GameObject) : type);
                if (req != null)
                {
                    yield return(req);

                    assetRef.mAsset = req.asset;
                }
            }
            assetRef.mLoaded = true;

            AssetBundleManager.Instance.UnloadAssetBundle(bundleName);
            for (int i = 0; i < dependencies.Length; ++i)
            {
                AssetBundleManager.Instance.UnloadAssetBundle(dependencies[i]);
            }
        }