Beispiel #1
0
 public XmppClient()
 {
     id_                  = 0;
     is_logined_          = false;
     is_loginning_        = false;
     params_              = new LoginParams();
     login_task_          = null;
     parser_              = new _XmlParser();
     conn_                = new _Connection();
     conn_.OnSocketRead  += OnSocketRead;
     conn_.OnSocketState += OnSocketState;
 }
Beispiel #2
0
 public bool Login()
 {
     if (is_logined_ || is_loginning_)
     {
         return(true);
     }
     is_loginning_       = true;
     jid_                = params_.user + "@" + params_.domain + "/" + params_.resource;
     login_task_         = new _LoginTask();
     login_task_.client_ = this;
     login_task_.parser_ = parser_;
     login_task_.conn_   = conn_;
     return(conn_.Connect(params_.host, params_.port));
 }
Beispiel #3
0
        private void OnSocketRead(string s)
        {
            Debug.WriteLine("\n[ proc_xml ]\n" + s);

            if (OnXmppInput != null)
            {
                OnXmppInput(this, s);
            }

            try
            {
                if (login_task_ != null)
                {
                    if (!login_task_.ProcData(s))
                    {
                        SignalLoginFailed(login_task_.GetErrorCode(), login_task_.GetErrorInfo());
                        login_task_ = null;
                    }
                    else if (login_task_.IsDone())
                    {
                        is_logined_   = true;
                        is_loginning_ = true;
                        login_task_   = null;
                        if (OnLoginOk != null)
                        {
                            OnLoginOk(this);
                        }
                    }
                    return;
                }

                XmlReader _reader = parser_.Create(s);
                while (_reader.Read())
                {
                    if (_reader.Depth == 0 &&
                        (_reader.IsEmptyElement || _reader.NodeType == XmlNodeType.Element))
                    {
                        string sname = _reader.Name;
                        string elem  = _reader.ReadOuterXml();
                        Debug.WriteLine(elem);

                        try
                        {
                            if (sname == "iq" && OnRecvIQ != null)
                            {
                                OnRecvIQ(this, elem);
                            }
                            if (sname == "message" && OnRecvMessage != null)
                            {
                                OnRecvMessage(this, elem);
                            }
                            if (sname == "presence" && OnRecvPresence != null)
                            {
                                OnRecvPresence(this, elem);
                            }
                        }
                        catch (System.Exception ex)
                        {
                        }

                        _reader.Skip();
                    }
                }
            }
            catch (Exception e)
            {
                if (s == "</stream:stream>")
                {
                    SignalConnectionClose((int)CloseReason.SERVER_CLOSE, "server close stream");
                }
                else
                {
                    SignalConnectionClose((int)CloseReason.XML_INVALID, "XML invalid");
                }
            }
        }