void Write(IByteBufferAllocator allocator, FullBulkStringRedisMessage message, ICollection <object> output) { Contract.Requires(allocator != null); Contract.Requires(message != null); Contract.Requires(output != null); IByteBuffer buffer = allocator.Buffer( RedisConstants.TypeLength + (message.IsNull ? RedisConstants.NullLength : RedisConstants.LongValueMaximumLength) + RedisConstants.EndOfLineLength); buffer.WriteByte((char)RedisMessageType.BulkString); if (message.IsNull) { buffer.WriteShort(RedisConstants.Null); buffer.WriteShort(RedisConstants.EndOfLine); output.Add(buffer); } else { int readableBytes = message.Content.ReadableBytes; byte[] bytes = this.NumberToBytes(readableBytes); buffer.WriteBytes(bytes); buffer.WriteShort(RedisConstants.EndOfLine); output.Add(buffer); output.Add(message.Content.Retain()); output.Add(allocator .Buffer(RedisConstants.EndOfLineLength) .WriteShort(RedisConstants.EndOfLine)); } }
public void EncodeFullBulkString() { var bulkString = (IByteBuffer)ByteBufOf("bulk\nstring\ntest").Retain(); int length = bulkString.ReadableBytes; var msg = new FullBulkStringRedisMessage(bulkString); Assert.True(this.channel.WriteOutbound(msg)); IByteBuffer written = ReadAll(this.channel); Assert.Equal(BytesOf("$" + length + "\r\nbulk\nstring\ntest\r\n"), BytesOf(written)); written.Release(); }
public void EncodeFullBulkString(string value) { var channel = new EmbeddedChannel(new RedisEncoder()); // Content IByteBuffer bulkStringBuffer = value.Buffer(); bulkStringBuffer.Retain(); int length = bulkStringBuffer.ReadableBytes; var message = new FullBulkStringRedisMessage(bulkStringBuffer); Assert.True(channel.WriteOutbound(message)); IByteBuffer written = ReadAll(channel); byte[] output = written.Bytes(); byte[] expected = $"${length}\r\n{value}\r\n".Bytes(); Assert.Equal(expected.Length, output.Length); Assert.True(output.SequenceEqual(expected)); written.Release(); }
void WriteFullBulkStringMessage(IByteBufferAllocator allocator, FullBulkStringRedisMessage msg, List <object> output) { if (msg.IsNull) { IByteBuffer buf = allocator.Buffer(RedisConstants.TypeLength + RedisConstants.NullLength + RedisConstants.EndOfLineLength); RedisMessageType.BulkString.WriteTo(buf); buf.WriteShort(RedisConstants.NullShort); buf.WriteShort(RedisConstants.EndOfLineShort); output.Add(buf); } else { IByteBuffer headerBuf = allocator.Buffer(RedisConstants.TypeLength + RedisConstants.LongMaxLength + RedisConstants.EndOfLineLength); RedisMessageType.BulkString.WriteTo(headerBuf); headerBuf.WriteBytes(this.NumberToBytes(msg.Content.ReadableBytes)); headerBuf.WriteShort(RedisConstants.EndOfLineShort); output.Add(headerBuf); output.Add(msg.Content.Retain()); output.Add(allocator.Buffer(RedisConstants.EndOfLineLength).WriteShort(RedisConstants.EndOfLineShort)); } }