Search in sources :

Example 31 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project netty by netty.

the class EchoClient method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.git
    final SslContext sslCtx;
    if (SSL) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                }
                //p.addLast(new LoggingHandler(LogLevel.INFO));
                p.addLast(new EchoClientHandler());
            }
        });
        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline) SslContext(io.netty.handler.ssl.SslContext)

Example 32 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project netty by netty.

the class EchoServer method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc()));
                }
                //p.addLast(new LoggingHandler(LogLevel.INFO));
                p.addLast(new EchoServerHandler());
            }
        });
        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 33 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project netty by netty.

the class AbstractOioMessageChannel method doRead.

@Override
protected void doRead() {
    if (!readPending) {
        // during the same read loop readPending was set to false.
        return;
    }
    // In OIO we should set readPending to false even if the read was not successful so we can schedule
    // another read on the event loop if no reads are done.
    readPending = false;
    final ChannelConfig config = config();
    final ChannelPipeline pipeline = pipeline();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.reset(config);
    boolean closed = false;
    Throwable exception = null;
    try {
        do {
            // Perform a read.
            int localRead = doReadMessages(readBuf);
            if (localRead == 0) {
                break;
            }
            if (localRead < 0) {
                closed = true;
                break;
            }
            allocHandle.incMessagesRead(localRead);
        } while (allocHandle.continueReading());
    } catch (Throwable t) {
        exception = t;
    }
    boolean readData = false;
    int size = readBuf.size();
    if (size > 0) {
        readData = true;
        for (int i = 0; i < size; i++) {
            readPending = false;
            pipeline.fireChannelRead(readBuf.get(i));
        }
        readBuf.clear();
        allocHandle.readComplete();
        pipeline.fireChannelReadComplete();
    }
    if (exception != null) {
        if (exception instanceof IOException) {
            closed = true;
        }
        pipeline.fireExceptionCaught(exception);
    }
    if (closed) {
        if (isOpen()) {
            unsafe().close(unsafe().voidPromise());
        }
    } else if (readPending || config.isAutoRead() || !readData && isActive()) {
        // Reading 0 bytes could mean there is a SocketTimeout and no data was actually read, so we
        // should execute read() again because no data may have been read.
        read();
    }
}
Also used : RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ChannelConfig(io.netty.channel.ChannelConfig) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 34 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project netty by netty.

the class LocalChannel method doBeginRead.

@Override
protected void doBeginRead() throws Exception {
    if (readInProgress) {
        return;
    }
    ChannelPipeline pipeline = pipeline();
    Queue<Object> inboundBuffer = this.inboundBuffer;
    if (inboundBuffer.isEmpty()) {
        readInProgress = true;
        return;
    }
    final InternalThreadLocalMap threadLocals = InternalThreadLocalMap.get();
    final Integer stackDepth = threadLocals.localChannelReaderStackDepth();
    if (stackDepth < MAX_READER_STACK_DEPTH) {
        threadLocals.setLocalChannelReaderStackDepth(stackDepth + 1);
        try {
            for (; ; ) {
                Object received = inboundBuffer.poll();
                if (received == null) {
                    break;
                }
                pipeline.fireChannelRead(received);
            }
            pipeline.fireChannelReadComplete();
        } finally {
            threadLocals.setLocalChannelReaderStackDepth(stackDepth);
        }
    } else {
        try {
            eventLoop().execute(readTask);
        } catch (Throwable cause) {
            logger.warn("Closing Local channels {}-{} because exception occurred!", this, peer, cause);
            close();
            peer.close();
            PlatformDependent.throwException(cause);
        }
    }
}
Also used : InternalThreadLocalMap(io.netty.util.internal.InternalThreadLocalMap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 35 with ChannelPipeline

use of io.netty.channel.ChannelPipeline in project netty by netty.

the class LocalServerChannel method doBeginRead.

@Override
protected void doBeginRead() throws Exception {
    if (acceptInProgress) {
        return;
    }
    Queue<Object> inboundBuffer = this.inboundBuffer;
    if (inboundBuffer.isEmpty()) {
        acceptInProgress = true;
        return;
    }
    ChannelPipeline pipeline = pipeline();
    for (; ; ) {
        Object m = inboundBuffer.poll();
        if (m == null) {
            break;
        }
        pipeline.fireChannelRead(m);
    }
    pipeline.fireChannelReadComplete();
}
Also used : ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

ChannelPipeline (io.netty.channel.ChannelPipeline)161 SocketChannel (io.netty.channel.socket.SocketChannel)42 Channel (io.netty.channel.Channel)40 Bootstrap (io.netty.bootstrap.Bootstrap)38 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)38 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)36 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ChannelFuture (io.netty.channel.ChannelFuture)33 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)28 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)28 EventLoopGroup (io.netty.channel.EventLoopGroup)25 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)25 SslHandler (io.netty.handler.ssl.SslHandler)21 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)18 InetSocketAddress (java.net.InetSocketAddress)17 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)16 LoggingHandler (io.netty.handler.logging.LoggingHandler)16 ChannelHandler (io.netty.channel.ChannelHandler)15 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)15 IOException (java.io.IOException)15