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