package com.yw.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.junit.Test;
import java.net.InetSocketAddress;
/**
* @author x y
* @description 最最最入门的netty
* @date 2022-03-15 11:01
*/
public class MyNetty {
@Test
public void byteBuf() {
// ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.directBuffer(8, 20);
ByteBuf byteBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(8, 20);
print(byteBuf);
byteBuf.writeBytes("abcd".getBytes());
print(byteBuf);
byteBuf.writeBytes("abcd".getBytes());
print(byteBuf);
byteBuf.writeBytes("abcd".getBytes());
print(byteBuf);
byteBuf.writeBytes("abcd".getBytes());
print(byteBuf);
byteBuf.writeBytes("abcd".getBytes());
print(byteBuf);
byteBuf.writeBytes("abcd".getBytes());
print(byteBuf);
}
private void print(ByteBuf byteBuf) {
System.err.println("byteBuf.isReadable() " + byteBuf.isReadable());
System.err.println("byteBuf.readerIndex() " + byteBuf.readerIndex());
System.err.println("byteBuf.readableBytes() " + byteBuf.readableBytes());
System.err.println("byteBuf.isWritable() " + byteBuf.isWritable());
System.err.println("byteBuf.writerIndex() " + byteBuf.writerIndex());
System.err.println("byteBuf.writableBytes() " + byteBuf.writableBytes());
System.err.println("是否队外内存" + byteBuf.isDirect());
System.err.println("......................................................");
}
@Test
public void server() throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(1);
NioServerSocketChannel server = new NioServerSocketChannel();
group.register(server);//epoll_create()
ChannelFuture bind = server.bind(new InetSocketAddress(9009));
ChannelPipeline pipeline = server.pipeline();
pipeline.addLast(new MyAccept(group, new MyAllHandler()));
bind.channel().closeFuture().sync();
}
@Test
public void client() throws InterruptedException {
NioEventLoopGroup executors = new NioEventLoopGroup(1);
NioSocketChannel client = new NioSocketChannel();
executors.register(client); // 相当于 epoll_ctl(3,add,4)
ChannelFuture connect = client.connect(new InetSocketAddress("192.168.2.243", 9009));
ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.directBuffer(8, 20);
// 直接写
// 什么时候读?
byteBuf.writeBytes("123456".getBytes());
client.writeAndFlush(byteBuf);
connect.sync().channel().closeFuture().sync(); //当通道关闭了,就继续往下走
}
@Test
public void client1() throws InterruptedException {
NioEventLoopGroup executors = new NioEventLoopGroup(1);
NioSocketChannel client = new NioSocketChannel();
executors.register(client); // 相当于 epoll_ctl(3,add,4)
ChannelFuture connect = client.connect(new InetSocketAddress("192.168.2.243", 9009));
ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.directBuffer(8, 20);
// 直接写
// 什么时候读?
byteBuf.writeBytes("123456".getBytes());
client.writeAndFlush(byteBuf);
connect.sync().channel().closeFuture().sync(); //当通道关闭了,就继续往下走
}
@ChannelHandler.Sharable
public class MyAllHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.err.println("all handler register ....");
NioSocketChannel client = (NioSocketChannel) ctx.channel();
ChannelPipeline pipeline = client.pipeline();
pipeline.addLast(new MyInHandler());
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.err.println("all handler channelUnregistered ....");
NioSocketChannel client = (NioSocketChannel) ctx.channel();
ChannelPipeline pipeline = client.pipeline();
pipeline.addLast(new MyInHandler());
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("all handler channelInactive ....");
NioSocketChannel client = (NioSocketChannel) ctx.channel();
ChannelPipeline pipeline = client.pipeline();
pipeline.addLast(new MyInHandler());
super.channelInactive(ctx);
}
}
public class MyAccept extends ChannelInboundHandlerAdapter {
private final EventLoopGroup group;
private final ChannelHandler handler;
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.err.println("register .......");
super.channelRegistered(ctx);
}
public MyAccept(EventLoopGroup group, ChannelHandler myInHandler) {
this.group = group;
this.handler = myInHandler;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
NioSocketChannel client = (NioSocketChannel) msg;
ChannelPipeline pipeline = client.pipeline();
pipeline.addLast(handler);
group.register(client);
// super.channelRead(ctx, msg);
}
}
public class MyInHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.err.println("register .......");
super.channelRegistered(ctx);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.err.println("active .....");
super.channelActive(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
System.err.println(buf.toString());
// ctx.writeAndFlush(buf);
// super.channelRead(ctx, msg);
}
}
}
最最最最入门的netty
最新推荐文章于 2024-07-15 22:27:18 发布