Search in sources :

Example 6 with OioEventLoopGroup

use of io.netty.channel.oio.OioEventLoopGroup in project cradle by BingLau7.

the class NettyOioServer method server.

public void server(int port) throws Exception {
    final ByteBuf buf = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", Charset.forName("UTF-8")));
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        // 创建 ServerBootstrap
        ServerBootstrap b = new ServerBootstrap();
        b.group(group).channel(OioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                // 添加一个 ChannelInBoundHandlerAdapter 以拦截和处理事件
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) throws Exception {
                        // 将消息写到客户端,并添加 ChannelFutureListener 以便消息已被写完就关闭连接
                        ctx.writeAndFlush(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
                    }
                });
            }
        });
        // 绑定服务器以接收连接
        ChannelFuture f = b.bind().sync();
        f.channel().closeFuture().sync();
    } finally {
        // 释放所有资源
        group.shutdownGracefully().sync();
    }
}
Also used : OioServerSocketChannel(io.netty.channel.socket.oio.OioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(io.netty.buffer.ByteBuf) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 7 with OioEventLoopGroup

use of io.netty.channel.oio.OioEventLoopGroup in project intellij-community by JetBrains.

the class BuiltInServer method startNioOrOio.

@NotNull
public static BuiltInServer startNioOrOio(int workerCount, int firstPort, int portsCount, boolean tryAnyPort, @Nullable NotNullProducer<ChannelHandler> handler) throws Exception {
    BuiltInServerThreadFactory threadFactory = new BuiltInServerThreadFactory();
    NioEventLoopGroup nioEventLoopGroup;
    try {
        nioEventLoopGroup = new NioEventLoopGroup(workerCount, threadFactory);
    } catch (IllegalStateException e) {
        Logger.getInstance(BuiltInServer.class).warn(e);
        return start(new OioEventLoopGroup(1, threadFactory), true, 6942, 50, false, handler);
    }
    return start(nioEventLoopGroup, true, firstPort, portsCount, tryAnyPort, handler);
}
Also used : OioEventLoopGroup(io.netty.channel.oio.OioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

OioEventLoopGroup (io.netty.channel.oio.OioEventLoopGroup)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 OioServerSocketChannel (io.netty.channel.socket.oio.OioServerSocketChannel)3 InetSocketAddress (java.net.InetSocketAddress)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelInitializer (io.netty.channel.ChannelInitializer)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 BuiltInServer (org.jetbrains.io.BuiltInServer)2 ExecutionException (com.intellij.execution.ExecutionException)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 RxtxChannel (io.netty.channel.rxtx.RxtxChannel)1 RxtxDeviceAddress (io.netty.channel.rxtx.RxtxDeviceAddress)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)1 ProtobufDecoder (io.netty.handler.codec.protobuf.ProtobufDecoder)1