示例#1
0
        void server_ReceiveCmd(object sender, QcCmdEventArgs e)
        {
            QcCmd  cmd  = e.Cmd;
            string from = e.Cmd.tokens(1);

            switch (cmd.CmdType)
            {
            case QcProtocol.QcCommand.QcUserLogin:

                bool blLogined = false;
                if (cmd.tokens(2) == User.Name)
                {
                    blLogined = true;
                    QcChanel chanel = new QcChanel();

                    chanel.SetChanel(e.Chanel, server);

                    if (lstUser.ContainsKey(from))
                    {
                        lstUser[from].Chanel = chanel;
                        if (this.Logined != null)
                        {
                            var userfrom = lstUser[from];
                            var evtarg   = new QcMessagerLoginEventArg(userfrom);
                            this.Logined(this, evtarg);
                        }
                    }
                }

                QcClientService qcs = e.Chanel as QcClientService;
                e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginReplay, blLogined));
                if (blLogined && from != User.Name)
                {
                    e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, User.Name, from));
                }

                break;
            }
        }
示例#2
0
        QcClient TryConnectIp(string ip, ushort port, string name)
        {
            if (ip == "")
            {
                return(null);
            }
            Ping myPing;

            myPing = new Ping();
            AutoResetEvent are         = new AutoResetEvent(false);
            bool           pingsuccess = false;

            myPing.PingCompleted += (o, e) => { are.Set(); pingsuccess = (e.Reply.Status == IPStatus.Success); };
            string pingIP = ip.ToString();

            try
            {
                myPing.SendAsync(pingIP, 100, null);
            }
            catch
            {
                return(null);
            }
            are.WaitOne(100);//等待ping完成
            if (pingsuccess == true)
            //  if (myPing.Send(pingIP, 10000).Status == IPStatus.Success)
            {
                QcClient       client      = new QcClient();
                AutoResetEvent msgresetevt = new AutoResetEvent(false);
                client.ConnectedServer += (o, e) => { msgresetevt.Set(); };
                client.Connect(ip, port);
                if (msgresetevt.WaitOne(1000))
                {
                    bool logined = false;
                    client.ReceiveCmd += (o, e) =>
                    {
                        QcCmd cmd = e.Cmd;
                        switch (cmd.CmdType)
                        {
                        case QcProtocol.QcCommand.QcLoginReplay:
                            if (cmd.tokens(1) == "True")
                            {
                                logined = true;
                            }
                            ;
                            break;

                        default:
                            break;
                        }
                        msgresetevt.Set();
                    };
                    client.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcUserLogin, User.Name, name));
                    if (msgresetevt.WaitOne(100))
                    {
                        if (logined)
                        {
                            return(client);
                        }
                    }
                    client.Close();
                }
            }

            //失败返回null
            return(null);
        }