栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

最最最最入门的netty

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

最最最最入门的netty

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;


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);
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769557.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号