Exemplo n.º 1
0
        protected override void RequestReply(HttpRequestState state, bool success, WebException exception)
        {
            object response = null;

            if (success)
            {
                response = LLSDParser.DeserializeXml(state.ResponseData);
            }
            else if (exception != null && exception.Message.Contains("502"))
            {
                // These are normal, retry the request automatically
                MakeRequest(state.RequestData, "application/xml", 0, null);

                return;
            }

            // Only fire the callback if there is response data or the call has
            // not been aborted. Timeouts and 502 errors don't count as aborting,
            // although 502 errors are already handled above
            if (response != null || !_Aborted)
            {
                if (OnCapsResponse != null)
                {
                    try { OnCapsResponse(response, state); }
                    catch (Exception e) { SecondLife.LogStatic(e.ToString(), Helpers.LogLevel.Error); }
                }
            }
        }
Exemplo n.º 2
0
        public new void MakeRequest()
        {
            // Create an EventQueueGet request
            Dictionary<string, object> request = new Dictionary<string, object>();
            request["ack"] = null;
            request["done"] = false;

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

            // Create a new HttpWebRequest
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_RequestURL);
            _RequestState = new HttpRequestState(httpRequest);

            if (_ProxyURL != String.Empty)
            {
                // Create a proxy object
                WebProxy proxy = new WebProxy();

                // Associate a new Uri object to the _wProxy object, using the proxy address
                // selected by the user
                proxy.Address = new Uri(_ProxyURL);

                // Finally, initialize the Web request object proxy property with the _wProxy
                // object
                httpRequest.Proxy = proxy;
            }

            // Always disable keep-alive for our purposes
            httpRequest.KeepAlive = false;

            // POST request
            _RequestState.WebRequest.Method = "POST";
            _RequestState.WebRequest.ContentLength = postData.Length;
            _RequestState.WebRequest.Headers.Add("X-SecondLife-UDP-Listen-Port", Simulator.udpPort.ToString());
            _RequestState.WebRequest.ContentType = "application/xml";
            _RequestState.RequestData = postData;

            IAsyncResult result = (IAsyncResult)_RequestState.WebRequest.BeginGetRequestStream(
                new AsyncCallback(EventRequestStreamCallback), _RequestState);
        }
Exemplo n.º 3
0
 protected abstract void RequestReply(HttpRequestState state, bool success, WebException exception);
Exemplo n.º 4
0
        protected void ReadCallback(IAsyncResult result)
        {
            try
            {
                _RequestState = (HttpRequestState)result.AsyncState;
                Stream responseStream = _RequestState.ResponseStream;
                int read = responseStream.EndRead(result);

                // Check if we have read the entire response
                if (read > 0)
                {
                    // Create the byte array if it hasn't been created yet
                    if (_RequestState.ResponseData == null || _RequestState.ResponseData.Length != _RequestState.WebResponse.ContentLength)
                        _RequestState.ResponseData = new byte[_RequestState.WebResponse.ContentLength];

                    // Copy the current buffer data in to the response variable
                    Buffer.BlockCopy(_RequestState.BufferRead, 0, _RequestState.ResponseData, _RequestState.ResponseDataPos, read);
                    // Increment our writing position in the response variable
                    _RequestState.ResponseDataPos += read;

                    // Continue reading the response until EndRead() returns 0
                    IAsyncResult asynchronousResult = responseStream.BeginRead(_RequestState.BufferRead, 0, BUFFER_SIZE, 
                        new AsyncCallback(ReadCallback), _RequestState);

                    return;
                }
                else
                {
                    // Fire the callback for receiving a response
                    try { RequestReply(_RequestState, true, null); }
                    catch (Exception e) { Log(e.ToString(), Helpers.LogLevel.Error); }

                    responseStream.Close();
                }
            }
            catch (WebException e)
            {
                Abort(false, e);
            }
        }
