Example #1
0
 public static bool PostMessage(string cmd)
 {
     if (client == null)
     {
         client = new QcMsgClient(null, null);
         client.ConnectedServer += (o, e) =>
         {
             client.Send(cmd);
         };
         client.Connect(QcMessagner.IP, QcMessagner.Port);
     }
     else
     {
         try
         {
             client.Send(cmd);
         }
         catch (Exception e)
         {
             QcLog.LogString("PostMessagee" + e.Message);
             client = null;
             PostMessage(cmd);
         }
     }
     return(true);
 }
Example #2
0
        public static void InitialLicense(string config, string lic)
        {
            dynamic ini;

            ini = new DynamicIniConfig(config);
            QcNetLicense   newnetlicense;
            QcLocalLicense newlocallicense;
            QcTimeLicense  newtimelicense;

            switch (((string)ini.License.IsLocal).ToUpper())
            {
            case "TRUE":
                LocalModel      = true;
                newlocallicense = new QcLocalLicense();
                newlocallicense.SetLicense(QcLicense.ReadLicFile(lic));
                License = newlocallicense;
                break;

            case "FALSE":
                LocalModel    = false;
                newnetlicense = new QcNetLicense();
                newnetlicense.SetServer(ini.MsgServer.Ip, Convert.ToUInt16(ini.MsgServer.Port));
                License = newnetlicense;
                break;

            case "ONLY_IGCES_SUPERTIME":    //HAHA 超级模式
                newtimelicense = new QcTimeLicense(DateTime.Parse("9999-01-01"));
                License        = newtimelicense;
                break;

            case "TRIAL":    //试用模式
                QcLicense timelic = new QcLicense(QcLicense.ReadLicFile(lic), false);
                XDocument doc     = timelic.LicDoc;
                LocalModel = false;
                if (doc.Root.Element("单位名称").Value == "TRIAL")
                {
                    if (DateTime.Now.Subtract(DateTime.Parse(doc.Root.Element("EndDate").Value)).Days <= 0)
                    {
                        newlocallicense = new QcLocalLicense();
                        newlocallicense.SetLicense(QcLicense.ReadLicFile(lic), false);
                        License = newlocallicense;
                    }
                    else
                    {
                        QcLog.LogString("初始授权失败,试用模式下,试用时间已经超出授权时间,请改用其他模式或重新申请授权");
                    }
                }
                else
                {
                    QcLog.LogString("初始授权失败,试用模式下,lic文件不是试用授权文件,请改用其他模式或重新申请授权");
                }

                break;
            }
            if (License == null)
            {
                License = new QcNullLicense();
                QcLog.LogString("初始授权失败,请检查授权模式是否正确");
            }
        }
Example #3
0
        public static DataSet GetDataset(string sql, string tablename = "")
        {
            switch (DbHelper.DBType)
            {
            case DatabaseType.Accdb:

                sql = accessdb_sql_converter(sql);
                break;

            case DatabaseType.Mdb:
                sql = accessdb_sql_converter(sql);
                break;
            }
            OleDbConnection conn = GetConnection();

            //using (OleDbConnection conn = GetConnection())
            //{
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                using (OleDbDataAdapter command = new OleDbDataAdapter(sql, conn))
                {
                    //command.SelectCommand.Parameters.Clear();
                    DataSet ds = new DataSet();
                    //DataTable db = new DataTable();
                    if (tablename == "")
                    {
                        tablename = GetTableNameFromSql(sql);
                    }
                    if (tablename == "")
                    {
                        tablename = "ds";
                    }
                    command.Fill(ds, tablename);
                    return(ds);
                }
            }
            catch (Exception ex)
            {
                QcLog.LogString("GetDataSet" + ex.Message + sql + ex.StackTrace);
                LastException = ex;
                return(null);
            }
            finally
            {
                if (DBType == DatabaseType.Oracle)
                {
                    conn.Close();
                    //conn.Dispose();
                    GC.Collect();
                }
            }
            //}
        }
Example #4
0
 public static string ReadLicFile(string filename)
 {
     try
     {
         QcCoder coder = new QcCoder();
         return(coder.GetEncodingString(System.IO.File.ReadAllBytes(filename), 0));
     }
     catch (Exception e)
     {
         QcLog.LogString(e.Message);
         return("");
     }
 }
