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