/// <summary> /// EncodePacketIdVariableHeader /// </summary> /// <param name="buffer"></param> public override void Encode(IByteBuffer buffer) { var buf = Unpooled.Buffer(); try { EncodePacketId(buf); FixedHeader.RemaingLength = buf.ReadableBytes; FixedHeader.WriteTo(buffer); buffer.WriteBytes(buf); buf = null; } finally { buf?.Release(); } }
public void Checks_payload_mac_then_decrypts_header_payload() { IByteBuffer buffer = Unpooled.Buffer(256); buffer.WriteBytes(_frame); UnderTest underTest = new UnderTest(_frameCipher, _macProcessor); underTest.Decode(buffer); Received.InOrder( () => { _macProcessor.Received().CheckMac(Arg.Any <byte[]>(), 32, LongFrameSize, false); _frameCipher.Received().Decrypt(Arg.Any <byte[]>(), 32, LongFrameSize, Arg.Any <byte[]>(), 32); } ); }
public void Checks_header_mac_then_decrypts() { IByteBuffer buffer = Unpooled.Buffer(256); buffer.WriteBytes(_frame); UnderTest underTest = new UnderTest(_frameCipher, _macProcessor); underTest.Decode(buffer); Received.InOrder( () => { _macProcessor.Received().CheckMac(Arg.Any <byte[]>(), 0, 16, true); _frameCipher.Received().Decrypt(Arg.Any <byte[]>(), 0, 16, Arg.Any <byte[]>(), 0); } ); }
public EchoClientHandler() { this.buffer = new byte[EchoClientSettings.Size]; this.initialMessage = Unpooled.Buffer(EchoClientSettings.Size); this.serializer = new DefaultSerializer(); var request = new MessageRequest(); request.MessageId = Guid.NewGuid().ToString(); request.ClassName = "yixin.base.service.carservice"; request.MethodName = "getlist"; request.ParamTypes = new Type[2] { typeof(int), typeof(string) }; request.Parameters = new object[] { 123, "onsale" }; // byte[] messageBytes = Encoding.UTF8.GetBytes("Hello world"); byte[] messageBytes = this.serializer.Serialize(request); this.initialMessage.WriteBytes(messageBytes); }
public void ShouldEqualExpect(short obj, short version, Codec codec, int tag, byte tarsStructType) { var buffer = Unpooled.Buffer(1); var options = new TarsConvertOptions() { Codec = codec, Version = version, Tag = tag }; sut.Serialize(obj, buffer, options); headHandler.ReadHead(buffer, options); var result = sut.Deserialize <short>(buffer, options); Assert.Equal(obj, result); Assert.Equal(tag, options.Tag); Assert.Equal(tarsStructType, options.TarsType); }
public void TestExtractNioBuffers() { IByteBuffer buf1 = Unpooled.DirectBuffer(10); IByteBuffer buf2 = Unpooled.Buffer(10); IByteBuffer buf3 = Unpooled.DirectBuffer(10); buf1.WriteBytes(Encoding.ASCII.GetBytes("a")); buf2.WriteBytes(Encoding.ASCII.GetBytes("b")); buf3.WriteBytes(Encoding.ASCII.GetBytes("c")); IByteBuffer composite = Unpooled.WrappedUnmodifiableBuffer(buf1, buf2, buf3); var byteBuffers = composite.GetIoBuffers(0, 3); Assert.Equal(3, byteBuffers.Length); Assert.Single(byteBuffers[0]); Assert.Single(byteBuffers[1]); Assert.Single(byteBuffers[2]); composite.Release(); }
public override void ChannelRead(IChannelHandlerContext context, object message) { var buffer = message as IByteBuffer; if (buffer != null) { //Console.WriteLine("Received from client: " + buffer.ToString(Encoding.UTF8)); var initialMessage = Unpooled.Buffer(256); byte[] messageBytes = Encoding.UTF8.GetBytes("Hello " + buffer.ToString(Encoding.UTF8)); initialMessage.WriteBytes(messageBytes); context.WriteAsync(initialMessage); } else { context.WriteAsync(message); } }
public override void Encode(IByteBuffer buffer) { var buf = Unpooled.Buffer(); try { // variable header buf.WriteString(ProtocolName); //byte 1 - 8 buf.WriteByte(ProtocolLevel); //byte 9 // connect flags; //byte 10 var flags = UsernameFlag.ToByte() << 7; flags |= PasswordFlag.ToByte() << 6; flags |= WillRetain.ToByte() << 5; flags |= ((byte)WillQos) << 3; flags |= WillFlag.ToByte() << 2; flags |= CleanSession.ToByte() << 1; buf.WriteByte((byte)flags); // keep alive buf.WriteShort(KeepAlive); //byte 11 - 12 // payload buf.WriteString(ClientId); if (WillFlag) { buf.WriteString(WillTopic); buf.WriteBytes(WillMessage); } if (UsernameFlag && PasswordFlag) { buf.WriteString(UserName); buf.WriteString(Password); } FixedHeader.RemaingLength = buf.ReadableBytes; FixedHeader.WriteFixedHeader(buffer); buffer.WriteBytes(buf); } finally { buf?.SafeRelease(); } }
public static byte[] BuildContent(IdType idType, MsgType msgType, OpsType opsType, int attr, int serial, byte[] core) { var coreLen = core == null ? 0 : core.Length; var buffer = Unpooled.Buffer(coreLen + 20); var msgId = Convert.ToInt32(idType); var msgIdBytes = BytesUtil.Int32ToBytes(msgId); buffer.WriteBytes(msgIdBytes); var timestamp = TimeUtil.GetTimeStamp(); var timeBytes = BytesUtil.Int32ToBytes(timestamp); buffer.WriteBytes(timeBytes); var msgTypeCode = Convert.ToInt16(msgType); var msgTypeBytes = BytesUtil.Int16ToBytes(msgTypeCode); buffer.WriteBytes(msgTypeBytes); var opsTypeCode = Convert.ToInt16(opsType); var opsTypeBytes = BytesUtil.Int16ToBytes(opsTypeCode); buffer.WriteBytes(opsTypeBytes); var attrBytes = BytesUtil.Int16ToBytes((Int16)attr); buffer.WriteBytes(attrBytes); var length = coreLen + 4; var lenBytes = BytesUtil.Int16ToBytes((Int16)length); buffer.WriteBytes(lenBytes); var serialBytes = BytesUtil.Int32ToBytes(serial); buffer.WriteBytes(serialBytes); if (core != null) { buffer.WriteBytes(core); } return(buffer.Array); }
public void PartialReadGenerator_will_correctly_handle_multiple_reads() { var prepender = new LengthFieldPrepender(4, false); var decoder = new LengthFieldBasedFrameDecoder(int.MaxValue, 0, 4, 0, 4); var partialReader = new PartialReadGenerator(new[] { new ReadInstruction(ReadMode.Partial, 2, 8), new ReadInstruction(ReadMode.Partial, 4, 8), new ReadInstruction(ReadMode.Full, 8, 8) }); var ec = new EmbeddedChannel(partialReader, prepender, decoder); var byteBuf1 = Unpooled.Buffer(10).WriteInt(4).WriteInt(10); var byteBuf2 = Unpooled.Buffer(100).WriteInt(5).WriteInt(6).WriteBytes(new byte[92]); var byteBuf3 = Unpooled.Buffer(10).WriteInt(21).WriteInt(11); Task.WaitAll(ec.WriteAndFlushAsync(byteBuf1.Duplicate().Retain()), ec.WriteAndFlushAsync(byteBuf2.Duplicate().Retain()), ec.WriteAndFlushAsync(byteBuf3.Duplicate().Retain())); IByteBuf encoded; do { encoded = ec.ReadOutbound <IByteBuf>(); if (encoded != null) { ec.WriteInbound(encoded.Duplicate().Retain()); } } while (encoded != null); // finish the read ec.Pipeline.FireUserEventTriggered(PartialReadGenerator.FinishPartialReads.Instance); var inbound1 = ec.ReadInbound <IByteBuf>(); var inbound2 = ec.ReadInbound <IByteBuf>(); var inbound3 = ec.ReadInbound <IByteBuf>(); Assert.Equal(byteBuf1.ResetReaderIndex(), inbound1, AbstractByteBuf.ByteBufComparer); Assert.Equal(byteBuf2.ResetReaderIndex(), inbound2, AbstractByteBuf.ByteBufComparer); Assert.Equal(byteBuf3.ResetReaderIndex(), inbound3, AbstractByteBuf.ByteBufComparer); byteBuf1.Release(); byteBuf2.Release(); byteBuf3.Release(); }
public override void ChannelRead(IChannelHandlerContext context, object message) { if (message is IByteBuffer buffer) { var clientMessage = buffer.ToString(Encoding.UTF8); Logger.Debug("Received from client: " + clientMessage); var result = WorkDispatcher.DoIt(clientMessage); Logger.Info("Returning Working Result"); Logger.Debug(result); var resultBuffer = Unpooled.Buffer(256); byte[] messageBytes = Encoding.UTF8.GetBytes(result); resultBuffer.WriteBytes(messageBytes); context.WriteAsync(resultBuffer); } }
public void WhenHasBytesShouldNotEmpty(short version, Codec codec, int tag, string str) { var buffer = Unpooled.Buffer(1); var options = new TarsConvertOptions() { Codec = codec, Version = version, Tag = tag, }; sut.Serialize(Unpooled.WrappedBuffer(Encoding.UTF8.GetBytes(str)), buffer, options); Assert.True(options.HasValue); Assert.True(buffer.ReadableBytes > 0); options.Tag = 0; headHandler.ReadHead(buffer, options); Assert.Equal(TarsStructType.ByteArray, options.TarsType); Assert.Equal(tag, options.Tag); Assert.Equal(str, Encoding.UTF8.GetString(sut.Deserialize <IByteBuffer>(buffer, options).Slice().Array)); }
public void AddEmptyBufferInMiddle() { CompositeByteBuffer cbuf = Unpooled.CompositeBuffer(); IByteBuffer buf1 = Unpooled.Buffer().WriteByte(1); cbuf.AddComponent(true, buf1); cbuf.AddComponent(true, Unpooled.Empty); IByteBuffer buf3 = Unpooled.Buffer().WriteByte(2); cbuf.AddComponent(true, buf3); Assert.Equal(2, cbuf.ReadableBytes); Assert.Equal((byte)1, cbuf.ReadByte()); Assert.Equal((byte)2, cbuf.ReadByte()); Assert.Same(Unpooled.Empty, cbuf.InternalComponent(1)); Assert.NotSame(Unpooled.Empty, cbuf.InternalComponentAtOffset(1)); cbuf.Release(); }
public void ReserveAndWriteUtf8InvalidSubsequences(int start, int end) { IByteBuffer buf = Unpooled.Buffer(16); try { ByteBufferUtil.ReserveAndWriteUtf8(buf, (AsciiString)"Some UTF-8 like äÄ∏ŒŒ", start, end, 32); Assert.False(true, "Did not throw IndexOutOfBoundsException for range (" + start + ", " + end + ")"); } catch (IndexOutOfRangeException) { // expected } finally { Assert.False(buf.IsReadable()); buf.Release(); } }
public void WhenEmptyShouldBeEmpty(short version, Codec codec, int tag) { var buffer = Unpooled.Buffer(1); var options = new TarsConvertOptions() { Codec = codec, Version = version, Tag = tag, }; sut.Serialize(Unpooled.Empty, buffer, options); Assert.True(options.HasValue); Assert.True(buffer.ReadableBytes > 0); options.Tag = 0; headHandler.ReadHead(buffer, options); Assert.Equal(TarsStructType.ByteArray, options.TarsType); Assert.Equal(tag, options.Tag); Assert.Equal(Unpooled.Empty, sut.Deserialize <IByteBuffer>(buffer, options)); }
public void WriteUsAsciiCompositeWrapped() { string usAscii = "NettyRocks"; var buf = Unpooled.Buffer(16); buf.WriteBytes(Encoding.ASCII.GetBytes(usAscii)); var buf2 = new WrappedCompositeByteBuffer(Unpooled.CompositeBuffer().AddComponent( Unpooled.Buffer(8)).AddComponent(Unpooled.Buffer(24))); // write some byte so we start AddComponent with an offset. buf2.WriteByte(1); ByteBufferUtil.WriteAscii(buf2, usAscii); // Skip the previously written byte. Assert.Equal(buf, buf2.SkipBytes(1)); buf.Release(); buf2.Release(); }
public void WriteUtf8CompositeWrapped() { string utf8 = "Some UTF-8 like äÄ∏ŒŒ"; var buf = Unpooled.Buffer(16); buf.WriteBytes(TextEncodings.UTF8NoBOM.GetBytes(utf8)); var buf2 = new WrappedCompositeByteBuffer(Unpooled.CompositeBuffer().AddComponent( Unpooled.Buffer(8)).AddComponent(Unpooled.Buffer(24))); // write some byte so we start writing with an offset. buf2.WriteByte(1); ByteBufferUtil.WriteUtf8(buf2, utf8); // Skip the previously written byte. Assert.Equal(buf, buf2.SkipBytes(1)); buf.Release(); buf2.Release(); }
public void FailedWhenDataFrameNotAssociateWithStream() { var input = Unpooled.Buffer(); var payload = Unpooled.Buffer(); try { payload.WriteByte(1); Http2CodecUtil.WriteFrameHeader(input, payload.ReadableBytes, Http2FrameTypes.Data, new Http2Flags().EndOfStream(true), 0); input.WriteBytes(payload); Assert.Throws <Http2Exception>(() => frameReader.ReadFrame(this.ctx.Object, input, this.listener.Object)); } finally { payload.Release(); input.Release(); } }
// STREAM刷新消息 public void FlushMessageAsync(Head headpackage) { // 序列,防止重发 lock (SyncMessage) { headpackage.SeqID = ++SequenceId; IByteBuffer iniMessage = Unpooled.Buffer(headpackage.CalculateSize()); iniMessage.WriteBytes(headpackage.ToByteArray()); try { connect_channel.WriteAndFlushAsync(iniMessage); } catch (Exception e) { Debug.Fail($"connect_channel error:{e.Message}"); } } }
public void CantRelease() { IByteBuffer buffer = Unpooled.UnreleasableBuffer(Unpooled.Buffer(1)); Assert.Equal(1, buffer.ReferenceCount); Assert.False(buffer.Release()); Assert.Equal(1, buffer.ReferenceCount); Assert.False(buffer.Release()); Assert.Equal(1, buffer.ReferenceCount); buffer.Retain(5); Assert.Equal(1, buffer.ReferenceCount); buffer.Retain(); Assert.Equal(1, buffer.ReferenceCount); Assert.True(buffer.Unwrap().Release()); Assert.Equal(0, buffer.ReferenceCount); }
public void Should_decoded_multiple_lengthFramed_messages() { var binaryContent1 = Encoding.UTF8.GetBytes("somebytes"); var binaryContent2 = Encoding.UTF8.GetBytes("moarbytes"); var binaryContent3 = BitConverter.GetBytes(100034034L); var buffer = Unpooled.Buffer(100).WriteInt(binaryContent1.Length) .WriteBytes(binaryContent1).WriteInt(binaryContent2.Length).WriteBytes(binaryContent2) .WriteInt(binaryContent3.Length).WriteBytes(binaryContent3); List <IByteBuf> decodedMessages; Decoder.Decode(TestConnection, buffer, out decodedMessages); Assert.Equal(3, decodedMessages.Count); Assert.True(binaryContent1.SequenceEqual(decodedMessages[0].ToArray())); Assert.True(binaryContent2.SequenceEqual(decodedMessages[1].ToArray())); Assert.True(binaryContent3.SequenceEqual(decodedMessages[2].ToArray())); }
public override void Encode(IByteBuffer buffer) { var buf = Unpooled.Buffer(); try { buf.WriteString(TopicName); EncodePacketId(buf); buf.WriteBytes(Payload, 0, Payload.Length); FixedHeader.RemaingLength = buf.ReadableBytes; FixedHeader.WriteTo(buffer); buffer.WriteBytes(buf); } finally { buf?.Release(); } }
public void EnsureWritableShouldThrow() { var buf = Unpooled.Buffer(1); var readOnly = buf.AsReadOnly(); try { readOnly.EnsureWritable(1); Assert.False(true); } catch (Exception ex) { Assert.True(ex is ReadOnlyBufferException); } finally { buf.Release(); } }
public async Task <RemoteInvokeResultMessage> SendAsync(TransportMessage message) { var callback = RegisterCallBack(message.Id); try { var senderString = JsonConvert.SerializeObject(message); byte[] messageBytes = Encoding.UTF8.GetBytes(senderString); var initMessage = Unpooled.Buffer(messageBytes.Length); initMessage.WriteBytes(messageBytes); await _channel.WriteAndFlushAsync(initMessage); } catch (Exception e) { throw new RpcException("与服务端通讯时发生了异常。", e); } return(await callback); }
private static void RoundTrip(HpackHuffmanEncoder encoder, byte[] buf) { var buffer = Unpooled.Buffer(); try { encoder.Encode(buffer, new AsciiString(buf, false)); byte[] bytes = new byte[buffer.ReadableBytes]; buffer.ReadBytes(bytes); byte[] actualBytes = Decode(bytes); Assert.Equal(buf, actualBytes); } finally { buffer.Release(); } }
private void Test(string frame, Func <byte[], IByteBuffer, ZeroFrameDecoderTestWrapper, IByteBuffer> deliveryStrategy, string expectedOutput) { byte[] frameBytes = Bytes.FromHexString(frame); IByteBuffer input = Unpooled.Buffer(256); ZeroFrameDecoderTestWrapper zeroFrameDecoderTestWrapper = new(_frameCipher, _macProcessor); IByteBuffer result = deliveryStrategy(frameBytes, input, zeroFrameDecoderTestWrapper); Assert.NotNull(result, "did not decode frame"); byte[] resultBytes = new byte[result.ReadableBytes]; result.ReadBytes(resultBytes); TestContext.WriteLine(resultBytes.ToHexString()); string expected = expectedOutput; TestContext.WriteLine(resultBytes.ToHexString()); Assert.AreEqual(expected, resultBytes.ToHexString()); Assert.AreEqual(input.ReaderIndex, input.WriterIndex, "reader index == writer index"); }
public override Task WriteAsync(IChannelHandlerContext context, object msg) { if (!(msg is ClusterMessage message)) { return(base.WriteAsync(context, null)); } message.Encode(); message.Encrypt(); var header = Unpooled.Buffer(5); header.WriteUnsignedShort(message.Id); header.WriteMedium(message.Length); base.WriteAsync(context, header); return(base.WriteAsync(context, message.Writer)); }
public void SetUp(BenchmarkContext context) { this.ServerGroup = new MultithreadEventLoopGroup(1); this.WorkerGroup = new MultithreadEventLoopGroup(); Encoding iso = Encoding.GetEncoding("ISO-8859-1"); IByteBuffer buf = Unpooled.Buffer().WriteInt(3).WriteBytes(iso.GetBytes("ABC")); this.message = new byte[buf.ReadableBytes]; buf.GetBytes(buf.ReaderIndex, this.message); this.inboundThroughputCounter = context.GetCounter(InboundThroughputCounterName); var counterHandler = new CounterHandlerInbound(this.inboundThroughputCounter); this.signal = new ManualResetEventSlimReadFinishedSignal(this.ResetEvent); // using default settings this.serverBufferAllocator = new PooledByteBufferAllocator(); ServerBootstrap sb = new ServerBootstrap() .Group(this.ServerGroup, this.WorkerGroup) .Channel <TcpServerSocketChannel>() .ChildOption(ChannelOption.Allocator, this.serverBufferAllocator) .ChildHandler(new ActionChannelInitializer <TcpSocketChannel>(channel => { channel.Pipeline .AddLast(this.GetEncoder()) .AddLast(this.GetDecoder()) .AddLast(counterHandler) .AddLast(new ReadFinishedHandler(this.signal, WriteCount)); })); // start server this.serverChannel = sb.BindAsync(TEST_ADDRESS).Result; // connect to server var address = (IPEndPoint)this.serverChannel.LocalAddress; this.ClientSocket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); this.ClientSocket.Connect(address.Address, address.Port); this.Stream = new NetworkStream(this.ClientSocket, true); }
private void WriteHeaderFrameWithData(IByteBuffer output, int streamId, IHttp2Headers headers, IByteBuffer dataPayload) { IByteBuffer headerBlock = Unpooled.Buffer(); try { hpackEncoder.EncodeHeaders(streamId, headerBlock, headers, NeverSensitiveDetector.Instance); Http2CodecUtil.WriteFrameHeader(output, headerBlock.ReadableBytes, Http2FrameTypes.Headers, new Http2Flags().EndOfHeaders(true), streamId); output.WriteBytes(headerBlock, headerBlock.ReadableBytes); Http2CodecUtil.WriteFrameHeader(output, dataPayload.ReadableBytes, Http2FrameTypes.Data, new Http2Flags().EndOfStream(true), streamId); output.WriteBytes(dataPayload); } finally { headerBlock.Release(); } }
public byte[] GetBytes() { var buffer = Unpooled.Buffer(); buffer.unpackdd(Found.Count); foreach (var item in Found) { buffer.WriteByte(item); } buffer.unpackdd(Result.Count); foreach (var item in Result) { buffer.WriteBytes(item?.GetBytes()); } var by = new Byte[buffer.ReadableBytes]; buffer.ReadBytes(by); return(by); }