Exemplo n.º 1
0
        //System.Timers.Timer m_Timer;

        private void ConnectServer()
        {
            try
            {
                if (client == null)
                {
                    client = new QcMsgClient(QcUser.Users, QcUser.User);
                    client.ConnectedServer += (o, e) =>
                    {
                        client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, User.Name, "*"));
                        //client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, "*", User.Name));
                    };
                    client.DataUpdate += (o, e) => { if (this.DataUpdate != null)
                                                     {
                                                         this.DataUpdate(o, e);
                                                     }
                    };
                    client.DisConnectedServer += (o, e) =>
                    {
                        client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, User.Name, "*"));
                    };
                    client.ReceiveCmd += QcMessagner_ReciveCmd;
                }
                client.Connect(ip, port);
            }
            catch (Exception ex)
            {
                QcLog.LogException("MessagnerCS", ex);
            }
        }
Exemplo n.º 2
0
        public virtual new void Start(ushort port, string ip)
        {
            base.Users = QcUser.Users;
            foreach (var v in base.Users)
            {
                var qmu = new QcMsgUser(v);
                lstUser.TryAdd(qmu.Name, qmu);
            }
            this.port = port;
            this.ip   = ip;
            this.User = QcUser.User;
            object state = new object();

            m_Timer = new System.Threading.Timer(m_Timer_Elapsed, state, 100000, 100000);
            ConnectServer();
            Task.Factory.StartNew(() =>
            {
                QcNode.NodeUpdateToDb += (o, e) =>
                {
                    //发现数据节点更新,就广播节点编码和表名
                    QcNode node = o as QcNode;
                    if (node != null)
                    {
                        client.Send(
                            QcCmd.MakeCmd(QcProtocol.QcCommand.QcDataUpdate
                                          , node.Code
                                          , node.TableName
                                          , e.ChangeType
                                          , e.Node.CodeField
                                          , (e.ChangeType == NodeChangeType.Delete) ? e.Node.ToString() : ""
                                          ));
                    }
                };
            });
        }
Exemplo n.º 3
0
 //  System.Threading.Timer keepalivetimer = null;
 public override void Start(ushort port, string ip = "")
 {
     this.port = port;
     SaveIpToDb();
     base.Users = QcUser.Users;
     this.User  = QcUser.User;
     base.Start(port);
     this.ReciveCmd        += QcMessagner_ReciveCmd;
     QcNode.NodeUpdateToDb += (o, e) =>
     {
         //发现数据节点更新,就广播节点编码和表名
         QcNode node = o as QcNode;
         if (node != null)
         {
             this.BroadcastMsg(
                 QcCmd.MakeCmd(QcProtocol.QcCommand.QcDataUpdate
                               , node.Code
                               , node.TableName
                               , e.ChangeType
                               , e.Node.CodeField
                               , (e.ChangeType == NodeChangeType.Delete)?  e.Node.ToString():""
                               ));
         }
     };
 }
Exemplo n.º 4
0
 void m_Timer_Elapsed(object sender)
 {
     if (client == null)
     {
         ConnectServer();
     }
     else
     {
         if (QcUser.User != null)
         {
             client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcKeepAlive, QcUser.User.Name));
         }
     }
 }
Exemplo n.º 5
0
        public bool SendMsg(string Name, string Msg, QcProtocol.QcCommand cmd = QcProtocol.QcCommand.QcMsg)
        {
            //发送消息到用户
            QcMsgUser user = null;

            if (lstUser.TryGetValue(Name, out user))
            {
                if (user.Send(QcCmd.MakeCmd(cmd, User.Name, Name, Msg)))
                {
                    return(true);
                }
            }
            //存储消息到缓存组
            return(false);
        }
Exemplo n.º 6
0
 void TimerCallBack(object o)
 {
     System.Threading.Tasks.Task.Factory.StartNew(() =>
     {
         foreach (var v in lstUser.Values.Where(t => t.IsLinked))
         {
             TimeSpan t = DateTime.Now - v.LastOnline;
             //超过3分钟以上,就关闭
             if (t.TotalSeconds > 100)
             {
                 v.Close();
                 if (this.Loginout != null)
                 {
                     var evt = new QcMessagerLoginEventArg(v);
                     this.Loginout(this, evt);
                 }
                 this.BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, "*", v.Name));
             }
         }
     });
 }
Exemplo n.º 7
0
 public bool SendMsg(string Name, string Msg, QcProtocol.QcCommand cmd = QcProtocol.QcCommand.QcMsg)
 {
     /*
      * //发送消息到用户
      * QcMsgUser user =null;
      * if(lstUser.TryGetValue(Name,out user))
      * {
      *  if(user.Send(QcCmd.MakeCmd(cmd,User.Name,Name,Msg,DateTime.Now.ToString() )))
      *  {
      *      return true;
      *  }
      * }
      * return false;
      * //存储消息到缓存组
      * */
     if (client == null)
     {
         return(false);
     }
     return(client.Send(QcCmd.MakeCmd(cmd, QcUser.User.Name, Name, Msg, DateTime.Now.ToString())));
 }
Exemplo n.º 8
0
        public bool SendSystemMsg(string ID, string DestUser, string Msg)
        {
            QcProtocol.QcCommand cmd = QcProtocol.QcCommand.QcSystemMsg;
            //发送消息到用户

            /*QcMsgUser user =null;
             * if(lstUser.TryGetValue(DestUser,out user))
             * {
             *  if (user.Send(QcCmd.MakeCmd(cmd, User.Name, DestUser,Msg, DateTime.Now.ToString(), ID)))
             *  {
             *      return true;
             *  }
             * }
             * return false
             * //存储消息到缓存组
             * */
            //直接发送给服务器,服务器负责转发
            if (client == null)
            {
                return(false);
            }
            return(client.Send(QcCmd.MakeCmd(cmd, QcUser.User.Name, DestUser, Msg, DateTime.Now.ToString(), ID)));
        }
