Search in sources :

Example 76 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project Terasology by MovingBlocks.

the class NetworkSystemImpl method host.

@Override
public void host(int port, boolean dedicatedServer) throws HostingFailedException {
    if (mode == NetworkMode.NONE) {
        try {
            if (hibernationSettings.isPresent()) {
                hibernationSettings.get().setHibernationAllowed(false);
            }
            mode = dedicatedServer ? NetworkMode.DEDICATED_SERVER : NetworkMode.LISTEN_SERVER;
            for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
                registerNetworkEntity(entity);
            }
            generateSerializationTables();
            // Configure the server.
            bossGroup = new NioEventLoopGroup();
            workerGroup = new NioEventLoopGroup();
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).localAddress(port).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new TerasologyServerPipelineFactory(this));
            // Start the server.
            serverChannelFuture = b.bind();
            logger.info("Started server on port {}", port);
            if (config.getServerMOTD() != null) {
                logger.info("Server MOTD is \"{}\"", config.getServerMOTD());
            } else {
                logger.info("No server MOTD is defined");
            }
            if (serverChannelFuture.isSuccess()) {
                logger.info("Server started");
            }
            serverChannelFuture.sync();
            nextNetworkTick = time.getRealTimeInMs();
        } catch (ChannelException e) {
            if (e.getCause() instanceof BindException) {
                throw new HostingFailedException("Port already in use (are you already hosting a game?)", e.getCause());
            } else {
                throw new HostingFailedException("Failed to host game", e.getCause());
            }
        } catch (InterruptedException e) {
            shutdown();
            throw new HostingFailedException("Server has been interrupted", e.getCause());
        }
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) HostingFailedException(org.terasology.engine.network.exceptions.HostingFailedException) TerasologyServerPipelineFactory(org.terasology.engine.network.internal.pipelineFactory.TerasologyServerPipelineFactory) BindException(java.net.BindException) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelException(io.netty.channel.ChannelException)

Example 77 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project ambry by linkedin.

the class NettyServer method bindServer.

/**
 * Bootstrap a new server with a {@link ChannelInitializer} and bind it to a port.
 * @param port the port number to bind this server to.
 * @param channelInitializer the {@link ChannelInitializer} for request handling on this server.
 * @param bossGroup the pool of boss threads that this server uses.
 * @param workerGroup the pool of worker threads that this server uses.
 * @throws InterruptedException if binding to the port failed.
 */
private void bindServer(int port, ChannelInitializer<SocketChannel> channelInitializer, EventLoopGroup bossGroup, EventLoopGroup workerGroup) throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    Class<? extends ServerSocketChannel> channelClass = Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class;
    // Note: ServerSocketChannel option doesn't apply to SocketChannel
    b.group(bossGroup, workerGroup).channel(channelClass).option(ChannelOption.SO_BACKLOG, nettyConfig.nettyServerSoBacklog).handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(channelInitializer);
    b.bind(port).sync();
    logger.info("NettyServer now listening on port {}", port);
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 78 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project java by wavefrontHQ.

the class Server method listen.

public Server listen() throws InterruptedException {
    if (workGroup != null) {
        try {
            logger.debug("Shutting down existing worker group before starting");
            workGroup.shutdownGracefully().sync();
        } catch (Exception e) {
            logger.error("Could not shut down worker group before starting", e);
        }
    }
    workGroup = new NioEventLoopGroup();
    try {
        logger.info("Starting server on port: {}", this.port);
        beatsInitializer = new BeatsInitializer(isSslEnable(), messageListener, clientInactivityTimeoutSeconds, beatsHeandlerThreadCount);
        ServerBootstrap server = new ServerBootstrap();
        server.group(workGroup).channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_LINGER, // Since the protocol doesn't support yet a remote close from the server and we don't want to have 'unclosed' socket lying around we have to use `SO_LINGER` to force the close of the socket.
        0).childHandler(beatsInitializer);
        Channel channel = server.bind(host, port).sync().channel();
        channel.closeFuture().sync();
    } finally {
        shutdown();
    }
    return this;
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 79 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project summer by foxsugar.

the class SocketServer method start.

private void start() throws Exception {
    ServerConfig serverConfig = SpringUtil.getBean(ServerConfig.class);
    int port = serverConfig.getPort();
    final SslContext sslCtx;
    if (SSL) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } 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 SocketServerInitializer(sslCtx));
        // .childHandler(new WebSocketServerInitializer());
        // 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) ServerConfig(com.code.server.gate.config.ServerConfig) LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SslContext(io.netty.handler.ssl.SslContext)

Example 80 with ServerBootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project sofa-ark by alipay.

the class NettyTelnetServer method open.

public void open() throws InterruptedException {
    serverBootstrap = new ServerBootstrap();
    serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);
    serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new NettyTelnetInitializer());
    channel = serverBootstrap.bind(port).sync().channel();
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

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