示例#1
0
        private void Client_UploadDataCompleted(object sender, CapsBase.UploadDataCompletedEventArgs e)
        {
            if (OnComplete != null && !e.Cancelled)
            {
                if (e.Error == null)
                {
                    LLSD result = LLSDParser.DeserializeXml(e.Result);

                    try { OnComplete(this, result, e.Error); }
                    catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
                }
                else
                {
                    // Some error occurred, try to figure out what happened
                    HttpStatusCode code = HttpStatusCode.OK;
                    if (e.Error is WebException && ((WebException)e.Error).Response != null)
                    {
                        code = ((HttpWebResponse)((WebException)e.Error).Response).StatusCode;
                    }

                    if (code == HttpStatusCode.BadGateway)
                    {
                        // This is not good (server) protocol design, but it's normal.
                        // The CAPS server is a proxy that connects to a Squid
                        // cache which will time out periodically. The CAPS server
                        // interprets this as a generic error and returns a 502 to us
                        // that we ignore
                        StartRequest(_PostData, _ContentType);
                    }
                    else if (code != HttpStatusCode.OK)
                    {
                        // Status code was set to something unknown, this is a failure
                        Logger.DebugLog(String.Format("Caps error at {0}: {1}", _Client.Location, code));

                        try { OnComplete(this, null, e.Error); }
                        catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
                    }
                    else
                    {
                        // Status code was not set, some other error occurred. This is a failure
                        Logger.DebugLog(String.Format("Caps error at {0}: {1}", _Client.Location, e.Error.Message));

                        try { OnComplete(this, null, e.Error); }
                        catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
                    }
                }
            }
            else if (e.Cancelled)
            {
                Logger.DebugLog("Capability action at " + _Client.Location + " cancelled");
            }
        }
        private void Client_UploadDataCompleted(object sender, CapsBase.UploadDataCompletedEventArgs e)
        {
            LLSDArray events = null;
            int       ack    = 0;

            if (e.Error != null)
            {
                HttpStatusCode code = HttpStatusCode.OK;
                if (e.Error is WebException && ((WebException)e.Error).Response != null)
                {
                    code = ((HttpWebResponse)((WebException)e.Error).Response).StatusCode;
                }

                if (code == HttpStatusCode.NotFound || code == HttpStatusCode.Gone)
                {
                    Logger.Log(String.Format("Closing event queue at {0} due to missing caps URI", _Client.Location),
                               Helpers.LogLevel.Info);

                    _Running = false;
                    _Dead    = true;
                }
                else if (code == HttpStatusCode.BadGateway)
                {
                    // This is not good (server) protocol design, but it's normal.
                    // The EventQueue server is a proxy that connects to a Squid
                    // cache which will time out periodically. The EventQueue server
                    // interprets this as a generic error and returns a 502 to us
                    // that we ignore
                }
                else if (!e.Cancelled)
                {
                    // Try to log a meaningful error message
                    if (code != HttpStatusCode.OK)
                    {
                        Logger.Log(String.Format("Unrecognized caps connection problem from {0}: {1}",
                                                 _Client.Location, code), Helpers.LogLevel.Warning);
                    }
                    else if (e.Error.InnerException != null)
                    {
                        Logger.Log(String.Format("Unrecognized caps exception from {0}: {1}",
                                                 _Client.Location, e.Error.InnerException.Message), Helpers.LogLevel.Warning);
                    }
                    else
                    {
                        Logger.Log(String.Format("Unrecognized caps exception from {0}: {1}",
                                                 _Client.Location, e.Error.Message), Helpers.LogLevel.Warning);
                    }
                }
            }
            else if (!e.Cancelled && e.Result != null)
            {
                // Got a response
                LLSD result = LLSDParser.DeserializeXml(e.Result);
                if (result != null && result.Type == LLSDType.Map)
                {
                    // Parse any events returned by the event queue
                    LLSDMap map = (LLSDMap)result;

                    events = (LLSDArray)map["events"];
                    ack    = map["id"].AsInteger();
                }
            }
            else if (e.Cancelled)
            {
                // Connection was cancelled
                Logger.DebugLog("Cancelled connection to event queue at " + _Client.Location);
            }

            if (_Running)
            {
                LLSDMap request = new LLSDMap();
                if (ack != 0)
                {
                    request["ack"] = LLSD.FromInteger(ack);
                }
                else
                {
                    request["ack"] = new LLSD();
                }
                request["done"] = LLSD.FromBoolean(_Dead);

                byte[] postData = LLSDParser.SerializeXmlBytes(request);

                _Client.UploadDataAsync(_Client.Location, postData);

                // If the event queue is dead at this point, turn it off since
                // that was the last thing we want to do
                if (_Dead)
                {
                    _Running = false;
                    Logger.DebugLog("Sent event queue shutdown message");
                }
            }

            if (OnEvent != null && events != null && events.Count > 0)
            {
                // Fire callbacks for each event received
                foreach (LLSDMap evt in events)
                {
                    string  msg  = evt["message"].AsString();
                    LLSDMap body = (LLSDMap)evt["body"];

                    try { OnEvent(msg, body); }
                    catch (Exception ex) { Logger.Log(ex.Message, Helpers.LogLevel.Error, ex); }
                }
            }
        }