/// <summary>
        /// Socket 初始化
        /// </summary>
        /// <param name="strAddress">服务器地址</param>
        /// <param name="iPort">服务器端口</param>
        /// <returns>T:成功;F:失败</returns>
        public bool Init(string strAddress, int iPort)
        {
            bool bConnect = false;

            try
            {
                this.strServerAddr = strAddress;
                this.iServerPort   = iPort;

                mTCPClient = new TcpClient();
                mTCPClient.Connect(strAddress, iPort);
                mTCPClient.ReceiveBufferSize = 8192 * 20;
                mStream = mTCPClient.GetStream();

                bConnect = true;
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "无法与连接服务器建立连接!";
                tLogData.strException = e.Message;

                bConnect = false;
            }

            return(bConnect);
        }
        private byte[] GetLongData(byte[] val)
        {
            byte[] bReturn = null;

            try
            {
                //第一次读取数据
                if (this.bLongData == null)
                {
                    this.iLoop     = 0;
                    this.bLongData = RemoveNull(iLoop, val);
                }
                else
                {
                    this.iLoop++;

                    //临时数组
                    byte[] bTempData = RemoveNull(iLoop, val);

                    //保留的长度
                    int iDataLength = this.bLongData.GetLength(0) + bTempData.GetLength(0);
                    bReturn = new byte[iDataLength];

                    //保留原来内容
                    for (int i = 0; i < this.bLongData.GetLength(0); i++)
                    {
                        bReturn[i] = this.bLongData[i];
                    }

                    //增加新内容
                    for (int i = 0; i < bTempData.GetLength(0); i++)
                    {
                        bReturn[i + this.bLongData.GetLength(0) - 1] = bTempData[i];

                        if (bTempData[i] != 0)
                        {
                            //bReturn[i+this.bLongData.GetLength(0)-1] = RemoveNull(iLoop, false, bTempData)[i];
                        }
                    }

                    this.bLongData = bReturn;
                }

                return(this.bLongData);
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "组合Socket数据失败!";
                tLogData.strException = e.Message;

                byte[] bLoopContent = System.Text.Encoding.Default.GetBytes("FAILURE");
                return(bLoopContent);
            }
        }
        /// <summary>
        /// 创建登录框窗体
        /// </summary>
        /// <param name="oParent">父窗体</param>
        /// <param name="oSender">窗体名称</param>
        /// <param name="oEvent">Socket事件</param>
        /// <returns>验证结果</returns>
        public bool CallLogin(object oParent, object oSender, object oEvent)
        {
            //执行结果
            bool bResult = false;
            //判断内容
            string strItem = (string)oSender;
            //模块入口点
            string strEntrance = "CreateModule";

            if (p_Apps.ContainsKey(strItem))
            {
                CModuleForms mModuleForm = (CModuleForms)p_Apps[strItem];
                Assembly     asm         = Assembly.LoadFile(mModuleForm.Path);
                Type[]       types       = asm.GetTypes();

                foreach (Type tCheckType in types)
                {
                    if (tCheckType.IsSubclassOf(typeof(System.Windows.Forms.Form)) && tCheckType.Name == strItem)
                    {
                        object o = Activator.CreateInstance(tCheckType);

                        try
                        {
                            MethodInfo mi = tCheckType.GetMethod(strEntrance, new Type[] { typeof(object), typeof(object) });
                            bResult = (bool)mi.Invoke(o, new Object[] { oParent, oEvent });
                        }
                        catch (Exception e)
                        {
                            bResult = false;

                            CEnum.TLogData tLogData = new CEnum.TLogData();

                            tLogData.strDescribe  = "调用模块信息出错!";
                            tLogData.strException = e.Message;

                            //throw new Exception("调用模块信息出错!");
                        }
                    }
                }
            }
            else
            {
                bResult = false;

                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 2;
                tLogData.strDescribe  = "未找到相应模块信息!";
                tLogData.strException = "N/A";

                //throw new Exception("未找到相应模块信息!");
            }

            return(bResult);
        }
        private byte[] RemoveNull(int Key, byte[] val)
        {
            int iIndexOfEnd = 0;

            byte[] bRetrun = null;

            for (int i = 0; i < val.GetLength(0); i++)
            {
                if (val[i] == 0xEF)
                {
                    iIndexOfEnd = i;
                }
            }

            try
            {
                if (Key == 0)
                {
                    bRetrun = new byte[iIndexOfEnd];
                    System.Array.Copy(val, bRetrun, iIndexOfEnd);
                }
                else if (Key == -1)
                {
                    bRetrun = new byte[val.GetLength(0) + 1];
                    System.Array.Copy(val, bRetrun, val.GetLength(0));
                    bRetrun[val.GetLength(0)] = 0xEF;
                }
                else
                {
                    bRetrun    = new byte[iIndexOfEnd + 1];
                    bRetrun[0] = 0xFD;
                    bRetrun[1] = 0x01;
                    System.Array.Copy(val, 1, bRetrun, 0, iIndexOfEnd - 1);
                }

                return(bRetrun);
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "组合Socket数据失败!";
                tLogData.strException = e.Message;

                return(null);
            }
        }
        /// <summary>
        /// 调用模块窗体 -- 默认方法
        /// </summary>
        /// <param name="oParent">父窗体</param>
        /// <param name="oSender">窗体类</param>
        /// 调用示例:
        ///		1、模块窗体中设置属性
        ///				[C_Global.CModuleAttribute("显示名称", "窗体类名", "功能描述", "隶属范围")]
        ///		2、主窗体中声明变量并赋值(根据需要设置私有或公共类型变量):
        ///				Hashtable m_Apps = C_Global.CModule.Module(模块所在路径,模块扩展名);
        ///		2、根据应用程序模式:
        ///			SDI模式:
        ///				oParent 参数为 NULL (空)
        ///				oSender 参数为 m_Apps的属性(根据函数体内判断选择相应属性)
        ///			MDI模式
        ///				主窗体声明私有静态变量:private static 主窗体类名 定义名称 = null;
        ///				主线程中创建主窗体类 定义名称 = new 主窗体类名();
        ///				修改默认方法 Application.Run(定义名称);
        ///				oParent 参数为 定义名称
        ///				oSender 参数为 m_Apps的属性(根据函数体内判断选择相应属性)
        public void CallForm(object oParent, object oSender)
        {
            string strItem = (string)oSender;

            if (p_Apps.ContainsKey(strItem))
            {
                if (((CModuleForms)p_Apps[strItem]).Form == null)
                {
                    CModuleForms mModuleForm = (CModuleForms)p_Apps[strItem];
                    Assembly     asm         = Assembly.LoadFile(mModuleForm.Path);
                    Type[]       types       = asm.GetTypes();

                    foreach (Type tCheckType in types)
                    {
                        //根据p_Apps属性进行判断
                        if (tCheckType.Name == strItem)
                        {
                            System.Windows.Forms.Form FrmChild = (System.Windows.Forms.Form)Activator.CreateInstance(tCheckType);
                            if (oParent != null)
                            {
                                FrmChild.MdiParent = (System.Windows.Forms.Form)oParent;
                            }
                            FrmChild.Name = strItem;
                            FrmChild.Show();

                            FrmChild.Closing += new CancelEventHandler(CloseForm);

                            ((CModuleForms)p_Apps[strItem]).Form = FrmChild;
                        }
                    }
                }
                else
                {
                    ((CModuleForms)p_Apps[strItem]).Form.Activate();
                }
            }
            else
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 2;
                tLogData.strDescribe  = "未找到相应模块信息!";
                tLogData.strException = "N/A";

                //throw new Exception("未找到相应模块信息!");
            }
        }
        /// <summary>
        /// 组织接收消息
        /// </summary>
        /// <param name="bMsg">接收消息</param>
        /// <returns>CSocketData类</returns>
        public CSocketData SocketRecive(byte[] bMsg)
        {
            try
            {
                CSocketData m_Return = new CSocketData(bMsg, Convert.ToUInt32(bMsg.GetLength(0)));
                return(m_Return);
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "解析Socket数据包失败!";
                tLogData.strException = e.Message;

                throw new Exception("接收数据异常!");
            }
        }
        /// <summary>
        /// 发送数据包
        /// </summary>
        /// <param name="bContent"></param>
        /// <returns></returns>
        public bool SendDate(byte[] bContent)
        {
            try
            {
                mStream.Write(bContent, 0, bContent.GetLength(0));
                return(true);
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "发送Socket数据失败!";
                tLogData.strException = e.Message;

                return(false);
            }
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="eKey">事件类型</param>
        /// <param name="Msg_Category">消息类型</param>
        /// <param name="tBody">消息内容</param>
        /// <returns>客户端消息</returns>
        public CSocketData SocketSend(CEnum.ServiceKey eKey, CEnum.Msg_Category eCategory, CEnum.Message_Body[] tBody)
        {
            try
            {
                int             iMsgLength = 0;                         //消息长度
                byte[]          bMsg       = null;
                TLV_Structure[] tMsg       = null;

                if (tBody != null)
                {
                    iMsgLength = tBody.GetLength(0);
                    tMsg       = new TLV_Structure[iMsgLength];
                    for (int i = 0; i < iMsgLength; i++)
                    {
                        bMsg    = TLV_Structure.ValueToByteArray(tBody[i].eTag, tBody[i].oContent);
                        tMsg[i] = new TLV_Structure(tBody[i].eName, (uint)bMsg.GetLength(0), bMsg);
                    }
                }
                else
                {
                    string strSendMsg = "Get List";
                    iMsgLength = 1;
                    tMsg       = new TLV_Structure[1];
                    bMsg       = System.Text.Encoding.Default.GetBytes(strSendMsg);
                    tMsg[0]    = new TLV_Structure(CEnum.TagName.Connect_Msg, (uint)bMsg.GetLength(0), bMsg);
                }

                Packet_Body body = new Packet_Body(tMsg, (uint)iMsgLength);
                Packet_Head head = new Packet_Head(SeqId_Generator.Instance().GetNewSeqID(), eCategory, eKey, body.m_uiBodyLen);

                CSocketData m_Return = new CSocketData(new Packet(head, body));
                return(m_Return);
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "构造Socket数据包失败!";
                tLogData.strException = e.Message;

                throw new Exception("发送数据异常!");
            }
        }
        /// <summary>
        /// Socket 状态
        /// </summary>
        /// <returns></returns>
        public bool Status()
        {
            bool bCheckStatus = false;

            try
            {
                bCheckStatus = true;
            }
            catch (Exception e)
            {
                bCheckStatus = false;

                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "失去与连接服务器的连接!";
                tLogData.strException = e.Message;
            }
            return(bCheckStatus);
        }
        /// <summary>
        /// 调用模块窗体 -- 传 CSocketEvent 方法
        /// </summary>
        /// <param name="oParent">父窗体</param>
        /// <param name="oSender">窗体类</param>
        /// <param name="oEvent">CSocketEvent 类</param>
        /// 调用示例:
        ///			使用方法同默认方法基本一致,仅需要在模块窗体内声明函数
        ///			函数声明方法:
        ///				public static Form CreateModule(object oParent, object oEvent)
        ///				{
        ///					ModuleFrm mModuleFrm = new ModuleFrm();
        ///					mModuleFrm.m_ClientEvent = (CSocketEvent)oEvent;(模块窗体内声明的变量)
        ///
        ///					//MDI模式
        ///					if (oParent != null)
        ///					{
        ///						mModuleFrm.MdiParent = (Form)oParent;
        ///						mModuleFrm.Show();
        ///					}
        ///					//SDI模式
        ///					else
        ///					{
        ///						mModuleFrm.ShowDialog();
        ///					}
        ///
        ///					return mModuleFrm;
        ///				}
        public void CallForm(object oParent, object oSender, object oEvent)
        {
            //MDI 控制
            bool bDisplay = false;
            //判断内容
            string strItem = (string)oSender;
            //模块入口点
            string strEntrance = "CreateModule";

            //MDI模式
            if (oParent != null)
            {
                //保证子窗体唯一性
                if (((System.Windows.Forms.Form)oParent).MdiChildren.GetLength(0) > 0)
                {
                    for (int i = 0; i < ((System.Windows.Forms.Form)oParent).MdiChildren.GetLength(0); i++)
                    {
                        if (((System.Windows.Forms.Form)oParent).MdiChildren[i].Name == strItem)
                        {
                            bDisplay = true;
                            ((System.Windows.Forms.Form)oParent).MdiChildren[i].Activate();
                            return;
                        }
                        else
                        {
                            bDisplay = false;
                        }
                    }

                    if (!bDisplay)
                    {
                        ((CModuleForms)p_Apps[strItem]).Form = null;

                        if (p_Apps.ContainsKey(strItem))
                        {
                            if (((CModuleForms)p_Apps[strItem]).Form == null)
                            {
                                CModuleForms mModuleForm = (CModuleForms)p_Apps[strItem];
                                Assembly     asm         = Assembly.LoadFile(mModuleForm.Path);
                                Type[]       types       = asm.GetTypes();

                                foreach (Type tCheckType in types)
                                {
                                    if (tCheckType.IsSubclassOf(typeof(System.Windows.Forms.Form)) && tCheckType.Name == strItem)
                                    {
                                        object o = Activator.CreateInstance(tCheckType);

                                        try
                                        {
                                            MethodInfo mi = tCheckType.GetMethod(strEntrance, new Type[] { typeof(object), typeof(object) });
                                            ((CModuleForms)p_Apps[strItem]).Form = (System.Windows.Forms.Form)mi.Invoke(o, new Object[] { oParent, oEvent });
                                        }
                                        catch (Exception e)
                                        {
                                            CEnum.TLogData tLogData = new CEnum.TLogData();

                                            tLogData.iSort        = 5;
                                            tLogData.strDescribe  = "调用模块信息出错!";
                                            tLogData.strException = e.Message;

                                            //throw new Exception("调用模块信息出错!");
                                        }
                                    }
                                }
                            }
                            else
                            {
                                ((CModuleForms)p_Apps[strItem]).Form.Activate();
                            }
                        }
                        else
                        {
                            CEnum.TLogData tLogData = new CEnum.TLogData();

                            tLogData.iSort        = 2;
                            tLogData.strDescribe  = "未找到相应模块信息!";
                            tLogData.strException = "N/A";

                            //throw new Exception("未找到相应模块信息!");
                        }
                    }
                }
                else
                {
                    if (p_Apps.ContainsKey(strItem))
                    {
                        ((CModuleForms)p_Apps[strItem]).Form = null;

                        if (((CModuleForms)p_Apps[strItem]).Form == null)
                        {
                            CModuleForms mModuleForm = (CModuleForms)p_Apps[strItem];
                            Assembly     asm         = Assembly.LoadFile(mModuleForm.Path);
                            Type[]       types       = asm.GetTypes();

                            foreach (Type tCheckType in types)
                            {
                                if (tCheckType.IsSubclassOf(typeof(System.Windows.Forms.Form)) && tCheckType.Name == strItem)
                                {
                                    object o = Activator.CreateInstance(tCheckType);

                                    try
                                    {
                                        MethodInfo mi = tCheckType.GetMethod(strEntrance, new Type[] { typeof(object), typeof(object) });
                                        ((CModuleForms)p_Apps[strItem]).Form = (System.Windows.Forms.Form)mi.Invoke(o, new Object[] { oParent, oEvent });
                                    }
                                    catch (Exception e)
                                    {
                                        CEnum.TLogData tLogData = new CEnum.TLogData();

                                        tLogData.iSort        = 5;
                                        tLogData.strDescribe  = "调用模块信息出错!";
                                        tLogData.strException = e.Message;

                                        //throw new Exception("调用模块信息出错!");
                                    }
                                }
                            }
                        }
                        else
                        {
                            ((CModuleForms)p_Apps[strItem]).Form.Activate();
                        }
                    }
                    else
                    {
                        CEnum.TLogData tLogData = new CEnum.TLogData();

                        tLogData.iSort        = 2;
                        tLogData.strDescribe  = "未找到相应模块信息!";
                        tLogData.strException = "N/A";

                        //throw new Exception("未找到相应模块信息!");
                    }
                }
            }
            //SDI模式
            else
            {
                if (p_Apps.ContainsKey(strItem))
                {
                    ((CModuleForms)p_Apps[strItem]).Form = null;

                    if (((CModuleForms)p_Apps[strItem]).Form == null)
                    {
                        CModuleForms mModuleForm = (CModuleForms)p_Apps[strItem];
                        Assembly     asm         = Assembly.LoadFile(mModuleForm.Path);
                        Type[]       types       = asm.GetTypes();

                        foreach (Type tCheckType in types)
                        {
                            if (tCheckType.IsSubclassOf(typeof(System.Windows.Forms.Form)) && tCheckType.Name == strItem)
                            {
                                object o = Activator.CreateInstance(tCheckType);

                                try
                                {
                                    MethodInfo mi = tCheckType.GetMethod(strEntrance, new Type[] { typeof(object), typeof(object) });
                                    ((CModuleForms)p_Apps[strItem]).Form = (System.Windows.Forms.Form)mi.Invoke(o, new Object[] { oParent, oEvent });
                                }
                                catch (Exception e)
                                {
                                    CEnum.TLogData tLogData = new CEnum.TLogData();

                                    tLogData.iSort        = 5;
                                    tLogData.strDescribe  = "调用模块信息出错!";
                                    tLogData.strException = e.Message;

                                    //throw new Exception("调用模块信息出错!");
                                }
                            }
                        }
                    }
                    else
                    {
                        ((CModuleForms)p_Apps[strItem]).Form.Activate();
                    }
                }
                else
                {
                    CEnum.TLogData tLogData = new CEnum.TLogData();

                    tLogData.iSort        = 2;
                    tLogData.strDescribe  = "未找到相应模块信息!";
                    tLogData.strException = "N/A";

                    //throw new Exception("未找到相应模块信息!");
                }
            }
        }
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <returns></returns>
        public byte[] ReceiveData()
        {
            try
            {
                //无限循环 -- 可读数据时退出
                DateTime mStarTime = DateTime.Now;
                for (; ;)
                {
                    DateTime mEndTime = DateTime.Now;

                    if ((mEndTime.Minute - mStarTime.Minute) > 4)
                    {
                        CEnum.TLogData tLogData = new CEnum.TLogData();

                        tLogData.iSort        = 5;
                        tLogData.strDescribe  = "接收Socket数据超时!";
                        tLogData.strException = "N/A";

                        byte[] bContent = System.Text.Encoding.Default.GetBytes("FAILURE");
                        return(bContent);
                    }

                    //防止无限循环引起的 CPU 占用率过高现象
                    Thread.Sleep(500);


                    if (mStream.DataAvailable == true && mStream.CanRead)
                    {
                        byte[] bReturn = null;

                        int    iLength  = LengthOfReceive();
                        byte[] bContent = new byte[iLength];
                        mStream.Read(bContent, 0, iLength);

                        //非多组数据
                        if (System.Text.Encoding.Default.GetString(bContent, 0, 5) != "BEGIN" && System.Text.Encoding.Default.GetString(bContent, 0, 3) != "END")
                        {
                            return(bContent);
                        }
                        //多组数据
                        else
                        {
                            //循环读取内容
                            for (; ;)
                            {
                                try
                                {
                                    //读取数据
                                    if (mStream.DataAvailable == true && mStream.CanRead)
                                    {
                                        int    iLoopLength  = LengthOfReceive();
                                        byte[] bLoopContent = new byte[iLoopLength];
                                        mStream.Read(bLoopContent, 0, iLoopLength);

                                        //退出循环
                                        if (System.Text.Encoding.Default.GetString(bLoopContent, 0, 3) == "END")
                                        {
                                            string ssd = System.Text.Encoding.Default.GetString(bReturn);
                                            byte[] tt  = RemoveNull(-1, bReturn);
                                            return(tt);
                                        }

                                        //组织多组数据
                                        bReturn = GetLongData(bLoopContent);

                                        //退出循环
                                        if (System.Text.Encoding.Default.GetString(bReturn) == "FAILURE")
                                        {
                                            return(bReturn);
                                        }

                                        //降低CPU占用率
                                        Thread.Sleep(500);

                                        //发送完成标志
                                        SendDate(System.Text.Encoding.Default.GetBytes("OK"));
                                    }
                                }
                                catch (Exception e)
                                {
                                    CEnum.TLogData tLogData = new CEnum.TLogData();

                                    tLogData.iSort        = 5;
                                    tLogData.strDescribe  = "接收多组Socket数据失败!";
                                    tLogData.strException = e.Message;

                                    byte[] bLoopContent = System.Text.Encoding.Default.GetBytes("FAILURE");
                                    return(bLoopContent);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                CEnum.TLogData tLogData = new CEnum.TLogData();

                tLogData.iSort        = 5;
                tLogData.strDescribe  = "接收Socket数据失败!";
                tLogData.strException = e.Message;

                byte[] bLoopContent = System.Text.Encoding.Default.GetBytes("FAILURE");
                return(bLoopContent);
            }
            finally
            {
            }
        }