예제 #1
0
        public void Close()
        {
            if (User != null)
            {
                BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, User.Name));

                foreach (var v in lstUser)
                {
                    v.Value.Close();
                }
            }
            if (server != null)
            {
                server.ShutDown();
            }
        }
예제 #2
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;
            }
        }
예제 #3
0
 public QcCmdEventArgs(QcCmd cmd, IQcSocket socket)
 {
     this.cmd    = cmd;
     this.socket = socket;
 }
예제 #4
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);
        }
예제 #5
0
 public void BroadcastLoginOut()
 {
     BroadcastMsg(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginOut, User.Name));
 }
예제 #6
0
        void serverchanel_ReceivedCmd(object sender, QcCmdEventArgs e)
        {
            // 这是登录服务器,首次登陆,没有创建连接对象
            if (lstUser.ContainsKey(e.Cmd.tokens(1)) == false)
            {
                return;
            }
            var userfrom = lstUser[e.Cmd.tokens(1)];

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

            case QcProtocol.QcCommand.QcLoginReplay:
                break;

            case QcProtocol.QcCommand.QcLoginOut:

                if (this.Loginout != null)
                {
                    var evt = new QcMessagerLoginEventArg(userfrom);
                    this.Loginout(this, evt);
                }
                userfrom.Close();
                break;

            case QcProtocol.QcCommand.QcUserLogin:
                if (sender == serverchanel)
                {
                    return;
                }
                if (userfrom.Name == User.Name)
                {
                    return;
                }
                bool blLogined = false;
                if (e.Cmd.tokens(2) == User.Name)
                {
                    blLogined = true;
                    if (this.Logined != null)
                    {
                        var evtarg = new QcMessagerLoginEventArg(userfrom);
                        this.Logined(this, evtarg);
                    }
                }
                e.Chanel.Send(QcCmd.MakeCmd(QcProtocol.QcCommand.QcLoginReplay, blLogined));
                break;
            }
            if (this.ReciveCmd != null)
            {
                var cmdevt = new QcMessagerCmdEventArg()
                {
                    cmd = e.Cmd, user = userfrom
                };
                this.ReciveCmd(this, cmdevt);
            }
        }