Exemplo n.º 5
0
        private void ResponseCallback(IAsyncResult result)
        {
            try
            {
                _RequestState = (HttpRequestState)result.AsyncState;
                _RequestState.WebResponse = (HttpWebResponse)_RequestState.WebRequest.EndGetResponse(result);

                // Read the response into a Stream object
                Stream responseStream = _RequestState.WebResponse.GetResponseStream();
                _RequestState.ResponseStream = responseStream;

                // Begin reading of the contents of the response
                IAsyncResult asynchronousInputRead = responseStream.BeginRead(_RequestState.BufferRead, 0, BUFFER_SIZE, 
                    new AsyncCallback(ReadCallback), _RequestState);

                // If there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(asynchronousInputRead.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback),
                    _RequestState, HTTP_TIMEOUT, true);
            }
            catch (WebException e)
            {
                Abort(false, e);
            }
        }
Exemplo n.º 6
0
        protected void RequestStreamCallback(IAsyncResult result)
        {
            try
            {
                _RequestState = (HttpRequestState)result.AsyncState;
                Stream reqStream = _RequestState.WebRequest.EndGetRequestStream(result);

                reqStream.Write(_RequestState.RequestData, 0, _RequestState.RequestData.Length);
                reqStream.Close();

                IAsyncResult newResult = _RequestState.WebRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), _RequestState);

                // If there is a timeout, the callback fires and the request becomes aborted
                ThreadPool.RegisterWaitForSingleObject(newResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback),
                    _RequestState, HTTP_TIMEOUT, true);
            }
            catch (WebException e)
            {
                Abort(false, e);
            }
        }
Exemplo n.º 7
0
        private void CreateItemFromAssetResponse(object response, HttpRequestState state)
        {
            Dictionary<string, object> contents = (Dictionary<string, object>)response;
            KeyValuePair<ItemCreatedFromAssetCallback, byte[]> kvp = (KeyValuePair<ItemCreatedFromAssetCallback, byte[]>)state.State;
            ItemCreatedFromAssetCallback callback = kvp.Key;
            byte[] itemData = (byte[])kvp.Value;

            string status = (string)contents["state"];

            if (status == "upload")
            {
                string uploadURL = (string)contents["uploader"];

                // This makes the assumption that all uploads go to CurrentSim, to avoid
                // the problem of HttpRequestState not knowing anything about simulators
                CapsRequest upload = new CapsRequest(uploadURL, _Client.Network.CurrentSim);
                upload.OnCapsResponse += new CapsRequest.CapsResponseCallback(CreateItemFromAssetResponse);
                upload.MakeRequest(itemData, "application/octet-stream", 0, kvp);
            }
            else if (status == "complete")
            {
                if (contents.ContainsKey("new_inventory_item") && contents.ContainsKey("new_asset"))
                {
                    try { callback(true, String.Empty, (LLUUID)contents["new_inventory_item"], (LLUUID)contents["new_asset"]); }
                    catch (Exception e) { _Client.Log(e.ToString(), Helpers.LogLevel.Error); }
                }
                else
                {
                    try { callback(false, "Failed to parse asset and item UUIDs", LLUUID.Zero, LLUUID.Zero); }
                    catch (Exception e) { _Client.Log(e.ToString(), Helpers.LogLevel.Error); }
                }
            }
            else
            {
                // Failure
                try { callback(false, status, LLUUID.Zero, LLUUID.Zero); }
                catch (Exception e) { _Client.Log(e.ToString(), Helpers.LogLevel.Error); }
            }
        }
Exemplo n.º 8
0
        private void GatherCapsResponse(object response, HttpRequestState state)
        {
            if (response is Dictionary<string, object>)
            {
                Dictionary<string, object> respTable = (Dictionary<string, object>)response;

                // parse
                _caps = new RegistrationCaps();

                _caps.CreateUser = new Uri((string)respTable["create_user"]);
                _caps.CheckName = new Uri((string)respTable["check_name"]);
                _caps.GetLastNames = new Uri((string)respTable["get_last_names"]);
                _caps.GetErrorCodes = new Uri((string)respTable["get_error_codes"]);

                // finalize
                _initializing++;

                GatherErrorMessages();
            }
        }
