public void TestByteConversion() { BitQuadrupleArray array = new BitQuadrupleArray(new[] { new BitQuadruple(Bit.Zero, Bit.One, Bit.One, Bit.Zero), new BitQuadruple(Bit.One, Bit.One, Bit.Zero, Bit.One), new BitQuadruple(Bit.Zero, Bit.One, Bit.Zero, Bit.One) }); array[1] = new BitQuadruple(Bit.One, Bit.Zero, Bit.One, Bit.One); byte[] buffer = array.ToBytes(); Assert.AreEqual(3, array.Length); Assert.AreEqual(2, buffer.Length); Assert.AreEqual((byte)Convert.ToInt32("11010110", 2), buffer[0]); Assert.AreEqual((byte)Convert.ToInt32("00001010", 2), buffer[1]); }
public async Task SendAsync(IMessageChannel channel, BitQuadrupleArray options, int numberOfInvocations) { if (options.Length != numberOfInvocations) { throw new ArgumentException("Provided options must match the specified number of invocations.", nameof(options)); } if (_senderBatch == null || _nextSenderInstanceId + numberOfInvocations > _senderBatch.NumberOfInstances) { throw new InvalidOperationException("Not enough preprocessed sender data available."); } byte[] packedDeltaSelectionIndices = await channel.ReadMessageAsync(); if (packedDeltaSelectionIndices.Length != QuadrupleIndexArray.RequiredBytes(numberOfInvocations)) { throw new DesynchronizationException("Received incorrect number of delta selection indices."); } QuadrupleIndexArray deltaSelectionIndices = QuadrupleIndexArray.FromBytes(packedDeltaSelectionIndices, numberOfInvocations); BitQuadrupleArray maskedOptionQuadruples = new BitQuadrupleArray(numberOfInvocations); for (int i = 0; i < numberOfInvocations; ++i) { int deltaSelectionIndex = deltaSelectionIndices[i]; BitQuadruple preprocessedOptions = _senderBatch.GetOptions(_nextSenderInstanceId + i); BitQuadruple unmaskedOptions = options[i]; BitQuadruple maskedOptions = new BitQuadruple( unmaskedOptions[0] ^ preprocessedOptions[(0 + deltaSelectionIndex) % 4], unmaskedOptions[1] ^ preprocessedOptions[(1 + deltaSelectionIndex) % 4], unmaskedOptions[2] ^ preprocessedOptions[(2 + deltaSelectionIndex) % 4], unmaskedOptions[3] ^ preprocessedOptions[(3 + deltaSelectionIndex) % 4] ); maskedOptionQuadruples[i] = maskedOptions; } await channel.WriteMessageAsync(maskedOptionQuadruples.ToBytes()); _nextSenderInstanceId += numberOfInvocations; }