Ejemplo n.º 1
0
        /**
         * Process the given data and error for a render response.
         */
        public void ProcessRenderResponse(byte[] data, string error, bool valid = true)
        {
            RSOutgoingCommand lastCommand   = currentCommands[currentCommands.Count - 1];
            RSRenderCommand   renderCommand = lastCommand.Command as RSRenderCommand;

            if (renderCommand == null)
            {
                Logger.Log("error", "Error getting last render command.");
                ResponseCompleted();
                return;
            }

            // If our render target can't be used anymore then ignore
            if (renderCommand.Target != null && !renderCommand.Target.Valid())
            {
                return;
            }

            try
            {
                if (data != null && data.Length >= 2 && (char)data[0] == '[' && (char)data[1] == '{')
                {
                    ArrayList errors    = JSON.JsonDecode(System.Text.Encoding.UTF8.GetString(data)) as ArrayList;
                    Hashtable errorResp = errors[0] as Hashtable;
                    Hashtable errorObj  = errorResp["error"] as Hashtable;
                    error = errorObj["message"].ToString();
                }

                if (error != null)
                {
                    lastCommand.DoClientErrorCallback(error, -2);
                    renderCommand.Target.OnError(error);
                }
                else if (valid)
                {
                    if (valid)
                    {
                        bool continueProcessing = renderCommand.Target.OnLoad(renderCommand, this, data);
                        if (!continueProcessing)
                        {
                            return;
                        }
                    }
                    lastCommand.DoResultCallback(new Hashtable()
                    {
                        { "result", new Hashtable() }, { "valid", valid }
                    });
                }
            }
            catch (Exception e)
            {
                Logger.Log("error", "Error handling render callback: " + e.ToString());
            }

            ResponseCompleted();
        }
Ejemplo n.º 2
0
        public void ProcessResponses(string str)
        {
            Logger.Log("debug", "Received: " + str);
            ArrayList responses = null;

            try
            {
                responses = JSON.JsonDecode(str) as ArrayList;
            }
            catch (Exception exp)
            {
                // Error
                Logger.Log("error", "Error parsing responses: " + exp.ToString());
                ResponseCompleted();
                return;
            }

            if (responses == null)
            {
                Logger.Log("error", "Error parsing responses: parsed responses was null.");
                ResponseCompleted();
                return;
            }

            for (int i = 0; i < responses.Count; i++)
            {
                Hashtable resp = responses[i] as Hashtable;
                if (resp == null)
                {
                    // Error
                    Logger.Log("error", "Unable to get response as hashtable: " + responses[i]);
                    continue;
                }

                if (resp.Contains("id"))
                {
                    int id = Convert.ToInt32(resp["id"]);
                    RSOutgoingCommand cmd = currentCommandsResponseMap[id];
                    cmd.DoResultCallback(resp);
                }
                else
                {
                    Logger.Log("error", "Response does not contain an id: " + resp);
                }
            }

            ResponseCompleted();
        }
Ejemplo n.º 3
0
        /**
         * Returns a sub sequence as an RSCommandSequence object that is safe to send in a single HTTP request.
         */
        public RSCommandSequence GetSafeSubsequence()
        {
            RSCommandSequence safeSeq = new RSCommandSequence(Service, StateData);

            if (commands.Count == 0)
            {
                return(safeSeq);
            }

            // Does the subsequence contain callbacks.
            bool hasCallbacks = false;
            // Does the subsequence contain a render.
            bool hasRenderCommand = false;

            // Go through commands an decide which are safe in a single request
            int i = 0;

            for (; i < commands.Count; i++)
            {
                RSOutgoingCommand command = commands[i];
                if (command.Command is RSRenderCommand)
                {
                    // Two cases:
                    if (hasCallbacks)
                    {
                        // Case 1: There are callbacks,
                        //         so not safe to include the render command.
                        break;
                    }

                    // Case 2: There are no callbacks, so we can add the render
                    //         command to the safe sequence and then break.
                    hasRenderCommand = true;
                    if (command.Callback != null)
                    {
                        hasCallbacks = true;
                    }

                    // We must increase i otherwise the render command will not be included.
                    i++;
                    break;
                }

                if (command.Callback != null)
                {
                    hasCallbacks = true;
                }
            }

            // i now equals the number of commands that are safe to send. Splice the
            // array into a safe part (return) and a non-safe part (keep).
            List <RSOutgoingCommand> safeCommands = commands;

            if (i < commands.Count)
            {
                safeCommands = commands.GetRange(0, i);
                commands.RemoveRange(0, i);
            }
            else
            {
                commands = new List <RSOutgoingCommand>();
            }

            safeSeq.commands = safeCommands;
            safeSeq.ContainsResponseHandlers = hasCallbacks;
            safeSeq.ContainsRenderCommands   = hasRenderCommand;

            return(safeSeq);
        }