Exemplo n.º 9
0
        private void MapLayerResponseHandler(object response, HttpRequestState state)
        {
            Hashtable body = (Hashtable)response;
            ArrayList layerData = (ArrayList)body["LayerData"];

            if (OnGridLayer != null)
            {
                for (int i = 0; i < layerData.Count; i++)
                {
                    Hashtable thisLayerData = (Hashtable)layerData[i];

                    GridLayer layer;
                    layer.Bottom = (int)thisLayerData["Bottom"];
                    layer.Left = (int)thisLayerData["Left"];
                    layer.Top = (int)thisLayerData["Top"];
                    layer.Right = (int)thisLayerData["Right"];
                    layer.ImageID = (LLUUID)thisLayerData["ImageID"];

                    try { OnGridLayer(layer); }
                    catch (Exception e) { Client.Log(e.ToString(), Helpers.LogLevel.Error); }
                }
            }

            if (body.ContainsKey("MapBlocks"))
            {
                // TODO: At one point this will become activated
                Client.Log("Got MapBlocks through CAPS, please finish this function!", Helpers.LogLevel.Error);
            }
        }
Exemplo n.º 10
0
        protected override void RequestReply(HttpRequestState state, bool success, WebException exception)
        {
            List<object> events = null;
            int ack = 0;

            #region Exception Handling

            if (exception != null)
            {
                // Check what kind of exception happened
                if (exception.Message.Contains("404") || exception.Message.Contains("410"))
                {
                    Simulator.Client.Log("Closing event queue for " + Simulator.ToString() + " due to missing caps URI",
                        Helpers.LogLevel.Info);

                    _Running = false;
                    _Dead = true;
                }
                else if (!exception.Message.ToLower().Contains("aborted") && !exception.Message.Contains("502"))
                {
                    Simulator.Client.Log(String.Format("Unrecognized caps exception for {0}: {1}", Simulator, exception.Message),
                        Helpers.LogLevel.Warning);
                }
            }

            #endregion Exception Handling

            #region Reply Decoding

            // Decode successful replies from the event queue
            if (success)
            {
                Dictionary<string, object> response = (Dictionary<string, object>)LLSDParser.DeserializeXml(state.ResponseData);

                if (response != null)
                {
                    // Parse any events returned by the event queue
                    events = (List<object>)response["events"];
                    ack = (int)response["id"];
                }
            }

            #endregion Reply Decoding

            #region Make New Request

            if (_Running)
            {
                Dictionary<string, object> request = new Dictionary<string, object>();
                if (ack != 0) request["ack"] = ack;
                else request["ack"] = null;
                request["done"] = _Dead;

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

                MakeRequest(postData, "application/xml", 0, null);

                // 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;
                    SecondLife.DebugLogStatic("Sent event queue shutdown message");
                }
            }

            #endregion Make New Request

            #region Callbacks

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

                    //Simulator.Client.DebugLog(
                    //    String.Format("[{0}] Event {1}: {2}{3}", Simulator, msg, Environment.NewLine, LLSD.LLSDDump(body, 0)));

                    if (Simulator.Client.Settings.SYNC_PACKETCALLBACKS)
                        Simulator.Client.Network.CapsEvents.RaiseEvent(msg, body, this);
                    else
                        Simulator.Client.Network.CapsEvents.BeginRaiseEvent(msg, body, this);
                }
            }

            #endregion Callbacks
        }
