Exemplo n.º 1
0
        public static Dictionary<string, DiscoInfo> GetCapsCache()
        {
            Dictionary<string, DiscoInfo>  capsCache = new Dictionary<string, DiscoInfo>(24);

            try
            {
                using (SQLiteCommand command = _connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM [CapsCache]";

                    SQLiteDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {

                        Document document = new Document();
                        document.LoadXml((string)reader["Features"]);

                        DiscoInfo info = document.RootElement as DiscoInfo;

                        if (info != null)
                        {
                            capsCache.Add((string) reader["Caps"], info);
                        }
                    }

                    reader.Close();
                }
            }

            catch (Exception e)
            {
                Events.Instance.OnEvent(e, new EventError(e.Message, null));
            }

            return capsCache;
        }
Exemplo n.º 2
0
        private void OnGetSessionRequestResponse(IAsyncResult result)
        {
            // grab the custom state object
            WebRequestState state = (WebRequestState)result.AsyncState;
            HttpWebRequest request = (HttpWebRequest)state.WebRequest;

            //state.TimeOutTimer.Dispose();

            // get the Response
            HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(result);

            // The server must always return a 200 response code,
            // sending any session errors as specially-formatted identifiers.
            if (resp.StatusCode != HttpStatusCode.OK)
            {
                //FireOnError(new PollSocketException("unexpected status code " + resp.StatusCode.ToString()));
                return;
            }

            Stream rs = resp.GetResponseStream();

            int readlen;
            byte[] readbuf = new byte[1024];
            MemoryStream ms = new MemoryStream();
            while ((readlen = rs.Read(readbuf, 0, readbuf.Length)) > 0)
            {
                ms.Write(readbuf, 0, readlen);
            }

            byte[] recv = ms.ToArray();

            if (recv.Length > 0)
            {
                string body = null;
                string stanzas = null;

                string res = Encoding.UTF8.GetString(recv, 0, recv.Length);

                ParseResponse(res, ref body, ref stanzas);

                Document doc = new Document();
                doc.LoadXml(body);
                Body boshBody = doc.RootElement as Body;

                sid         = boshBody.Sid;
                polling     = boshBody.Polling;
                m_MaxPause  = boshBody.MaxPause;

                byte[] bin = Encoding.UTF8.GetBytes(DummyStreamHeader + stanzas);

                base.FireOnReceive(bin, bin.Length);

                // cleanup webrequest resources
                ms.Close();
                rs.Close();
                resp.Close();

                waitingRequests--;

                if (waitingRequests == 0)
                    StartWebRequest();
            }
        }
Exemplo n.º 3
0
 private Message ToMessage(string messageXml)
 {
     Document doc = new Document();
     doc.LoadXml(messageXml);
     return (Message)doc.RootElement;
 }
        private void OnGetResponse(IAsyncResult ar)
        {
            try
            {
                requestIsTerminating = true;
                // grab the custom state object
                WebRequestState state = (WebRequestState)ar.AsyncState;
                HttpWebRequest request = (HttpWebRequest)state.WebRequest;
                HttpWebResponse resp = null;

                if (request.HaveResponse)
                {
                    // TODO, its crashing mostly here
                    // get the Response
                    try
                    {
                        resp = (HttpWebResponse) request.EndGetResponse(ar);
                    }
                    catch (WebException ex)
                    {
                        activeRequests--;
                        requestIsTerminating = false;
                        if (ex.Response == null)
                        {
                            StartWebRequest();
                        }
                        else
                        {
                            HttpWebResponse res = ex.Response as HttpWebResponse;
                            if (res.StatusCode == HttpStatusCode.NotFound)
                            {
                                TerminateBoshSession();
                            }
                        }
                        return;
                    }

                    // The server must always return a 200 response code,
                    // sending any session errors as specially-formatted identifiers.
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        activeRequests--;
                        requestIsTerminating = false;
                        if (resp.StatusCode == HttpStatusCode.NotFound)
                        {
                            //Console.WriteLine("Not Found");
                            TerminateBoshSession();
                        }
                        return;
                    }
                }
                else
                {
                    //Console.WriteLine("No response");
                }

                Stream rs = resp.GetResponseStream();

                int readlen;
                byte[] readbuf = new byte[1024];
                MemoryStream ms = new MemoryStream();
                while ((readlen = rs.Read(readbuf, 0, readbuf.Length)) > 0)
                {
                    ms.Write(readbuf, 0, readlen);
                }

                byte[] recv = ms.ToArray();

                if (recv.Length > 0)
                {
                    string sbody = null;
                    string stanzas = null;

                    ParseResponse(Encoding.UTF8.GetString(recv, 0, recv.Length), ref sbody, ref stanzas);

                    if (stanzas != null)
                    {
                        byte[] bStanzas = Encoding.UTF8.GetBytes(stanzas);
                        base.FireOnReceive(bStanzas, bStanzas.Length);
                    }
                    else
                    {
                        if (sbody != null)
                        {
                            var doc = new Document();
                            doc.LoadXml(sbody);
                            if (doc.RootElement != null)
                            {
                                var body = doc.RootElement as Body;
                                if (body.Type == BoshType.terminate)
                                    TerminateBoshSession();
                            }
                        }

                        if (terminate && !terminated)
                        {
                            // empty teminate response
                            TerminateBoshSession();
                        }
                    }
                }

                // cleanup webrequest resources
                ms.Close();
                rs.Close();
                resp.Close();

                activeRequests--;
                requestIsTerminating = false;

                //if (activeRequests == 0 && !terminated)
                if ( (activeRequests == 0 && !terminated)
                    || (activeRequests == 1 && m_SendQueue.Count > 0) )
                {
                    StartWebRequest();
                }
            }
            catch (Exception ex)
            {

            }
        }