io.netty.channel.socket.oio.OioServerSocketChannel Java Examples

The following examples show how to use io.netty.channel.socket.oio.OioServerSocketChannel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SocketTestPermutation.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
public List<BootstrapFactory<ServerBootstrap>> serverSocket() {
    return Arrays.asList(
            new BootstrapFactory<ServerBootstrap>() {
                @Override
                public ServerBootstrap newInstance() {
                    return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
                            .channel(NioServerSocketChannel.class);
                }
            },
            new BootstrapFactory<ServerBootstrap>() {
                @Override
                public ServerBootstrap newInstance() {
                    return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup)
                            .channel(OioServerSocketChannel.class)
                            .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
                }
            }
    );
}
 
Example #2
Source File: OioEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTooManyAcceptedChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap sb = new ServerBootstrap();
    sb.channel(OioServerSocketChannel.class);
    sb.group(g);
    sb.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = sb.bind(0);
    f1.sync();

    Socket s = new Socket(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
    assertThat(s.getInputStream().read(), is(-1));
    s.close();

    g.shutdownGracefully();
}
 
Example #3
Source File: NettyOioServer.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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 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 {
						ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
							@Override
							public void channelActive(ChannelHandlerContext ctx) throws Exception {
								ctx.writeAndFlush(buf.duplicate()).addListener(ChannelFutureListener.CLOSE);
							}
						});
					}
				});
		ChannelFuture f = b.bind().sync();
		f.channel().closeFuture().sync();
	} finally {
		group.shutdownGracefully().sync();
	}
}
 
Example #4
Source File: SocketTestPermutation.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
public List<BootstrapFactory<ServerBootstrap>> serverSocket() {
    return Arrays.asList(
            new BootstrapFactory<ServerBootstrap>() {
                @Override
                public ServerBootstrap newInstance() {
                    return new ServerBootstrap().group(nioBossGroup, nioWorkerGroup)
                            .channel(NioServerSocketChannel.class);
                }
            },
            new BootstrapFactory<ServerBootstrap>() {
                @Override
                public ServerBootstrap newInstance() {
                    return new ServerBootstrap().group(oioBossGroup, oioWorkerGroup)
                            .channel(OioServerSocketChannel.class)
                            .option(ChannelOption.SO_TIMEOUT, OIO_SO_TIMEOUT);
                }
            }
    );
}
 
Example #5
Source File: OioEventLoopTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testTooManyAcceptedChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap sb = new ServerBootstrap();
    sb.channel(OioServerSocketChannel.class);
    sb.group(g);
    sb.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = sb.bind(0);
    f1.sync();

    Socket s = new Socket(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
    assertThat(s.getInputStream().read(), is(-1));
    s.close();

    g.shutdownGracefully();
}
 
Example #6
Source File: BlockingEchoServer.java    From netty.book.kor with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new OioEventLoopGroup(1);
    EventLoopGroup workerGroup = new OioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
        .channel(OioServerSocketChannel.class)
        .childHandler(new ChannelInitializer<SocketChannel>() {
           @Override
           public void initChannel(SocketChannel ch) {
               ChannelPipeline p = ch.pipeline();
               p.addLast(new EchoServerHandler());
           }
        });

        ChannelFuture f = b.bind(8888).sync();

        f.channel().closeFuture().sync();
    }
    finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}
 
Example #7
Source File: OioEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyServerChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap b = new ServerBootstrap();
    b.channel(OioServerSocketChannel.class);
    b.group(g);
    b.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = b.bind(0);
    f1.sync();

    ChannelFuture f2 = b.bind(0);
    f2.await();

    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));

    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });

    notified.await();
    g.shutdownGracefully();
}
 
Example #8
Source File: OioEventLoopTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyClientChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap sb = new ServerBootstrap();
    sb.channel(OioServerSocketChannel.class);
    sb.group(g);
    sb.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = sb.bind(0);
    f1.sync();

    Bootstrap cb = new Bootstrap();
    cb.channel(OioSocketChannel.class);
    cb.group(g);
    cb.handler(new ChannelInboundHandlerAdapter());
    ChannelFuture f2 = cb.connect(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
    f2.await();

    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));

    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });

    notified.await();
    g.shutdownGracefully();
}
 
Example #9
Source File: OioEventLoopTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyServerChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap b = new ServerBootstrap();
    b.channel(OioServerSocketChannel.class);
    b.group(g);
    b.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = b.bind(0);
    f1.sync();

    ChannelFuture f2 = b.bind(0);
    f2.await();

    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));

    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });

    notified.await();
    g.shutdownGracefully();
}
 
