Beispiel #1
0
        /// <summary>
        /// 主动断线
        /// </summary>
        public void DisConnect(bool NeedReconnect = true)
        {
            if (!m_IsConnected)
            {
                return;
            }

            CLOG.I("DisConnect!");
            m_IsConnected = false;

            if (m_SocketInstance != null)
            {
                m_SocketInstance.CloseConnect();
            }

            if (m_Sender != null)
            {
                m_Sender.ClearMessage();
            }

            if (m_Receiver != null)
            {
                m_Receiver.ClearMessage();
            }

            // 如果需要主动重连,那么压入连接丢失信号
            // 由应用层检测到断线进行处理
            // 否则直接断线即可
            if (NeedReconnect)
            {
                m_Connecter.PushConnectSignal(EConnectSignal.CONNECT_LOST);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 网络初始化
        /// 创建Socket
        /// 启动各个线程
        /// </summary>
        public void Init()
        {
            CLOG.I("-- Network init --");

            // 注册所有网络消息包反射信息
            CPackageManager.Instance.PerpareNetworkPackageReflectionInformation();

            // 创建Socket
            m_SocketInstance = new CSocket();

            // 创建连接线程
            m_Connecter = new CThreadConnecter(this);

            // 创建收线程
            m_Receiver = new CThreadReceiver(this);

            // 创建发送线程
            m_Sender = new CThreadSender(this);

            m_Connecter.Start();
            m_Sender.Start();
            m_Receiver.Start();

            m_HasInit = true;
        }
Beispiel #3
0
        /// <summary>
        /// 应用退出
        /// </summary>
        private void OnApplicationQuit()
        {
            CLOG.I("Application Quit");

            if (EventAppQuit != null)
            {
                EventAppQuit();
            }
        }
Beispiel #4
0
 /// <summary>
 /// 尝试连接
 /// </summary>
 public void TryConnect()
 {
     //如果已经连接就算了
     if (m_IsConnected)
     {
         CLOG.W("you has already connect to server ! so can not try to connect to server!");
         return;
     }
     CLOG.I("Try to connect to the server < IP:{0} Port:{1} >", IP, Port);
     m_Connecter.TryConnect();
 }
Beispiel #5
0
 /// <summary>
 /// 立刻切换到目标场景
 /// </summary>
 /// <param name="TargetScene">目标场景</param>
 public void ChangeSceneImmediately(string SceneName)
 {
     try
     {
         CLOG.I("ready to load scene {0} immediate", SceneName);
         CompleteCallback = null;
         SceneManager.LoadScene(SceneName, LoadSceneMode.Single);
     }
     catch (Exception ex)
     {
         CLOG.E((ex.ToString()));
     }
 }
Beispiel #6
0
        /// <summary>
        /// 应用程序焦点变化
        /// </summary>
        /// <param name="focus"></param>
        private void OnApplicationFocus(bool focus)
        {
            CLOG.I("Application Focus state {0}", focus);
            IsApplecationFocus = focus;

            if (focus)
            {
                CheckSwitchIn();
            }
            else
            {
                CheckSwitchOut();
            }
        }
Beispiel #7
0
        /// <summary>
        /// 应用程序暂停状态变化
        /// </summary>
        /// <param name="pause"></param>
        private void OnApplicationPause(bool pause)
        {
            CLOG.I("Application Pause state {0}", pause);
            IsApplecationPause = pause;

            if (pause)
            {
                CheckSwitchOut();
            }
            else
            {
                CheckSwitchIn();
            }
        }
Beispiel #8
0
        /// <summary>
        /// 退出
        /// </summary>
        public void Exit()
        {
            CLOG.I("End Game");

#if UNITY_EDITOR
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                EditorApplication.isPlaying = false;
                return;
            }
#endif

            Application.Quit();
        }
Beispiel #9
0
        /// <summary>
        /// 每帧执行
        /// </summary>
        private void Update()
        {
            for (int i = 0; i < MissionSeqs.Count; i++)
            {
                if (MissionSeqs[i].Update( ))
                {
                    CLOG.I("the mission Seq index {0} has finish", i);
                    MissionSeqs.RemoveAt(i);
                }
            }

#if UNITY_EDITOR
            Instance.m_SequenceCount = Instance.MissionSeqs.Count;
#endif
        }
Beispiel #10
0
        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="SceneName">场景名</param>
        /// <param name="AutoSwitch">是否在加载完成自动切换到目标场景,如果切换场景使用了加载界面,那么一般需要等加载界面完成动画后再来切换,而没有使用加载界面的,可以自动切换场景</param>
        /// <returns></returns>
        IEnumerator LoadScene(string SceneName, bool AutoSwitch)
        {
            CLOG.I("ready to load scene {0} asyn", SceneName);
            m_AsyncOperator = SceneManager.LoadSceneAsync(SceneName);
            m_AsyncOperator.allowSceneActivation = AutoSwitch;
            yield return(m_AsyncOperator);

            if (CompleteCallback != null && m_AsyncOperator != null)
            {
                yield return(new WaitUntil(() => { return m_AsyncOperator.isDone; }));

                CLOG.I("scene {0} was load complete!", SceneName);
                CompleteCallback();
                m_AsyncOperator  = null;
                CompleteCallback = null;
            }
        }
Beispiel #11
0
        /// <summary>
        /// 开始读取
        /// </summary>
        /// <param name="URL"></param>
        /// <param name="CompleteCallBack"></param>
        /// <param name="LoadingCallBack"></param>
        /// <param name="EHT"> URL位置是本地还是网络 </param>
        /// <returns>协程</returns>
        private IEnumerator StartLoad(string URL, DelegateHttpLoadComplete CompleteCallBack = null, DelegateHttpLoading LoadingCallBack = null, EHttpLocation EHT = EHttpLocation.REMOTE)
        {
            if (EHT == EHttpLocation.LOCAL)
            {
                URL = "file:///" + Application.persistentDataPath + "/" + URL;
                CLOG.I("read local file uri={0}", URL);
            }
            else
            {
                CLOG.I("read remote file url={0}", URL);
            }

            WWW HttpObj = new WWW(URL);

            m_NowRunningLoaderCount++;

            yield return(StartCoroutine(Loading(HttpObj, CompleteCallBack, LoadingCallBack)));

            m_NowRunningLoaderCount--;
            HttpObj.Dispose();
        }
Beispiel #12
0
        /// <summary>
        /// 得到数据
        /// </summary>
        /// <returns></returns>
        public byte[] GetData()
        {
            byte[] finalData = new byte[4 + m_BodyLength];
            int    index     = 0;

            //压入OpCode
            byte[] data = BitConverter.GetBytes(m_OpCode);
            finalData[index++] = data[0];
            finalData[index++] = data[1];

            CLOG.I("Get Data OpCode is {0} {1}", m_OpCode, Convert.ToString(m_OpCode, 2).PadLeft(16, '0'));

            //压入头长度
            data = BitConverter.GetBytes(m_BodyLength);
            finalData[index++] = data[0];
            finalData[index++] = data[1];

            CLOG.I("Get Data BodyLength is {0} {1}", m_BodyLength, Convert.ToString(m_BodyLength, 2).PadLeft(16, '0'));

            //压入身体
            data = m_Body.Buffer;
            if (data.Length < m_BodyLength)
            {
                CLOG.E("the ready push body data {0} bytes, but the message object body has only {1} bytes", m_BodyLength, data.Length);
                return(null);
            }

            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < m_BodyLength; i++)
            {
                finalData[index++] = data[i];
                sb.Append(Convert.ToString(data[i], 2).PadLeft(8, '0') + ",");
            }
            CLOG.I("Get Data is {0}", sb.ToString());


            return(finalData);
        }
