Search in sources :

Example 86 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project angel by Tencent.

the class MatrixTransportServer method start.

public void start() {
    Configuration conf = context.getConf();
    int workerNum = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_EVENTGROUP_THREADNUM, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_EVENTGROUP_THREADNUM);
    int sendBuffSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_SNDBUF, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_SNDBUF);
    int recvBuffSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_RCVBUF, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_RCVBUF);
    final int maxMessageSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE);
    int ioRatio = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_IORATIO, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_IORATIO);
    String channelType = conf.get(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_CHANNEL_TYPE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_CHANNEL_TYPE);
    // Use Epoll for linux
    Class channelClass;
    String os = System.getProperty("os.name");
    if (os != null && os.toLowerCase().startsWith("linux") && channelType.equals("epoll")) {
        LOG.info("Use epoll channel");
        channelClass = EpollServerSocketChannel.class;
        bossGroup = new EpollEventLoopGroup(1);
        workerGroup = new EpollEventLoopGroup(workerNum);
        ((EpollEventLoopGroup) workerGroup).setIoRatio(ioRatio);
    } else {
        LOG.info("Use nio channel");
        channelClass = NioServerSocketChannel.class;
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup(workerNum);
        ((NioEventLoopGroup) workerGroup).setIoRatio(70);
    }
    LOG.info("Server port = " + port);
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(channelClass).option(ChannelOption.SO_SNDBUF, sendBuffSize).option(ChannelOption.SO_RCVBUF, recvBuffSize).option(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new LengthFieldBasedFrameDecoder(maxMessageSize, 0, 4, 0, 4));
            p.addLast(new LengthFieldPrepender(4));
            p.addLast(new MatrixTransportServerHandler(context));
        }
    });
    channelFuture = b.bind(port);
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Configuration(org.apache.hadoop.conf.Configuration) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 87 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project rskj by rsksmart.

the class PeerServerImpl method start.

private void start(InetAddress host, int port) {
    // TODO review listening use
    listening = true;
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance("");
    ethereumListener.trace("Listening on port " + port);
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
        b.handler(new LoggingHandler());
        b.childHandler(ethereumChannelInitializer);
        // Start the client.
        logger.info("Listening for incoming connections, host: {}, port: [{}] ", host, port);
        logger.info("NodeId: [{}] ", ByteUtil.toHexString(config.nodeId()));
        ChannelFuture f = b.bind(host, port).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
        logger.debug("Connection is closed");
        // TODO review listening use
        listening = false;
    } catch (Exception e) {
        logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
        throw new Error("Server Disconnected");
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 88 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project rskj by rsksmart.

the class Web3WebSocketServer method start.

@Override
public void start() {
    logger.info("RPC WebSocket enabled");
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpServerCodec());
            p.addLast(new HttpObjectAggregator(maxAggregatedFrameSize));
            p.addLast(new WriteTimeoutHandler(serverWriteTimeoutSeconds, TimeUnit.SECONDS));
            p.addLast(new RskWebSocketServerProtocolHandler("/websocket", maxFrameSize));
            p.addLast(new WebSocketFrameAggregator(maxAggregatedFrameSize));
            p.addLast(webSocketJsonRpcHandler);
            p.addLast(web3ServerHandler);
            p.addLast(new Web3ResultWebSocketResponseHandler());
        }
    });
    webSocketChannel = b.bind(host, port);
    try {
        webSocketChannel.sync();
    } catch (InterruptedException e) {
        logger.error("The RPC WebSocket server couldn't be started", e);
        Thread.currentThread().interrupt();
    }
}
Also used : WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) WriteTimeoutHandler(io.netty.handler.timeout.WriteTimeoutHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec)

Example 89 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project rskj by rsksmart.

the class Web3HttpServer method start.

@Override
public void start() {
    logger.info("RPC HTTP enabled");
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_LINGER, socketLinger);
    b.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new HttpRequestDecoder());
            p.addLast(new HttpResponseEncoder());
            p.addLast(new HttpObjectAggregator(1024 * 1024 * 5));
            p.addLast(new HttpContentCompressor());
            if (corsConfiguration.hasHeader()) {
                p.addLast(new CorsHandler(CorsConfig.withOrigin(corsConfiguration.getHeader()).allowedRequestHeaders(HttpHeaders.Names.CONTENT_TYPE).allowedRequestMethods(HttpMethod.POST).build()));
            }
            p.addLast(jsonRpcWeb3FilterHandler);
            p.addLast(new Web3HttpMethodFilterHandler());
            p.addLast(jsonRpcWeb3ServerHandler);
            p.addLast(buildWeb3ResultHttpResponseHandler());
        }
    });
    try {
        b.bind(bindAddress, port).sync();
    } catch (InterruptedException e) {
        logger.error("The RPC HTTP server couldn't be started", e);
        Thread.currentThread().interrupt();
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) CorsHandler(io.netty.handler.codec.http.cors.CorsHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 90 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project swift by luastar.

the class HttpServer method start.

public void start() {
    // 设置线程名称
    ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
    EventLoopGroup bossGroup = new NioEventLoopGroup(HttpConstant.SWIFT_BOSS_THREADS, threadFactoryBuilder.setNameFormat("boss-group-%d").build());
    EventLoopGroup workerGroup = new NioEventLoopGroup(HttpConstant.SWIFT_WORKER_THREADS, threadFactoryBuilder.setNameFormat("worker-group-%d").build());
    // EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(HttpConstant.SWIFT_BUSINESS_THREADS, threadFactoryBuilder.setNameFormat("executor-group-%d").build());
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                if (ssl) {
                    SelfSignedCertificate ssc = new SelfSignedCertificate();
                    SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
                    pipeline.addLast(sslContext.newHandler(ch.alloc()));
                }
                // http request decode and response encode
                pipeline.addLast(new HttpServerCodec());
                // 将消息头和体聚合成FullHttpRequest和FullHttpResponse
                pipeline.addLast(new HttpObjectAggregator(HttpConstant.SWIFT_MAX_CONTENT_LENGTH));
                // 压缩处理
                pipeline.addLast(new HttpContentCompressor(HttpConstant.SWIFT_COMPRESSION_LEVEL));
                // 自定义http服务
                // pipeline.addLast(executorGroup, "http-handler", new HttpChannelHandler(handlerMapping));
                pipeline.addLast(new HttpChannelHandler(handlerMapping));
            }
        });
        Channel channel = bootstrap.bind(port).sync().channel();
        channel.closeFuture().sync();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        // executorGroup.shutdownGracefully();
        HttpThreadPoolExecutor.shutdownGracefully();
    }
}
Also used : 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) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)448 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)246 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)239 Channel (io.netty.channel.Channel)187 ChannelFuture (io.netty.channel.ChannelFuture)183 EventLoopGroup (io.netty.channel.EventLoopGroup)168 Bootstrap (io.netty.bootstrap.Bootstrap)134 SocketChannel (io.netty.channel.socket.SocketChannel)126 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)115 InetSocketAddress (java.net.InetSocketAddress)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)98 ChannelPipeline (io.netty.channel.ChannelPipeline)88 LoggingHandler (io.netty.handler.logging.LoggingHandler)82 LocalServerChannel (io.netty.channel.local.LocalServerChannel)76 LocalChannel (io.netty.channel.local.LocalChannel)73 CountDownLatch (java.util.concurrent.CountDownLatch)66 Test (org.junit.jupiter.api.Test)66 LocalAddress (io.netty.channel.local.LocalAddress)61 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)60 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)57