Beispiel #1
0
        private void OnGetRequestStream(IAsyncResult ar)
        {
            try
            {
                WebRequestState state = ar.AsyncState as WebRequestState;

                if (state.Aborted)
                {
                    activeRequests--;
                    StartWebRequest(true, state.Output);
                }
                else
                {
                    state.TimeOutTimer.Dispose();
                    HttpWebRequest req = state.WebRequest as HttpWebRequest;

                    Stream requestStream = req.EndGetRequestStream(ar);
                    state.RequestStream = requestStream;
                    byte[] bytes = Encoding.UTF8.GetBytes(state.Output);
                    requestStream.BeginWrite(bytes, 0, bytes.Length, OnEndWrite, state);
                }
            }
            catch (Exception ex)
            {
                activeRequests--;

                WebRequestState state = ar.AsyncState as WebRequestState;
                StartWebRequest(true, state.Output);
            }
        }
Beispiel #2
0
        private void OnEndWrite(IAsyncResult ar)
        {
            WebRequestState state = ar.AsyncState as WebRequestState;

            HttpWebRequest req           = state.WebRequest as HttpWebRequest;
            Stream         requestStream = state.RequestStream;

            requestStream.EndWrite(ar);
            requestStream.Close();

            IAsyncResult result;

            try
            {
                if (state.IsSessionRequest)
                {
                    req.BeginGetResponse(OnGetSessionRequestResponse, state);
                }
                else
                {
                    req.BeginGetResponse(OnGetResponse, state);
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
            }
        }
Beispiel #3
0
        public void TimeOutGetRequestStream(Object stateObj)
        {
            //Console.WriteLine("Web Request timed out");

            WebRequestState state = stateObj as WebRequestState;

            state.TimeOutTimer.Dispose();
            state.Aborted = true;
            state.WebRequest.Abort();
        }
Beispiel #4
0
        private void StartWebRequest(bool retry, string content)
        {
            lock (this)
            {
                webRequestId++;
            }

            activeRequests++;

            lastSend = DateTime.Now;

            HttpWebRequest req = (HttpWebRequest)CreateWebrequest(Address);;

            WebRequestState state = new WebRequestState(req);

            state.Started      = DateTime.Now;
            state.WebRequestId = webRequestId;

            if (!retry)
            {
                state.Output = BuildPostData();
            }
            else
            {
                state.Output = content;
            }

            req.Method        = METHOD;
            req.ContentType   = CONTENT_TYPE;
            req.Timeout       = m_Wait * 1000;
            req.KeepAlive     = m_KeepAlive;
            req.ContentLength = Encoding.UTF8.GetBytes(state.Output).Length;

            // Create the delegate that invokes methods for the timer.
            TimerCallback timerDelegate = TimeOutGetRequestStream;
            Timer         timeoutTimer  = new Timer(timerDelegate, state, WEBREQUEST_TIMEOUT, WEBREQUEST_TIMEOUT);

            state.TimeOutTimer = timeoutTimer;

            try
            {
                req.BeginGetRequestStream(OnGetRequestStream, state);
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex.Message);
            }
        }
Beispiel #5
0
        private void OnGetSessionRequestStream(IAsyncResult ar)
        {
            try
            {
                WebRequestState state = ar.AsyncState as WebRequestState;
                HttpWebRequest  req   = state.WebRequest as HttpWebRequest;

                Stream outputStream = req.EndGetRequestStream(ar);

                byte[] bytes = Encoding.UTF8.GetBytes(state.Output);

                state.RequestStream = outputStream;
                IAsyncResult result = outputStream.BeginWrite(bytes, 0, bytes.Length, OnEndWrite, state);
            }
            catch (WebException ex)
            {
                FireOnError(ex);
                Disconnect();
            }
        }
Beispiel #6
0
        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)
            {
            }
        }
Beispiel #7
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)
            {
                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();

                activeRequests--;

                if (activeRequests == 0)
                {
                    StartWebRequest();
                }
            }
        }
Beispiel #8
0
        public void RequestBoshSession()
        {
            /*
             * Example 1. Requesting a BOSH session
             *
             * POST /webclient HTTP/1.1
             * Host: httpcm.jabber.org
             * Accept-Encoding: gzip, deflate
             * Content-Type: text/xml; charset=utf-8
             * Content-Length: 104
             *
             * <body content='text/xml; charset=utf-8'
             *    hold='1'
             *    rid='1573741820'
             *    to='jabber.org'
             *    route='xmpp:jabber.org:9999'
             *    secure='true'
             *    ver='1.6'
             *    wait='60'
             *    ack='1'
             *    xml:lang='en'
             *    xmlns='http://jabber.org/protocol/httpbind'/>
             */

            lastSend = DateTime.Now;

            // Generate the keys
            GenerateKeys();
            rid = GenerateRid();
            Body body = new Body();

            /*
             * <body hold='1' xmlns='http://jabber.org/protocol/httpbind'
             *  to='vm-2k'
             *  wait='300'
             *  rid='782052'
             *  newkey='8e7d6cec12004e2bfcf7fc000310fda87bc8337c'
             *  ver='1.6'
             *  xmpp:xmlns='urn:xmpp:xbosh'
             *  xmpp:version='1.0'/>
             */
            //window='5' content='text/xml; charset=utf-8'
            //body.SetAttribute("window", "5");
            //body.SetAttribute("content", "text/xml; charset=utf-8");

            body.Version     = BOSH_VERSION;
            body.XmppVersion = "1.0";
            body.Hold        = m_Hold;
            body.Wait        = m_Wait;
            body.Rid         = rid;
            body.Polling     = 0;
            body.Requests    = m_Requests;
            body.To          = new Jid(m_XmppCon.Server);

            body.NewKey = Keys[CurrentKeyIdx];

            body.SetAttribute("xmpp:xmlns", "urn:xmpp:xbosh");

            activeRequests++;

            HttpWebRequest req = (HttpWebRequest)CreateWebrequest(Address);

            WebRequestState state = new WebRequestState(req);

            state.Started          = DateTime.Now;
            state.Output           = body.ToString();
            state.IsSessionRequest = true;

            req.Method        = METHOD;
            req.ContentType   = CONTENT_TYPE;
            req.Timeout       = m_Wait * 1000;
            req.KeepAlive     = m_KeepAlive;
            req.ContentLength = Encoding.UTF8.GetBytes(state.Output).Length;   // state.Output.Length;

            try
            {
                IAsyncResult result = req.BeginGetRequestStream(new AsyncCallback(this.OnGetSessionRequestStream), state);
            }
            catch (Exception)
            {
            }
        }