Exemplo n.º 11
0
        public new void MakeRequest(byte[] postData, string contentType, int udpListeningPort, object state)
        {
            // Create a new HttpWebRequest
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_RequestURL);
            _RequestState = new HttpRequestState(httpRequest);

            if (_ProxyURL != String.Empty)
            {
                // Create a proxy object
                WebProxy proxy = new WebProxy();

                // Associate a new Uri object to the _wProxy object, using the proxy address
                // selected by the user
                proxy.Address = new Uri(_ProxyURL);

                // Finally, initialize the Web request object proxy property with the _wProxy
                // object
                httpRequest.Proxy = proxy;
            }

            // Always disable keep-alive for our purposes
            httpRequest.KeepAlive = false;

            try
            {
                if (postData != null)
                {
                    // POST request
                    _RequestState.WebRequest.Method = "POST";
                    _RequestState.WebRequest.ContentLength = postData.Length;
                    _RequestState.WebRequest.Headers.Add("X-SecondLife-UDP-Listen-Port", Simulator.udpPort.ToString());
                    _RequestState.WebRequest.ContentType = "application/xml";
                    _RequestState.RequestData = postData;

                    IAsyncResult result = (IAsyncResult)_RequestState.WebRequest.BeginGetRequestStream(
                        new AsyncCallback(EventRequestStreamCallback), _RequestState);
                }
                else
                {
                    throw new ArgumentException("postData cannot be null for the event queue", "postData");
                }
            }
            catch (WebException e)
            {
                Abort(false, e);
            }
            catch (Exception e)
            {
                SecondLife.LogStatic(String.Format("CapsEventQueue.MakeRequest(): {0} (Source: {1})", e.Message, e.Source),
                    Helpers.LogLevel.Warning);

                Abort(false, null);
            }
        }
Exemplo n.º 12
0
        private void CreateUserResponse(object response, HttpRequestState state)
        {
            if (response is Dictionary<string, object>)
            {
                // everything is okay
                // FIXME:
                //return new LLUUID(((Dictionary<string, object>)response)["agent_id"].ToString());
            }
            else
            {
                // an error happened
                List<object> al = (List<object>)response;

                StringBuilder sb = new StringBuilder();

                foreach (int ec in al)
                {
                    if (sb.Length > 0)
                        sb.Append("; ");

                    sb.Append(_errors[ec]);
                }

                // FIXME:
                //throw new Exception("failed to create user: " + sb.ToString());
            }
        }
Exemplo n.º 13
0
 private void CheckNameResponse(object response, HttpRequestState state)
 {
     if (response is bool)
     {
         // FIXME:
         //(bool)response;
     }
     else
     {
         // FIXME:
     }
 }
Exemplo n.º 14
0
        private void GatherLastNamesResponse(object response, HttpRequestState state)
        {
            if (response is Dictionary<string, object>)
            {
                Dictionary<string, object> respTable = (Dictionary<string, object>)response;

                _lastNames = new List<LastName>(respTable.Count);

                for (Dictionary<string, object>.Enumerator it = respTable.GetEnumerator(); it.MoveNext(); )
                {
                    LastName ln = new LastName();

                    ln.ID = int.Parse(it.Current.Key.ToString());
                    ln.Name = it.Current.Value.ToString();

                    _lastNames.Add(ln);
                }

                _lastNames.Sort(new Comparison<LastName>(delegate(LastName a, LastName b) { return a.Name.CompareTo(b.Name); }));
            }
        }
Exemplo n.º 15
0
        private void GatherErrorMessagesResponse(object response, HttpRequestState state)
        {
            if (response is Dictionary<string, object>)
            {
                // parse

                //FIXME: wtf?
                foreach (KeyValuePair<string, object> error in (Dictionary<string, object>)response)
                {
                    //StringBuilder sb = new StringBuilder();

                    //sb.Append(error[1]);
                    //sb.Append(" (");
                    //sb.Append(error[0]);
                    //sb.Append("): ");
                    //sb.Append(error[2]);

                    //_errors.Add((int)error[0], sb.ToString());
                }

                // finalize
                _initializing++;
            }
        }
Exemplo n.º 16
0
 protected abstract void RequestSent(HttpRequestState request);
