最最最最入门的netty

2 篇文章 0 订阅
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);
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值