diff --git a/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java b/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java index 0549d32de291b135591ab77c9e680dd4afc9b109..3fe7e9038c00ca003f11ff384df4fde867771158 100644 --- a/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java +++ b/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/Server.java @@ -52,6 +52,7 @@ public class Server { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { log.info("3"); log.info("h3结果{},class={}", msg, msg.getClass()); + //入栈处理器最后的写入,供出栈处理器读取 ch.writeAndFlush(ctx.alloc().buffer().writeBytes("server...".getBytes())); } }); diff --git a/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/TestEmbeddedChannel.java b/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/TestEmbeddedChannel.java new file mode 100644 index 0000000000000000000000000000000000000000..57b653f74b05893582282f05aa00a136e386acf4 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_06_pipeline/TestEmbeddedChannel.java @@ -0,0 +1,80 @@ +package com.kwan.shuyu.heima.netty_06_pipeline; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelOutboundHandlerAdapter; +import io.netty.channel.ChannelPromise; +import io.netty.channel.embedded.EmbeddedChannel; +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.Charset; + +/** + * netty提供的 EmbeddedChannel 测试方法 + * 可以 绑定 很多的 handler 进行测试 + *

+ *

+ * 入站:服务端 处理 客户端 write事件 + */ +@Slf4j +public class TestEmbeddedChannel { + public static void main(String[] args) { + // 1. 入站 + ChannelInboundHandlerAdapter h1 = new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.info("1"); + super.channelRead(ctx, msg); + } + }; + // 2. 入站 + ChannelInboundHandlerAdapter h2 = new ChannelInboundHandlerAdapter() {// 当前handler起名 h2 + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.info("2"); + super.channelRead(ctx, msg); + } + }; + // 3. 入站 + ChannelInboundHandlerAdapter h3 = new ChannelInboundHandlerAdapter() { + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + log.info("3333333333333333"); + ctx.channel().writeAndFlush(msg); // 【最后一个handler往前找】 + // ctx.writeAndFlush(msg); // 【当前节点往上找 出站处理器】 + } + }; + // 4. 出站 + ChannelOutboundHandlerAdapter h4 = new ChannelOutboundHandlerAdapter() { + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + log.info("4"); + super.write(ctx, msg, promise); + } + }; + // 5. 出站 + ChannelOutboundHandlerAdapter h5 = new ChannelOutboundHandlerAdapter() { + @Override + public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { + log.info("5"); + super.write(ctx, msg, promise); + } + }; + final EmbeddedChannel channel = new EmbeddedChannel(h1, h2, h3, h4, h5); + // 模拟 入站 + channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("hello".getBytes())); + System.out.println(((ByteBuf) channel.readOutbound()).toString(Charset.defaultCharset())); + + /** + * 输出结果 + * [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 1 + * [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 2 + * [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 3333333333333333 + * [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 5 + * [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 4 + * hello + */ + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_01.java b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_01.java new file mode 100644 index 0000000000000000000000000000000000000000..7f323dee4f9c8741a64d21ad63fcfde55bb54014 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_01.java @@ -0,0 +1,68 @@ +package com.kwan.shuyu.heima.netty_07_bytebuf; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; + +import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump; +import static io.netty.util.internal.StringUtil.NEWLINE; + +/** + * 测试 ByteBuf 扩容 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/27 09:34 + */ +public class ByteBuf_01 { + + public static void main(String[] args) { + ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(); + log(buf); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 300; i++) { + sb.append("a"); + } + buf.writeBytes(sb.toString().getBytes()); + log(buf); + + /** + * PooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 256) + * read index:0 write index:300 capacity:512 + * +-------------------------------------------------+ + * | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + * +--------+-------------------------------------------------+----------------+ + * |00000000| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000010| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000020| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000030| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000040| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000050| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000060| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000070| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000080| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000090| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |000000a0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |000000b0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |000000c0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |000000d0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |000000e0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |000000f0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000100| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000110| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa| + * |00000120| 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaa | + * +--------+-------------------------------------------------+----------------+ + */ + } + + private static void log(ByteBuf buffer) { + int length = buffer.readableBytes(); + int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4; + StringBuilder buf = new StringBuilder(rows * 80 * 2) + .append("read index:").append(buffer.readerIndex()) + .append(" write index:").append(buffer.writerIndex()) + .append(" capacity:").append(buffer.capacity()) + .append(NEWLINE); + appendPrettyHexDump(buf, buffer); + System.out.println(buf.toString()); + } +} diff --git a/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_02.java b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_02.java new file mode 100644 index 0000000000000000000000000000000000000000..102390d248e6aa5cd74d52c26557d52c81099b2d --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_02.java @@ -0,0 +1,38 @@ +package com.kwan.shuyu.heima.netty_07_bytebuf; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; + +import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump; +import static io.netty.util.internal.StringUtil.NEWLINE; + +/** + * ByteBuf 创建 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/27 09:34 + */ +public class ByteBuf_02 { + + public static void main(String[] args) { + ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10); + log(buffer); + //上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10 + /** + * read index:0 write index:0 capacity:10 + */ + } + + private static void log(ByteBuf buffer) { + int length = buffer.readableBytes(); + int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4; + StringBuilder buf = new StringBuilder(rows * 80 * 2) + .append("read index:").append(buffer.readerIndex()) + .append(" write index:").append(buffer.writerIndex()) + .append(" capacity:").append(buffer.capacity()) + .append(NEWLINE); + appendPrettyHexDump(buf, buffer); + System.out.println(buf.toString()); + } +} diff --git a/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_03.java b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_03.java new file mode 100644 index 0000000000000000000000000000000000000000..703cfa8215b9685a06082fc16f6bf53ef7dd02b2 --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_03.java @@ -0,0 +1,48 @@ +package com.kwan.shuyu.heima.netty_07_bytebuf; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import lombok.extern.slf4j.Slf4j; + +import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump; +import static io.netty.util.internal.StringUtil.NEWLINE; + +/** + * ByteBuf 的类型打印 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/27 09:34 + */ +@Slf4j +public class ByteBuf_03 { + + public static void main(String[] args) { + ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10); + ByteBuf buffer1 = ByteBufAllocator.DEFAULT.heapBuffer(10); + ByteBuf buffer2 = ByteBufAllocator.DEFAULT.directBuffer(10); + log.info(buffer.getClass().getSimpleName()); + log.info(buffer1.getClass().getSimpleName()); + log.info(buffer2.getClass().getSimpleName()); + log(buffer); + //上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10 + /** + [main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeDirectByteBuf + [main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeHeapByteBuf + [main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeDirectByteBuf + read index:0 write index:0 capacity:10 + */ + } + + private static void log(ByteBuf buffer) { + int length = buffer.readableBytes(); + int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4; + StringBuilder buf = new StringBuilder(rows * 80 * 2) + .append("read index:").append(buffer.readerIndex()) + .append(" write index:").append(buffer.writerIndex()) + .append(" capacity:").append(buffer.capacity()) + .append(NEWLINE); + appendPrettyHexDump(buf, buffer); + System.out.println(buf.toString()); + } +} diff --git a/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_04.java b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_04.java new file mode 100644 index 0000000000000000000000000000000000000000..ebc265f0c1d19b5cf3a0071109563697b186a7da --- /dev/null +++ b/src/main/java/com/kwan/shuyu/heima/netty_07_bytebuf/ByteBuf_04.java @@ -0,0 +1,66 @@ +package com.kwan.shuyu.heima.netty_07_bytebuf; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import lombok.extern.slf4j.Slf4j; + +import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump; +import static io.netty.util.internal.StringUtil.NEWLINE; + +/** + * ByteBuf 数据写入 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/27 09:34 + */ +@Slf4j +public class ByteBuf_04 { + + public static void main(String[] args) { + ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10); + buffer.writeBytes(new byte[]{1, 2, 3, 4}); + log(buffer); + //上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10 + /** + * read index:0 write index:4 capacity:10 + * +-------------------------------------------------+ + * | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + * +--------+-------------------------------------------------+----------------+ + * |00000000| 01 02 03 04 |.... | + * +--------+-------------------------------------------------+----------------+ + */ + buffer.writeInt(5);//int占四个字节 + log(buffer); + /** + * read index:0 write index:8 capacity:10 + * +-------------------------------------------------+ + * | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + * +--------+-------------------------------------------------+----------------+ + * |00000000| 01 02 03 04 00 00 00 05 |........ | + * +--------+-------------------------------------------------+----------------+ + */ + buffer.setInt(0, 5);//int占四个字节,不会改变写指针,会出现数据覆盖 + log(buffer); + /** + * read index:0 write index:8 capacity:10 + * +-------------------------------------------------+ + * | 0 1 2 3 4 5 6 7 8 9 a b c d e f | + * +--------+-------------------------------------------------+----------------+ + * |00000000| 00 00 00 05 00 00 00 05 |........ | + * +--------+-------------------------------------------------+----------------+ + */ + } + + private static void log(ByteBuf buffer) { + int length = buffer.readableBytes(); + int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4; + StringBuilder buf = new StringBuilder(rows * 80 * 2) + .append("read index:").append(buffer.readerIndex()) + .append(" write index:").append(buffer.writerIndex()) + .append(" capacity:").append(buffer.capacity()) + .append(NEWLINE); + appendPrettyHexDump(buf, buffer); + System.out.println(buf.toString()); + } +} diff --git a/src/main/java/com/kwan/shuyu/until/ByteBufferUtil.java b/src/main/java/com/kwan/shuyu/until/ByteBufferUtil.java index 25b00a0ca38bb9aa3e9b689c5c5f816d22d717cc..33192fe7d29a096afd56d9922e3d3655f9ef3d0c 100644 --- a/src/main/java/com/kwan/shuyu/until/ByteBufferUtil.java +++ b/src/main/java/com/kwan/shuyu/until/ByteBufferUtil.java @@ -1,10 +1,12 @@ package com.kwan.shuyu.until; +import io.netty.buffer.ByteBuf; import io.netty.util.internal.StringUtil; import java.nio.ByteBuffer; import static io.netty.util.internal.MathUtil.isOutOfBounds; +import static io.netty.util.internal.StringUtil.NEWLINE; /** * ByteBuffer工具类 @@ -167,4 +169,6 @@ public class ByteBufferUtil { public static short getUnsignedByte(ByteBuffer buffer, int index) { return (short) (buffer.get(index) & 0xFF); } + + } \ No newline at end of file diff --git a/target/classes/com/kwan/shuyu/until/ByteBufferUtil.class b/target/classes/com/kwan/shuyu/until/ByteBufferUtil.class index 0933393e770b52c7d1b9fac920b16d4bb511b4a4..d3d285f58e9fcf7dc9bb8bebae727050e60bc8aa 100644 Binary files a/target/classes/com/kwan/shuyu/until/ByteBufferUtil.class and b/target/classes/com/kwan/shuyu/until/ByteBufferUtil.class differ