Exemplo n.º 9
0
 void QcMsgClient_ConnectedServer(object sender, EventArgs e)
 {
     this.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, User.Name, "*"));
 }
Exemplo n.º 10
0
 public void BroadcastShutdown()
 {
     BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, User.Name));
 }
Exemplo n.º 11
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;
                }
            }
        }
Exemplo n.º 12
0
        void QcMessagner_ReciveCmd(object sender, QcCmdEventArgs e)
        {
            try
            {
                if (e.Cmd.CmdType == QcProtocol.QcCommand.Undefine)
                {
                    return;
                }
                QcMsgUser userfrom = null;
                if (lstUser.ContainsKey(e.Cmd.From))
                {
                    userfrom = lstUser[e.Cmd.From];
                }
                if (this.ReciveCmd != null)
                {
                    var cmdevt = new QcMessagerCmdEventArg()
                    {
                        cmd  = e.Cmd,
                        user = userfrom
                    };
                    this.ReciveCmd(this, cmdevt);
                }

                switch (e.Cmd.CmdType)
                {
                case QcProtocol.QcCommand.QcLoginReplay:
                    client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcListUser));
                    break;

                case QcProtocol.QcCommand.QcLoginOut:
                    if (userfrom != null)
                    {
                        userfrom.Chanel   = null;
                        userfrom.IsLinked = false;
                    }
                    if (this.Loginout != null)
                    {
                        if (userfrom != null)
                        {
                            this.Loginout(this, new QcMessagerLoginEventArg(userfrom));
                        }
                    }

                    break;

                case QcProtocol.QcCommand.QcMsg:
                    if (this.ReciveMsg != null)
                    {
                        var msgevt = new QcMessagerMsgEventArg(userfrom.Name, e.Cmd.tokens(3), e.Cmd.tokens(4));
                        this.ReciveMsg(this, msgevt);
                    }
                    break;

                case QcProtocol.QcCommand.QcSystemMsg:
                    if (this.ReciveSystemMsg != null)
                    {
                        var systemevt = new QcSystemMsgEventArg(e.Cmd.tokens(5), e.Cmd.From, e.Cmd.tokens(3), e.Cmd.tokens(4));
                        this.ReciveSystemMsg(this, systemevt);
                    }
                    break;

                case QcProtocol.QcCommand.QcUserLogin:
                    foreach (QcMsgUser u in lstUser.Values)
                    {
                        if (u.Name == userfrom.Name)
                        {
                            u.IsLinked = true;
                            break;
                        }
                    }


                    if (this.Logined != null)
                    {
                        this.Logined(this, new QcMessagerLoginEventArg(userfrom));
                    }
                    break;

                case QcProtocol.QcCommand.QcListUser:
                    var v = e.Cmd.tokens(1).Split(',');
                    foreach (var u in v)
                    {
                        if (lstUser.ContainsKey(u))
                        {
                            lstUser[u].Chanel = new QcChanel();
                        }
                    }
                    break;

                case QcProtocol.QcCommand.QcUpdateMsg:
                    if (ReciveUpdateMsg != null)
                    {
                        var msg = QcMsg.GetMsg(e.Cmd.tokens(4));
                        ReciveUpdateMsg(this, new QcMessagerUpdateMsgEventArg(msg));
                    }
                    break;

                case QcProtocol.QcCommand.QcDataUpdate:
                    string index = e.Cmd.tokens(2) + "|" + e.Cmd.tokens(1);

                    //收到数据更新消息,进行更新
                    //   QcLog.LogString(e.Cmd.ToString());
                    NodeChangeType type = NodeChangeType.Create;
                    if (Enum.TryParse <NodeChangeType>(e.Cmd.tokens(3), out type))
                    {
                        switch (type)
                        {
                        case NodeChangeType.Create:

                            break;

                        case NodeChangeType.Update:
                            if (QcNode.lstNode.ContainsKey(index))
                            {
                                QcNode.lstNode[index].Refresh();
                            }
                            break;

                        case NodeChangeType.Delete:
                            if (QcNode.lstNode.ContainsKey(index))
                            {
                                QcNode.lstNode[index].RiseDelete();
                            }
                            break;
                        }
                        if (DataUpdate != null)
                        {
                            if (QcNode.lstNode.ContainsKey(index))
                            {
                                DataUpdate(this, new QcMessagerDataUpdateEventArg(QcNode.lstNode[index], type));
                            }
                            else
                            {
                                string tablename = e.Cmd.tokens(2);
                                string code      = e.Cmd.tokens(1);
                                string codefield = e.Cmd.tokens(4);
                                QcNode node      = new QcNewNode(tablename, code, codefield);
                                node.InitFromString(e.Cmd.tokens(5));
                                DataUpdate(this, new QcMessagerDataUpdateEventArg(node, type));
                            }
                        }
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                QcLog.LogException("QcMessagner:", ex);
            }
        }
Exemplo n.º 13
0
 public static bool PostSystemMsg(string ID, string DestUser, string Msg)
 {
     QcProtocol.QcCommand cmd = QcProtocol.QcCommand.QcSystemMsg;
     return(PostMessage(QcCmd.MakeCmd(cmd, "*", DestUser, Msg, DateTime.Now.ToString(), ID)));
 }
Exemplo n.º 14
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();
            }
        }