Beispiel #13
0
        /// <summary>
        /// Image this扩展,读取图片
        /// </summary>
        /// <param name="image">Imagte组件</param>
        /// <param name="URL">网络URL头像地址</param>
        /// <param name="LoadCompleteCallBack">加载完毕回调</param>
        public static void LoadURLImage(this Image image, string URL, DelegateURLImageLoadComplete LoadCompleteCallBack = null)
        {
            if (URL == null || URL == "")
            {
                return;
            }

            //文件类型
            string TargetFileType = GetFileType(URL);

            //得到文件名
            string TargetFileName = GetMD5HeadName(URL) + "." + TargetFileType;

            //本地文件名,用于判断文件是否存在
            string LoacalFileName = Application.persistentDataPath + "/" + TargetFileName;

            //判断本地文件是否存在
            FileInfo FI = new FileInfo(LoacalFileName);

            //存在就读取本地文件,不存在就读取网络文件
            EHttpLocation EHT = FI.Exists ? EHttpLocation.LOCAL : EHttpLocation.REMOTE;

            //读取完毕回调
            DelegateHttpLoadComplete OnComplete = (WWW HttpObj, bool isSuccess) =>
            {
                try
                {
                    //加载失败时的回调
                    if (!isSuccess)
                    {
                        if (LoadCompleteCallBack != null)
                        {
                            LoadCompleteCallBack(isSuccess);
                        }

                        return;
                    }

                    //加载成功,但是头像已销毁
                    if (image == null)
                    {
                        return;
                    }

                    //得到Texture
                    Texture2D texture = HttpObj.texture;

                    if (texture == null)
                    {
                        return;
                    }

                    //设置Image图片精灵
                    image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);

                    //只有远程文件才需要保存
                    if (EHT == EHttpLocation.REMOTE)
                    {
                        //得到字节流
                        byte[] ImageData = null;
                        if (TargetFileType == "jpg" || TargetFileType == "jpeg")
                        {
                            ImageData = texture.EncodeToJPG();
                        }
                        else if (TargetFileType == "png")
                        {
                            ImageData = texture.EncodeToPNG();
                        }

                        //保存路径
                        string SaveURl = Application.persistentDataPath + "/" + TargetFileName;
                        CLOG.I("read http remote data complete , Start save image to {0}!", SaveURl);

                        //保存文件
                        if (ImageData != null)
                        {
                            File.WriteAllBytes(SaveURl, ImageData);
                        }
                        else
                        {
                            CLOG.E("Write file {0} error!", SaveURl);
                        }
                    }

                    //执行回调
                    if (LoadCompleteCallBack != null)
                    {
                        LoadCompleteCallBack(isSuccess);
                    }
                }
                catch (Exception ex)
                {
                    CLOG.I("**********  don't warry it's OK  ************");
                    CLOG.E(ex.ToString());
                    CLOG.I("*********************************************");
                }
            };

            //读取中回调
            DelegateHttpLoading OnLoading = null;

            /* (WWW HttpObj) =>
             * {
             *
             * };
             */

            //创建HTTP加载器
            if (EHT == EHttpLocation.REMOTE)
            {
                //加载网络头像
                CHttp.Instance.CreateHttpLoader(URL, OnComplete, OnLoading, EHT);
            }
            else if (EHT == EHttpLocation.LOCAL)
            {
                //加载本地文件
                CHttp.Instance.CreateHttpLoader(TargetFileName, OnComplete, OnLoading, EHT);
            }
        }