Example #5
0
        public static bool Execute(string sql)
        {
            switch (DbHelper.DBType)
            {
            case DatabaseType.Accdb:
                sql = sql.Replace("!=", "<>");
                break;

            case DatabaseType.Mdb:
                sql = sql.Replace("!=", "<>");
                break;
            }

            if (sql == "")
            {
                return(true);
            }
            OleDbConnection conn = GetConnection();

            //using (OleDbConnection conn = GetConnection())
            //{
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                OleDbCommand cmd  = new OleDbCommand(sql, conn);
                int          rows = cmd.ExecuteNonQuery();
                return(true);
            }
            catch (OleDbException e)
            {
                LastException = e;
                QcLog.LogString(e.Message + sql);
            }
            finally
            {
                if (DBType == DatabaseType.Oracle)
                {
                    conn.Close();
                    // conn.Dispose();
                    GC.Collect();
                }
            }
            //}
            return(false);
        }
Example #6
0
        /// <summary>
        /// 使用事务执行一个sql语句
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool Execute(string sql)
        {
            if (sql == "")
            {
                return(true);
            }
            try
            {
                if (Connection.State != System.Data.ConnectionState.Open)
                {
                    Connection.Open();
                }
                OleDbCommand cmd = new OleDbCommand(sql, Connection, m_transaction);

                int rows = cmd.ExecuteNonQuery();
                return(true);
            }
            catch (OleDbException e)
            {
                QcLog.LogString(e.Message + sql);
            }
            return(false);
        }
Example #7
0
        public static Object ExecuteScalar(string sql)
        {
            OleDbConnection conn = GetConnection();

            //using (OleDbConnection conn = GetConnection())
            //{

            //using (OleDbCommand cmd = new OleDbCommand(sql, conn))
            //{
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                return(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                LastException = ex;
                QcLog.LogString(ex.Message + sql);
                return(null);
            }
            finally
            {
                if (DBType == DatabaseType.Oracle)
                {
                    conn.Close();
                    //  conn.Dispose();
                    GC.Collect();
                }
            }
            //}
            //}
        }
        /// <summary>
        ///  虚拟的访问,使用字段名称作为索引访问
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public virtual string this[string key]
        {
            get
            {
                if (key == null)
                {
                    key = row.Table.Columns[0].ColumnName;
                }
                if (row == null)
                {
                    return("");
                }

                if (row.Table.Columns.Contains(key))
                {
                    if (row[key] is DBNull)
                    {
                        return("");
                    }
                    return(row[key].ToString());
                }
                else
                {
                    QcLog.LogString(this.tablename + "不包含字段" + key);
                }
                return("");
            }
            set
            {
                if (row == null)
                {
                    return;
                }
                if (row.Table.Columns.Contains(key))
                {
                    if (row.Table.Columns[key].DataType == typeof(DateTime))
                    {
                        DateTime date = new DateTime();
                        if (DateTime.TryParse(value, out date))
                        {
                            row[key] = value;
                        }
                        else
                        {
                            row[key] = DBNull.Value;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(value))
                        {
                            row[key] = DBNull.Value;
                        }
                        else
                        {
                            row[key] = value;
                        }
                    }
                }
                else
                {
                    throw new Exception("数据库中不存在对应的字段");
                }
            }
        }
