/** * Function that process all the commands in the given * RSCommandSequence. An assumption is made that all the commands are safe * to send in a single request. If there is a render command, it must be * the last command, and no other commands may have callbacks. */ protected void ProcessCommands(RSCommandSequence seq) { lock (callbackQueue) { // If no commands left to process, flag that we are no longer busy // and continue to process callbacks. if (seq.Commands.Count == 0) { isBusy = false; ProcessCallbacks(); return; } // Array containing the commands to send. IList <RSOutgoingCommand> commands = null; IList <IRSCommand> stateCommands = null; if (seq.StateData != null && seq.StateData.StateCommands != null) { stateCommands = seq.StateData.StateCommands; } // Build the array of commands to send. if (stateCommands == null || stateCommands.Count == 0) { // No state commands, use the seq.Commands directly. commands = seq.Commands; } else { // Build a new array including the state commands with all the commands. commands = new List <RSOutgoingCommand>(); // Add state commands. if (stateCommands != null) { foreach (IRSCommand cmd in stateCommands) { commands.Add(new RSOutgoingCommand(this, cmd)); } } // Add normal commands. foreach (RSOutgoingCommand cmd in seq.Commands) { commands.Add(cmd); } } currentCommands = commands; currentCommandsResponseMap = new Dictionary <int, RSOutgoingCommand>(); StringBuilder arrStr = new StringBuilder(); bool first = true; arrStr.Append('['); foreach (RSOutgoingCommand cmd in commands) { // Add the command to the response map if it has a callaback. if (cmd.CommandId >= 0) { currentCommandsResponseMap[cmd.CommandId] = cmd; } if (!first) { arrStr.Append(','); } first = false; try { arrStr.Append(cmd.ToJSON()); } catch (Exception e) { // FIXME: // Strictly speaking, this callback can't be made // immediately since that breaks the service contract that // response handlers are called in the same order they are // handed to the service. // failed to json serialize the command. call response // handler with an error (if one is registered). cmd.DoClientErrorCallback("Failed to JSON serialize the command. " + e.ToString(), CLIENT_SIDE_ERROR_CODE_INTERNAL); } } arrStr.Append(']'); string url = BaseURL; if (seq.ContainsRenderCommands) { // Do render! using (RSWebClient webClient = new RSWebClient(Timeout)) { webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(RenderCompleted); webClient.Headers.Add("Content-Type", "application/json"); Uri uri = new Uri(url + "?json_rpc_request=" + arrStr.ToString()); Logger.Log("debug", "Getting render: " + uri.ToString()); webClient.DownloadDataAsync(uri); } } else { using (RSWebClient webClient = new RSWebClient(Timeout)) { webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(NonRenderCompleted); webClient.Headers.Add("Content-Type", "application/json"); Logger.Log("debug", "Sending to: " + url + " | " + arrStr.ToString()); webClient.UploadStringAsync(new Uri(url), arrStr.ToString()); } } } }
/** * Function that process all the commands in the given * RSCommandSequence. An assumption is made that all the commands are safe * to send in a single request. If there is a render command, it must be * the last command, and no other commands may have callbacks. */ protected void ProcessCommands(RSCommandSequence seq) { lock (callbackQueue) { // If no commands left to process, flag that we are no longer busy // and continue to process callbacks. if (seq.Commands.Count == 0) { isBusy = false; ProcessCallbacks(); return; } // Array containing the commands to send. IList<RSOutgoingCommand> commands = null; IList<IRSCommand> stateCommands = null; if (seq.StateData != null && seq.StateData.StateCommands != null) { stateCommands = seq.StateData.StateCommands; } // Build the array of commands to send. if (stateCommands == null || stateCommands.Count == 0) { // No state commands, use the seq.Commands directly. commands = seq.Commands; } else { // Build a new array including the state commands with all the commands. commands = new List<RSOutgoingCommand>(); // Add state commands. if (stateCommands != null) { foreach (IRSCommand cmd in stateCommands) { commands.Add(new RSOutgoingCommand(this, cmd)); } } // Add normal commands. foreach (RSOutgoingCommand cmd in seq.Commands) { commands.Add(cmd); } } currentCommands = commands; currentCommandsResponseMap = new Dictionary<int, RSOutgoingCommand>(); StringBuilder arrStr = new StringBuilder(); bool first = true; arrStr.Append('['); foreach (RSOutgoingCommand cmd in commands) { // Add the command to the response map if it has a callaback. if (cmd.CommandId >= 0) { currentCommandsResponseMap[cmd.CommandId] = cmd; } if (!first) { arrStr.Append(','); } first = false; try { arrStr.Append(cmd.ToJSON()); } catch (Exception e) { // FIXME: // Strictly speaking, this callback can't be made // immediately since that breaks the service contract that // response handlers are called in the same order they are // handed to the service. // failed to json serialize the command. call response // handler with an error (if one is registered). cmd.DoClientErrorCallback("Failed to JSON serialize the command. " + e.ToString(), CLIENT_SIDE_ERROR_CODE_INTERNAL); } } arrStr.Append(']'); string url = BaseURL; if (seq.ContainsRenderCommands) { // Do render! using (RSWebClient webClient = new RSWebClient(Timeout)) { webClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(RenderCompleted); webClient.Headers.Add("Content-Type", "application/json"); Uri uri = new Uri(url + "?json_rpc_request=" + arrStr.ToString()); Logger.Log("debug", "Getting render: " + uri.ToString()); webClient.DownloadDataAsync(uri); } } else { using (RSWebClient webClient = new RSWebClient(Timeout)) { webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(NonRenderCompleted); webClient.Headers.Add("Content-Type", "application/json"); Logger.Log("debug", "Sending to: " + url + " | " + arrStr.ToString()); webClient.UploadStringAsync(new Uri(url), arrStr.ToString()); } } } }