Exemplo n.º 1
0
 /// <summary>
 /// CallBack After Socket Connectd
 /// </summary>
 /// <param name="ar"></param>
 private void ConnectCallback(IAsyncResult ar)
 {
     try
     {
         ar.AsyncWaitHandle.Close();
         if (m_Socket != null)
         {
             m_Socket.EndConnect(ar);
             m_Socket.Blocking       = false;               // 非阻断
             m_Socket.ReceiveTimeout = 3000;
             m_Socket.SendTimeout    = 3000;
         }
         m_State = TNetState.State_Connected;
         if (m_HandleOnConnected != null)
         {
             m_HandleOnConnected(0);
         }
     }
     catch (Exception ex)
     {
         if (m_HandleOnConnected != null)
         {
             m_HandleOnConnected(-1);
         }
         BTDebug.Exception("Socket Connect CallBack Exception:" + ex.Message);
         Disconnect(TNetState.State_ConnectFailed);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 断开连接
 /// </summary>
 /// <param name="state"></param>
 /// <returns></returns>
 public Int32 Disconnect(TNetState state)
 {
     BTDebug.Log("Disconnect For:" + state.ToString(), "NET");
     m_State = state;
     if (m_Socket == null)
     {
         return(0);
     }
     try
     {
         if (m_Socket.Connected == true)
         {
             m_Socket.Shutdown(SocketShutdown.Both);
             if (m_HandleOnDisConnected != null)
             {
                 m_HandleOnDisConnected(0);
             }
         }
     }
     catch (System.Exception ex)
     {
         BTDebug.Exception("ShutDown Socket Error:" + ex.Message);
         if (m_HandleOnDisConnected != null)
         {
             m_HandleOnDisConnected(-1);
         }
     }
     m_Socket.Close();
     m_Socket = null;
     return(0);
 }
Exemplo n.º 3
0
 /// <summary>
 /// 开始加载游戏启动数据
 /// </summary>
 public void StartLoading()
 {
     GameDataManager.Instance.StartLoadData(m_preLoadDataTypeList, OnLoadComplete, (info) =>
     {
         BTDebug.Exception("Load GameData Error");
     });
 }
Exemplo n.º 4
0
        // 加载 由GameDataManager管理
        private static bool Load(Action handleOnLoadComplete, Action <string> handleOnLoadError)
        {
            Action <string> tmpActionOnError = (strInfo) =>
            {
                if (handleOnLoadError != null)
                {
                    handleOnLoadError(strInfo);
                }
            };

            if (m_DataSingleton != null)             // 重复加载
            {
                string strErrorInfo = string.Format("Duplicate Load:{0}", typeof(U));
                BTDebug.Warning(strErrorInfo, "DATA");
                tmpActionOnError(strErrorInfo);
                return(false);
            }
            m_DataSingleton = new GameData <T>();
            string strDataFileKey = GetDataFileKey();

            if (string.IsNullOrEmpty(strDataFileKey) == true)
            {
                string strErrorInfo = string.Format("GameData:{0} Has No DataFileKey", typeof(U).ToString());
                BTDebug.Exception(strErrorInfo, "DATA");
                tmpActionOnError(strErrorInfo);
                return(false);
            }

            if (GameDataManager.Instance.AsynLoadGameDataFile(strDataFileKey, (errorInfo, dataArray) =>
            {
                if (string.IsNullOrEmpty(errorInfo) == false)
                {
                    BTDebug.Exception(string.Format("GameData:{0} Load File Error:{1}", typeof(U), errorInfo), "DATA");
                    tmpActionOnError(errorInfo);
                    return;
                }
                if (m_DataSingleton.ReadFromXML(PackageSetting.cTextFileEncoding.GetString(dataArray)) == false)
                {
                    string info = string.Format("GameData:{0} Parse Failed", typeof(U).ToString());
                    BTDebug.Exception(info, "DATA");
                    tmpActionOnError(errorInfo);
                    return;
                }
                if (handleOnLoadComplete != null)
                {
                    handleOnLoadComplete();
                }
            }
                                                              ) == false)
            {
                string strInfo = string.Format("GameData:{0} Load File Failed", typeof(U).ToString());
                BTDebug.Exception(strInfo, "DATA");
                tmpActionOnError(strInfo);
                return(false);
            }

            return(true);
        }
Exemplo n.º 5
0
        // 发送数据
        public Int32 Send(Byte[] byteArray, Int32 nPos = 0, Int32 nLength = CBTStream.EOS)
        {
            if (m_State != TNetState.State_Connected || m_Socket == null)
            {
                return(-1);
            }

            if (byteArray == null)
            {
                return(-1);
            }
            Int32 nArrayLength = byteArray.Length;

            if (nLength == CBTStream.EOS)
            {
                nLength = nArrayLength - nPos;
            }
            if (nPos < 0 || nPos >= nArrayLength || nLength <= 0 || nLength > nArrayLength - nPos)
            {
                return(-1);
            }

            Int32 nSendStart  = nPos;
            Int32 nSendLength = 0;

            try
            {
                do
                {
                    nSendLength = m_Socket.Send(byteArray, nSendStart, nLength, SocketFlags.None);
                    nSendStart += nSendLength;
                } while (nSendStart < nPos + nLength);
            }
            catch (System.Exception ex)
            {
                BTDebug.Exception("Scoket Send:" + ex.Message);
                return(-1);
            }

            return(0);
        }
Exemplo n.º 6
0
 /// <summary>
 /// 从内存卸载资源
 /// </summary>
 public void UnLoadUnusedAssets()
 {
     BTDebug.Exception("TODO UnLoad");
 }