Example #9
0
        void server_ReceiveCmd(object sender, QcCmdEventArgs e)
        {
            lock (obj)
            {
                QcCmd  cmd  = e.Cmd;
                string from = e.Cmd.tokens(1);
                string to   = e.Cmd.tokens(2);
                if (lstUser.ContainsKey(from))
                {
                    lstUser[from].LastOnline = DateTime.Now;
                }
                switch (cmd.CmdType)
                {
                case QcProtocol.QcCommand.QcCheckLicense:
                    try
                    {
                        LicenseRetCode ret = LicenseRetCode.未授权;
                        //CONTINUE:
                        //    if (cmd == null) goto CONTINUE;
                        string type = cmd.tokens(1);
                        string arg  = cmd.tokens(2);
                        switch (type)
                        {
                        case "Feature":
                            int id = 0;
                            int.TryParse(arg, out id);
                            ret = License.CheckFeature(id);
                            break;

                        case "Module":
                            ret = License.CheckModule(arg);
                            break;

                        case "Product":
                            ret = License.CheckProduct(arg);
                            break;

                        case "Regsiter":
                            ret = License.RegsisterUser(arg);
                            break;

                        case "Release":
                            License.ReleaseUser(arg);
                            break;
                        }
                        e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcCheckLicense, ret.ToString()));
                        e.Chanel.CloseLink();
                    }
                    catch (Exception ex)
                    {
                        QcLog.LogString("QcCheckLicense   " + cmd.ToString() + ":" + ex.Message);
                    }

                    break;

                case QcProtocol.QcCommand.QcUserLogin:
                    bool blLogined = false;
                    blLogined = true;
                    if (lstUser.ContainsKey(from) == false)
                    {
                        var user = QcUser.RefreshUser(from);
                        if (user != null)
                        {
                            var qmu = new QcMsgUser(user);
                            lstUser.TryAdd(qmu.Name, qmu);
                        }
                    }

                    if (lstUser.ContainsKey(from))
                    {
                        //这里判断一下是否可以登录
                        var msguser = lstUser[from];
                        var user    = msguser.User as QcUser;

                        string sql = "select * from " + QcUser.TableName + " where " + user.CodeField + "='" + user.Code + "' and 状态='启用'";
                        if (DbHelper.Exists(sql))
                        {
                            QcChanel chanel = new QcChanel();
                            chanel.SetChanel(e.Chanel, this);
                            lstUser[from].Chanel = chanel;
                            if (this.Logined != null)
                            {
                                var userfrom = lstUser[from];
                                var evtarg   = new QcMessagerLoginEventArg(userfrom);
                                this.Logined(this, evtarg);
                            }
                            //this.BroadcastMsg(e.Cmd.ToString());
                            this.BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, from, "*"));
                            blLogined = true;
                        }
                        else
                        {
                            blLogined = false;
                        }
                        QcClientService qcs = e.Chanel as QcClientService;
                        e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginReplay, blLogined));
                    }
                    break;

                case QcProtocol.QcCommand.QcListUser:
                    var strusers = "";
                    foreach (var u in OnlineUsers)
                    {
                        strusers += u.Name + ",";
                    }
                    e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcListUser, strusers));
                    break;

                case QcProtocol.QcCommand.QcLoginOut:
                    if (lstUser.ContainsKey(from))
                    {
                        var userfrom = lstUser[from];
                        if (this.Loginout != null)
                        {
                            var evt = new QcMessagerLoginEventArg(userfrom);
                            this.Loginout(this, evt);
                        }
                        this.BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, from, "*"));
                        userfrom.Close();
                    }
                    break;

                case QcProtocol.QcCommand.QcDataUpdate:
                    this.BroadcastMsg(e.Cmd.ToString());
                    break;

                default:
                    if (to == "*")
                    {
                        this.BroadcastMsg(e.Cmd.ToString());
                        return;
                    }
                    else
                    {
                        if (lstUser.ContainsKey(to))
                        {
                            var user = lstUser[to];
                            user.Send(e.Cmd.ToString());
                        }
                    }
                    break;
                }
            }
        }
Example #10
0
        LicenseRetCode PostLicenseMessage(string type, string arg)
        {
            int            count  = 3;
            QcMsgClient    client = new QcMsgClient(null, null);
            AutoResetEvent are    = new AutoResetEvent(false);
            QcCmd          retcmd = null;
            string         cmd    = QcCmd.MakeCmd(QcProtocol.QcCommand.QcCheckLicense, type, arg);

            try
            {
                //client =;
                client.ConnectedServer += (o, e) =>
                {
                    client.Send(cmd);
                };
                client.ReceiveCmd += (o, e) =>
                {
                    retcmd = e.Cmd;
                    are.Set();
                    //接收消息
                };
CONTINUE:
                client.Connect(QcMessagner.IP, QcMessagner.Port);
                if (are.WaitOne(5000))//3秒钟超时连接
                {
                    LicenseRetCode ret = LicenseRetCode.授权未知错误01;
                    //if (retcmd == null)
                    //{
                    //    Thread.Sleep(500);//有可能消息接收 事件还没有给retcmd赋值,因此延迟500毫秒
                    //}
                    if (retcmd != null)
                    {
                        string t_Test = retcmd.tokens(1);
                        ret = (LicenseRetCode)Enum.Parse(typeof(LicenseRetCode), t_Test);
                    }
                    return(ret);
                }
                else
                {
                    if (count > 0)
                    {
                        count--;
                        goto CONTINUE;
                    }
                    else
                    {
                        return(LicenseRetCode.授权服务器连接超时);
                    }
                }
            }
            catch (Exception e)
            {
                QcLog.LogString(e.Message + e.StackTrace);
                //System.Windows.Forms.MessageBox.Show(e.Message + e.StackTrace);
                return(LicenseRetCode.授权未知错误);
            }
            finally
            {
                //这里该怎样断开服务器上的连接呢?
                if (client != null)
                {
                    client.Close();
                }
                are.Set();
            }
        }
