Search in sources :

Example 91 with SocketChannel

use of io.netty.channel.socket.SocketChannel in project jdepth by Crab2died.

the class EchoClient method connect.

private void connect(String host, int port) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)).addLast(new StringDecoder()).addLast(new EchoClientHandler());
            }
        });
        ChannelFuture future = bootstrap.connect(host, port).sync();
        future.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) Bootstrap(io.netty.bootstrap.Bootstrap) StringDecoder(io.netty.handler.codec.string.StringDecoder) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 92 with SocketChannel

use of io.netty.channel.socket.SocketChannel in project jdepth by Crab2died.

the class EchoServer method bind.

private void bind(int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)).addLast(new StringDecoder()).addLast(new EchoServerHandler());
            }
        });
        ChannelFuture future = bootstrap.bind(port).sync();
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 93 with SocketChannel

use of io.netty.channel.socket.SocketChannel in project jdepth by Crab2died.

the class EchoClient method connect.

private void connect(String host, int port) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(// 定长设置
                new FixedLengthFrameDecoder(15)).addLast(new StringDecoder()).addLast(new EchoClientHandler());
            }
        });
        ChannelFuture future = bootstrap.connect(host, port).sync();
        future.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) FixedLengthFrameDecoder(io.netty.handler.codec.FixedLengthFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 94 with SocketChannel

use of io.netty.channel.socket.SocketChannel in project x-pipe by ctripcorp.

the class NettyKeyedPoolClientFactory method doStart.

@Override
protected void doStart() throws Exception {
    eventLoopGroup = new NioEventLoopGroup(eventLoopThreads, XpipeThreadFactory.create("NettyKeyedPoolClientFactory"));
    b.group(eventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new LoggingHandler());
            p.addLast(new NettySimpleMessageHandler());
            p.addLast(new NettyClientHandler());
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NettySimpleMessageHandler(com.ctrip.xpipe.netty.NettySimpleMessageHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 95 with SocketChannel

use of io.netty.channel.socket.SocketChannel in project x-pipe by ctripcorp.

the class PsyncLatencyTest method startGetLatency.

private void startGetLatency() {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new LoggingHandler(LogLevel.DEBUG));
            p.addLast(new NettySimpleMessageHandler());
            p.addLast(new ReceiveMessageHandler(runId, offset));
        }
    });
    b.connect(dest);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NettySimpleMessageHandler(com.ctrip.xpipe.netty.NettySimpleMessageHandler) ReceiveMessageHandler(com.ctrip.xpipe.redis.keeper.simple.latency.netty.ReceiveMessageHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

SocketChannel (io.netty.channel.socket.SocketChannel)235 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)137 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)101 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)94 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)94 Bootstrap (io.netty.bootstrap.Bootstrap)89 ChannelPipeline (io.netty.channel.ChannelPipeline)79 EventLoopGroup (io.netty.channel.EventLoopGroup)79 ChannelFuture (io.netty.channel.ChannelFuture)77 InetSocketAddress (java.net.InetSocketAddress)43 LoggingHandler (io.netty.handler.logging.LoggingHandler)42 Channel (io.netty.channel.Channel)35 IOException (java.io.IOException)33 SslContext (io.netty.handler.ssl.SslContext)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)29 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)29 ByteBuf (io.netty.buffer.ByteBuf)27 StringDecoder (io.netty.handler.codec.string.StringDecoder)23 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)18 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15