private ICommand ReadCommandFrom(PresentationContextInputStream stream) { var input = new BufferedStreamReader(stream); var command = CommandSerialization.ReadFrom(input); if (command is IMayHaveDataSet mayHaveDataSet) { if (command.IsFollowedByDataSet()) { _pendingCommand = command; _pendingPresentationContext = stream.PresentationContextID; } else if (mayHaveDataSet.IsDataSetRequired()) { throw new IOException($"{command} must be followed by a data set"); } } else if (command.IsFollowedByDataSet()) { throw new IOException($"Unexpected data set received for command {command}"); } stream.SkipToEnd(); if (TraceWriter != null) { NetUtils.TraceOutput(TraceWriter, $"PC {stream.PresentationContextID} received ", command); } return(command); }
public void CreateCommandAdapterTest() { string aggregateId = "Box"; string aggregateType = typeof(Location).FullName; string commandId = Guid.NewGuid().ToString(); string correlationId = commandId; CreateLocation createLocationCommand = new CreateLocation(aggregateId); string jsonCommand = new CommandSerialization().SerializeCommand(createLocationCommand); var commandAdapter = new CommandSerialization().DeserializeCommand(commandId, correlationId, aggregateId, jsonCommand); Assert.IsInstanceOfType(commandAdapter, typeof(ICommand)); Assert.IsInstanceOfType(commandAdapter, typeof(ICommand <CreateLocation>)); }
public void SendCommand(byte presentationContextID, ICommand command, Action <Stream> dataSetWriter) { if (command is IMayHaveDataSet mayHaveDataSet) { if ((dataSetWriter == null) && mayHaveDataSet.IsDataSetRequired()) { throw new ArgumentException($"{command} must be followed by a data set"); } } else if (dataSetWriter != null) { throw new ArgumentException($"{command} must not be followed by a data set"); } var commandAttribute = command.GetType().GetCustomAttribute <CommandAttribute>(); command.CommandField = commandAttribute.CommandType; command.CommandDataSetType = (ushort)((dataSetWriter == null) ? 0x0101 : 0xFEFE); if (TraceWriter != null) { NetUtils.TraceOutput(TraceWriter, $"PC {presentationContextID} sending ", command); } using (var stream = new PresentationContextOutputStream(this, presentationContextID, FragmentType.Command)) { var output = new BufferedStreamWriter(stream); CommandSerialization.WriteTo(output, command); output.Flush(FlushMode.Deep); } if (dataSetWriter != null) { using (var stream = new PresentationContextOutputStream(this, presentationContextID, FragmentType.DataSet)) { dataSetWriter.Invoke(stream); stream.Flush(); } } }