Example #11
0
        public void checklicensetext()
        {
            string licensetext = _licensetext;

            try
            {
                XLicDoc = XDocument.Parse(licensetext);
                if (protect.CheckLicense(licensetext))
                {
                    Stats = LicenseRetCode.成功;
                    if (DateTime.Now.CompareTo(DateTime.Parse(LicDoc.Root.Element("EndDate").Value.ToString())) > 0)
                    {
                        Stats = LicenseRetCode.超出授权时间范围;
                    }
                    else
                    {
                        RegistryKey rsg = null;
                        if (Registry.LocalMachine.OpenSubKey("SOFTWARE\\OfficeServer") == null)
                        {
                            if (chksn)//如果要进行机器码检验那么就需要进行注册时间判断
                            {
                                if (DateTime.Now.Subtract(DateTime.Parse(LicDoc.Root.Element("StartDate").Value.ToString())).Days > 7)
                                {
                                    Stats = LicenseRetCode.超出允许授权期限;
                                }
                            }
                            //不论超不超期限都要进行记录系统时间
                            Registry.LocalMachine.CreateSubKey("SOFTWARE", RegistryKeyPermissionCheck.ReadWriteSubTree).CreateSubKey("OfficeServer");
                            rsg = Registry.LocalMachine.CreateSubKey("SOFTWARE").OpenSubKey("OfficeServer", true);
                            rsg.SetValue("PreDate", DateTime.Now.ToShortDateString());
                            rsg.Close();
                        }
                        else
                        {
                            rsg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\OfficeServer", true);
                            if (DateTime.Now.CompareTo(DateTime.Parse(rsg.GetValue("PreDate").ToString())) < 0)
                            {
                                Stats = LicenseRetCode.授权服务系统时间被修改;
                            }
                            else
                            {
                                rsg.SetValue("PreDate", DateTime.Now.ToShortDateString());
                            }
                            rsg.Close();
                        }
                    }
                    QcLog.LogString("授权文件检查成功,当前机器码为:" + new QcProtect().GetSn() + "授权机器码为" + LicDoc.Root.Element("序列号").Value + "\n当前机器特征为:" + new QcProtect().GetHdw());
                    //if (DateTime.Now > DateTime.Parse(LicDoc.Root.Element("EndDate").Value.ToString())) Stats = LicenseRetCode.超出允许授权期限;
                    //创建系统时间注入文件,如果没有则创建,如果有则判断系统时间是否被篡改,如果没有篡改就注入系统时间,如果超过7天时间仍没有注册不允许
                    //创建系统时间注入文件,
                }
                else
                {
                    Stats = LicenseRetCode.未授权;
                    QcLog.LogString("授权文件检查失败,当前机器码为:" + new QcProtect().GetSn() + "授权机器码为" + LicDoc.Root.Element("序列号").Value + "\n当前机器特征为:" + new QcProtect().GetHdw());
                }
                //授权时间判断

                //注册时间判断在这里判断
                //系统服务时间篡改在这里判断
                lstFeature.AddRange(LicDoc.Root.Element("FeatureID").Value.Split('_'));

                lstProduct.AddRange(LicDoc.Root.Element("Product").Value.Split('_'));
                foreach (var v in LicDoc.Root.Element("Modules").Elements("ModuleName"))
                {
                    lstModule.Add(v.Value);
                }

                QcLog.LogString("授权文件读取成功!");
                QcLog.LogString("FeatureList = " + String.Join(";", lstFeature));
                QcLog.LogString("ProductList = " + String.Join(";", lstProduct));
            }
            catch (Exception e)
            {
                Stats = LicenseRetCode.未授权;
                QcLog.LogString("授权文件不合无法读取" + e.Message);
            }
        }