Example #10
Source File: OioEventLoopTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooManyClientChannels() throws Exception {
    EventLoopGroup g = new OioEventLoopGroup(1);
    ServerBootstrap sb = new ServerBootstrap();
    sb.channel(OioServerSocketChannel.class);
    sb.group(g);
    sb.childHandler(new ChannelInboundHandlerAdapter());
    ChannelFuture f1 = sb.bind(0);
    f1.sync();

    Bootstrap cb = new Bootstrap();
    cb.channel(OioSocketChannel.class);
    cb.group(g);
    cb.handler(new ChannelInboundHandlerAdapter());
    ChannelFuture f2 = cb.connect(NetUtil.LOCALHOST, ((InetSocketAddress) f1.channel().localAddress()).getPort());
    f2.await();

    assertThat(f2.cause(), is(instanceOf(ChannelException.class)));
    assertThat(f2.cause().getMessage().toLowerCase(), containsString("too many channels"));

    final CountDownLatch notified = new CountDownLatch(1);
    f2.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            notified.countDown();
        }
    });

    notified.await();
    g.shutdownGracefully();
}
 
Example #11
Source File: NettyOioServer.java    From code with Apache License 2.0 4 votes vote down vote up
public void server(int port)
        throws Exception {
    final ByteBuf buf =
            Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("Hi!\r\n", StandardCharsets.UTF_8));
    EventLoopGroup group = new OioEventLoopGroup();
    try {
        //创建 ServerBootstrap
        ServerBootstrap b = new ServerBootstrap();
        b.group(group)
                //使用 OioEventLoopGroup以允许阻塞模式(旧的I/O)
                .channel(OioServerSocketChannel.class)
                .localAddress(new InetSocketAddress(port))
                //指定 ChannelInitializer,对于每个已接受的连接都调用它
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch)
                            throws Exception {
                        ch.pipeline().addLast(
                                //添加一个 ChannelInboundHandlerAdapter以拦截和处理事件
                                new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelActive(
                                            ChannelHandlerContext ctx)
                                            throws Exception {
                                        ctx.writeAndFlush(buf.duplicate())
                                                .addListener(
                                                        //将消息写到客户端,并添加 ChannelFutureListener,
                                                        //以便消息一被写完就关闭连接
                                                        ChannelFutureListener.CLOSE);
                                    }
                                });
                    }
                });
        //绑定服务器以接受连接
        ChannelFuture f = b.bind().sync();
        f.channel().closeFuture().sync();
    } finally {
        //释放所有的资源
        group.shutdownGracefully().sync();
    }
}
 
Example #12
Source File: BaseServer.java    From ext-opensource-netty with Mozilla Public License 2.0 4 votes vote down vote up
public void bind() {
	initBind();
	initGroup();
	try {
		serverBootstrap.option(ChannelOption.SO_KEEPALIVE, keepAlive);
		serverBootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
		serverBootstrap.option(ChannelOption.SO_BACKLOG, soBacklog);
		serverBootstrap.group(bossGroup, workerGroup);
		if (SocketModel.BLOCK.equals(socketModel)) {
			serverBootstrap.channel(OioServerSocketChannel.class);
		} else {
			serverBootstrap.channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class);
		}
		serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
			@Override
			protected void initChannel(SocketChannel ch) throws Exception {
				if (sslCtx != null) {
			        SSLEngine sslEngine = sslCtx.newEngine(ch.alloc());
			        sslEngine.setUseClientMode(false);
			        sslEngine.setNeedClientAuth(sslClientAuth);
			        ch.pipeline().addFirst(NettyConstant.HANDLER_NAME_SSL, new SslHandler(sslEngine));  
				}
				if (self().isCheckHeartbeat()) {
					NettyLog.info("checkHeartBeat.....");
					ch.pipeline().addLast(
							new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds));
					ch.pipeline().addLast(NettyConstant.HANDLER_NAME_HEARTCHECK, new HeartbeatServerHandler());
				}

				ch.pipeline().addLast(new ChannelManagerHandler(self().getClientManager()));

				initSocketChannel(ch);
			}
		});

		ChannelFuture future = serverBootstrap.bind(this.getHost(), this.getPort());
		channel = future.channel();

		future.sync();

	} catch (Exception ex) {
		NettyLog.error("Netty start error:", ex);
		throw new SocketRuntimeException(ex);
	} finally {
		if (channel != null && channel.isActive()) {
			NettyLog.info("Netty server listening " + this.getHost() + " on port " + this.getPort()
					+ " and ready for connections...");
		} else {
			NettyLog.error("Netty server start up Error!");
		}
	}
}
 
Example #13
Source File: RpcServer.java    From TakinRPC with Apache License 2.0 4 votes vote down vote up
public RpcServer(OioEventLoopGroup eventLoopGroup, EventExecutorGroup eventExecutor, @Assisted SocketAddress address) {
    this(eventLoopGroup, eventExecutor, OioServerSocketChannel.class, address);
}
 
Example #14
Source File: NettyKt.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static ServerBootstrap serverBootstrap(EventLoopGroup group) {
  ServerBootstrap bootstrap =
          new ServerBootstrap().group(group).channel(group instanceof NioEventLoopGroup ? NioServerSocketChannel.class : OioServerSocketChannel.class);
  bootstrap.childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true);
  return bootstrap;
}