private async Task SendCommand(OutCommand cmd) { await _writer.WriteLineAsync(cmd._value); await _writer.FlushAsync(); OnRawMessageOut(cmd._value); cmd._tcs.SetResult(true); }
async void HandleOut(OutCommand outCommand, IConnection connection) { await @lock.WriterLockAsync(); try { if (this.connection == connection) { LogoutReason reason = LogoutReason.ConnectionError; if (outCommand.OutCode == "OTH") reason = LogoutReason.LoggedInElsewhere; else if (outCommand.OutCode == "SSD") reason = LogoutReason.ServerShuttingDown; LogoutInner(reason, null); } } finally { @lock.WriterRelease(); } }
private ICommand CommandFromString(string name, string[] arguments) { ICommand command = null; switch (name) { case "SET": { Register register = Registers[arguments[0]]; int value = int.Parse(arguments[1]); command = new SetCommand(register, value); break; } case "ADD": { Register registerOne = Registers[arguments[0]]; Register registerTwo = Registers[arguments[1]]; Register registerOut = Registers[arguments[2]]; command = new AddCommand(registerOne, registerTwo, registerOut); break; } case "SUB": { Register registerOne = Registers[arguments[0]]; Register registerTwo = Registers[arguments[1]]; Register registerOut = Registers[arguments[2]]; command = new SubtractCommand(registerOne, registerTwo, registerOut); break; } case "MUL": { Register registerOne = Registers[arguments[0]]; Register registerTwo = Registers[arguments[1]]; Register registerOut = Registers[arguments[2]]; command = new MultiplyCommand(registerOne, registerTwo, registerOut); break; } case "DIV": { Register registerOne = Registers[arguments[0]]; Register registerTwo = Registers[arguments[1]]; Register registerOut = Registers[arguments[2]]; command = new DivideCommand(registerOne, registerTwo, registerOut); break; } case "OUT": { if (Registers.ContainsKey(arguments[0])) { Register register = Registers[arguments[0]]; command = new OutCommand(register); } else { command = new OutCommand(arguments[0]); } break; } case "HLT": { command = new HaltCommand(); break; } case "JMP": { string label = arguments[0]; command = new JumpCommand(label); break; } case "JIF": { Register registerOne = Registers[arguments[0]]; Register registerTwo = Registers[arguments[2]]; string conditionString = arguments[1]; JumpCondition jumpCondition = JumpCondition.Undefined; switch (conditionString) { case "==": jumpCondition = JumpCondition.Equal; break; case "<": jumpCondition = JumpCondition.LessThan; break; case "<=": jumpCondition = JumpCondition.LessThanOrEqual; break; case ">": jumpCondition = JumpCondition.GreaterThan; break; case ">=": jumpCondition = JumpCondition.GreaterThanOrEqual; break; case "!=": jumpCondition = JumpCondition.NotEqual; break; } string label = arguments[3]; command = new JumpIfCommand(registerOne, registerTwo, label, jumpCondition); break; } case "CPY": { Register registerOne = Registers[arguments[0]]; Register registerTwo = Registers[arguments[1]]; command = new CopyCommand(registerOne, registerTwo); break; } default: if (name == "str:") { string stringKey = arguments[0]; string stringValue = string.Empty; for (int i = 1; i < arguments.Length; i++) { stringValue += arguments[i]; if (i < arguments.Length) { stringValue += " "; } } Program.RegisterString(stringKey, stringValue); } else if (name.Length > 1 && name.EndsWith(":")) { Program.MakeLabel(name.Substring(0, name.Length - 1)); break; } break; } return(command); }