private void ApplyChange(MessageReceivedEventArgs args) { if (args.Message.Slot != this.emberDataMessage.Slot) { return; } var command = (EmberData)args.Message.Command; if (command.Dtd != EmberDataCommand.Dtd) { throw new ModelException(string.Format(InvariantCulture, "Unexpected DTD: {0:X2}.", command.Dtd)); } var actualBytes = command.ApplicationBytes; var expectedBytes = EmberDataCommand.ApplicationBytes; if ((actualBytes.Count != expectedBytes.Count) || (actualBytes.Zip(expectedBytes, (l, r) => l - r).Reverse().FirstOrDefault(b => b != 0) < 0)) { throw new ModelException(string.Format( InvariantCulture, "Encountered actual Glow DTD Version {0} while expecting >= {1}.", GetVersion(actualBytes), GetVersion(expectedBytes))); } var payload = args.GetPayload(); // TODO: Reuse MemoryStream and EmberReader for all incoming messages. using (var stream = new MemoryStream(payload)) using (var reader = new EmberReader(stream)) { this.root.Read(reader, this.pendingInvocations, this.streamedParameters); } }