Exemplo n.º 17
0
        public void MakeRequest(byte[] postData, string contentType, int udpListeningPort, object state)
        {
            // Create a new HttpWebRequest
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(_RequestURL);
            _RequestState = new HttpRequestState(httpRequest);
            _RequestState.State = state;

            if (_ProxyURL != String.Empty)
            {
                // Create a proxy object
                WebProxy proxy = new WebProxy();

                // Associate a new Uri object to the _wProxy object, using the proxy address
                // selected by the user
                proxy.Address = new Uri(_ProxyURL);

                // Finally, initialize the Web request object proxy property with the _wProxy
                // object
                httpRequest.Proxy = proxy;
            }

            // Always disable keep-alive for our purposes
            httpRequest.KeepAlive = false;

            try
            {
                if (postData != null)
                {
                    // POST request
                    _RequestState.WebRequest.Method = "POST";
                    _RequestState.WebRequest.ContentLength = postData.Length;
                    if (udpListeningPort > 0)
                        _RequestState.WebRequest.Headers.Add("X-SecondLife-UDP-Listen-Port", udpListeningPort.ToString());
                    if (String.IsNullOrEmpty(contentType))
                        _RequestState.WebRequest.ContentType = "application/xml";
                    else
                        _RequestState.WebRequest.ContentType = contentType;
                    _RequestState.RequestData = postData;

                    IAsyncResult result = (IAsyncResult)_RequestState.WebRequest.BeginGetRequestStream(
                        new AsyncCallback(RequestStreamCallback), _RequestState);

                    // If there is a timeout, the callback fires and the request becomes aborted
                    ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback),
                        _RequestState, HTTP_TIMEOUT, true);
                }
                else
                {
                    // GET request
                    IAsyncResult result = (IAsyncResult)_RequestState.WebRequest.BeginGetResponse(
                        new AsyncCallback(ResponseCallback), _RequestState);

                    // If there is a timeout, the callback fires and the request becomes aborted
                    ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback),
                        _RequestState, HTTP_TIMEOUT, true);

                    // If we get here the request has been initialized, so fire the callback for a request being started
                    RequestSent(_RequestState);
                }
            }
            catch (WebException e)
            {
                Abort(false, e);
            }
            catch (Exception e)
            {
                Log(String.Format("HttpBase.MakeRequest(): {0} (Source: {1})", e.Message, e.Source),
                    Helpers.LogLevel.Warning);

                Abort(false, null);
            }
        }
Exemplo n.º 18
0
 protected override void RequestSent(HttpRequestState request)
 {
     ;
 }
Exemplo n.º 19
0
        private void seedRequest_OnCapsResponse(object response, HttpRequestState state)
        {
            if (response is Dictionary<string, object>)
            {
                Dictionary<string, object> respTable = (Dictionary<string, object>)response;

                StringBuilder capsList = new StringBuilder();

                foreach (string cap in respTable.Keys)
                {
                    capsList.Append(cap);
                    capsList.Append(' ');

                    _Caps[cap] = (string)respTable[cap];
                }

                Simulator.Client.DebugLog("Got capabilities: " + capsList.ToString());

                // Signal that we have connected to the CAPS server and received a list of capability URIs
                CapsReceivedEvent.Set();

                if (_Caps.ContainsKey("EventQueueGet"))
                {
                    _EventQueueCap = new CapsEventQueue(Simulator, _Caps["EventQueueGet"]);
                    Simulator.Client.DebugLog("Starting event queue for " + Simulator.ToString());

                    _EventQueueCap.MakeRequest();
                }
            }
            else
            {
                // The initial CAPS connection failed, try again
                MakeSeedRequest();
            }
        }
Exemplo n.º 20
0
        private void ProvisionCapsResponse(object response, HttpRequestState state)
        {
            if (response is System.Collections.Hashtable)
            {
                System.Collections.Hashtable respTable = (System.Collections.Hashtable)response;

                if (OnProvisionAccount != null) OnProvisionAccount((string)respTable["username"], (string)respTable["password"]);
            }
        }