List of usage examples for io.netty.channel ChannelOption SO_KEEPALIVE
ChannelOption SO_KEEPALIVE
To view the source code for io.netty.channel ChannelOption SO_KEEPALIVE.
Click Source Link
From source file:org.jupiter.transport.netty.NettyTcpAcceptor.java
License:Apache License
@Override protected void setOptions() { super.setOptions(); ServerBootstrap boot = bootstrap();// w ww . java2 s. c o m // parent options NettyConfig.NettyTcpConfigGroup.ParentConfig parent = configGroup.parent(); boot.option(ChannelOption.SO_BACKLOG, parent.getBacklog()); boot.option(ChannelOption.SO_REUSEADDR, parent.isReuseAddress()); if (parent.getRcvBuf() > 0) { boot.option(ChannelOption.SO_RCVBUF, parent.getRcvBuf()); } // child options NettyConfig.NettyTcpConfigGroup.ChildConfig child = configGroup.child(); boot.childOption(ChannelOption.SO_REUSEADDR, child.isReuseAddress()) .childOption(ChannelOption.SO_KEEPALIVE, child.isKeepAlive()) .childOption(ChannelOption.TCP_NODELAY, child.isTcpNoDelay()) .childOption(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure()); if (child.getRcvBuf() > 0) { boot.childOption(ChannelOption.SO_RCVBUF, child.getRcvBuf()); } if (child.getSndBuf() > 0) { boot.childOption(ChannelOption.SO_SNDBUF, child.getSndBuf()); } if (child.getLinger() > 0) { boot.childOption(ChannelOption.SO_LINGER, child.getLinger()); } if (child.getIpTos() > 0) { boot.childOption(ChannelOption.IP_TOS, child.getIpTos()); } int bufLowWaterMark = child.getWriteBufferLowWaterMark(); int bufHighWaterMark = child.getWriteBufferHighWaterMark(); if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) { WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark); boot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark); } }
From source file:org.jupiter.transport.netty.NettyTcpConnector.java
License:Apache License
@Override protected void setOptions() { super.setOptions(); Bootstrap boot = bootstrap();//from w ww .j a v a 2 s.c om NettyConfig.NettyTcpConfigGroup.ChildConfig child = childConfig; // child options boot.option(ChannelOption.SO_REUSEADDR, child.isReuseAddress()) .option(ChannelOption.SO_KEEPALIVE, child.isKeepAlive()) .option(ChannelOption.TCP_NODELAY, child.isTcpNoDelay()) .option(ChannelOption.ALLOW_HALF_CLOSURE, child.isAllowHalfClosure()); if (child.getRcvBuf() > 0) { boot.option(ChannelOption.SO_RCVBUF, child.getRcvBuf()); } if (child.getSndBuf() > 0) { boot.option(ChannelOption.SO_SNDBUF, child.getSndBuf()); } if (child.getLinger() > 0) { boot.option(ChannelOption.SO_LINGER, child.getLinger()); } if (child.getIpTos() > 0) { boot.option(ChannelOption.IP_TOS, child.getIpTos()); } if (child.getConnectTimeoutMillis() > 0) { boot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, child.getConnectTimeoutMillis()); } int bufLowWaterMark = child.getWriteBufferLowWaterMark(); int bufHighWaterMark = child.getWriteBufferHighWaterMark(); if (bufLowWaterMark >= 0 && bufHighWaterMark > 0) { WriteBufferWaterMark waterMark = new WriteBufferWaterMark(bufLowWaterMark, bufHighWaterMark); boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, waterMark); } }
From source file:org.kobeyoung81.socksproxy.SocksServerConnectHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final SocksCmdRequest request) throws Exception { Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new GenericFutureListener<Future<Channel>>() { @Override/*w w w . j av a 2 s . co m*/ public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType())) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); //System.out.println(request.toString()); } else { ctx.channel() .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); //System.out.println(request.host() +":"+ request.port()); //SocksCmdRequest re = new SocksCmdRequest(request.cmdType(), request.addressType(), "192.168.87.103", 8080); b.connect(request.host(), request.port()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection established use handler provided results } else { // Close the connection if the connection attempt has failed. ctx.channel() .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); }
From source file:org.kordamp.javatrove.chat04.client.impl.ChatClientImpl.java
License:Open Source License
@Override public void login(String server, int port, String name) { group = new NioEventLoopGroup(); try {/*from w w w .ja va2s. c o m*/ Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true).handler(channelInitializer); channel = bootstrap.connect(server, port).sync().channel(); channel.writeAndFlush(loginCommand(name)); } catch (Exception e) { LOG.error("Unexpected error", e); terminate(); throw new IllegalStateException(e); } }
From source file:org.l2junity.gameserver.network.telnet.TelnetServer.java
License:Open Source License
public void init() { addHandler(new ITelnetCommand() { @Override/* w w w .ja v a 2 s .co m*/ public String getCommand() { return "help"; } @Override public String getUsage() { return "help [command]"; } @Override public String handle(ChannelHandlerContext ctx, String[] args) { if (args.length == 0) { final StringBuilder sb = new StringBuilder("Available commands:" + System.lineSeparator()); for (ITelnetCommand cmd : TelnetServer.getInstance().getCommands()) { sb.append(cmd.getCommand() + System.lineSeparator()); } return sb.toString(); } final ITelnetCommand cmd = TelnetServer.getInstance().getCommand(args[0]); if (cmd == null) { return "Unknown command." + System.lineSeparator(); } return "Usage:" + System.lineSeparator() + cmd.getUsage() + System.lineSeparator(); } }); try { final InetSocketAddress socket = Config.TELNET_HOSTNAME.equals("*") ? new InetSocketAddress(Config.TELNET_PORT) : new InetSocketAddress(Config.TELNET_HOSTNAME, Config.TELNET_PORT); //@formatter:off new ServerBootstrap().group(_workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true).childHandler(new TelnetServerInitializer()) .bind(socket); //@formatter:on LOGGER.info("Listening on " + Config.TELNET_HOSTNAME + ":" + Config.TELNET_PORT); } catch (Exception e) { LOGGER.warn(e.getMessage(), e); } }
From source file:org.lanternpowered.pingy.Pingy.java
License:MIT License
/** * Starts the pingy server.//from ww w. j a v a 2 s . c om * * @throws IOException */ public void start() throws IOException { boolean epoll = false; if (this.properties.isUseEpollWhenAvailable()) { if (Epoll.isAvailable()) { debugInfo("Epoll is available"); epoll = true; } else { debugWarn( "Epoll is unavailable (The following exception is only used to print the cause why it's unavailable, " + "it won't affect the functionality.)"); //noinspection ThrowableResultOfMethodCallIgnored debug(() -> Epoll.unavailabilityCause().printStackTrace()); } } final ServerBootstrap bootstrap = new ServerBootstrap(); final EventLoopGroup group = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup(); final ChannelFuture future = bootstrap.group(group) .channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ReadTimeoutHandler(20)) .addLast(new PingyLegacyHandler(properties)).addLast(new PingyFramingHandler()) .addLast(new PingyHandler(properties)); } }).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true) .bind(getBindAddress(this.properties.getIp(), this.properties.getPort())); final Channel channel = future.awaitUninterruptibly().channel(); if (!channel.isActive()) { final Throwable cause = future.cause(); if (cause instanceof BindException) { throw (BindException) cause; } throw new RuntimeException("Failed to bind to address", cause); } info("Successfully bound to: " + channel.localAddress()); }
From source file:org.lanternpowered.server.network.NetworkManager.java
License:MIT License
@Override protected ChannelFuture init0(SocketAddress address, boolean epoll) { this.bootstrap = new ServerBootstrap(); // Take advantage of the fast thread local threads, // this is also provided by the default thread factory final ThreadFactory threadFactory = ThreadHelper .newFastThreadLocalThreadFactory(() -> "netty-" + threadCounter.getAndIncrement()); this.bossGroup = createEventLoopGroup(epoll, threadFactory); this.workerGroup = createEventLoopGroup(epoll, threadFactory); this.socketAddress = address; return this.bootstrap.group(this.bossGroup, this.workerGroup).channel(getServerSocketChannelClass(epoll)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/* ww w . j a va 2 s .c o m*/ protected void initChannel(SocketChannel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); final NetworkSession networkSession = new NetworkSession(ch, server, NetworkManager.this); final CodecContext codecContext = new SimpleCodecContext( new LanternByteBufferAllocator(ch.alloc()), ch, networkSession); pipeline.addLast(new ReadTimeoutHandler(NetworkSession.READ_TIMEOUT_SECONDS)) .addLast(NetworkSession.LEGACY_PING, new LegacyProtocolHandler(networkSession)) .addLast(NetworkSession.ENCRYPTION, NoopHandler.INSTANCE) .addLast(NetworkSession.FRAMING, new MessageFramingHandler()) .addLast(NetworkSession.COMPRESSION, NoopHandler.INSTANCE) .addLast(NetworkSession.CODECS, new MessageCodecHandler(codecContext)) .addLast(NetworkSession.PROCESSOR, new MessageProcessorHandler(codecContext)) .addLast(NetworkSession.HANDLER, networkSession); } }).childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true) .bind(address); }
From source file:org.lightmare.remote.rpc.RPCall.java
License:Open Source License
/** * Prepares {@link Bootstrap} for RPC service client connection * //from w w w. j a va 2s . com * @return {@link Bootstrap} */ private Bootstrap getBootstrap() { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(worker); bootstrap.channel(NioSocketChannel.class); bootstrap.option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); if (timeout > ZERO_TIMEOUT) { bootstrap.option(ChannelOption.SO_TIMEOUT, timeout); } handler = new RcpHandler(); bootstrap.handler(new ChannelInitializerImpl(handler)); return bootstrap; }
From source file:org.lightmare.remote.rpc.RpcListener.java
License:Open Source License
/** * Starts RPC server/*from ww w .j a va2 s.co m*/ * */ public static void startServer(Configuration config) { setNettyPools(config); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss, worker).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializerImpl()); bootstrap.option(ChannelOption.SO_BACKLOG, 500); bootstrap.childOption(ChannelOption.SO_KEEPALIVE, Boolean.TRUE); bootstrap.childOption(ChannelOption.SO_TIMEOUT, config.getIntValue(ConfigKeys.CONNECTION_TIMEOUT.key)); InetSocketAddress address = new InetSocketAddress( Inet4Address.getByName(config.getStringValue("listening_ip")), config.getIntValue("listening_port")); ChannelFuture future = bootstrap.bind(address).sync(); LOG.info(future); } catch (UnknownHostException ex) { LOG.error(ex.getMessage(), ex); } catch (InterruptedException ex) { LOG.error(ex.getMessage(), ex); } }
From source file:org.maodian.flyingcat.xmpp.XmppServer.java
License:Apache License
private void run(ApplicationContext beanFactory) throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); try {/*from ww w . jav a 2s .c o m*/ b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) .localAddress(port).childHandler(new XmppServerInitializer(beanFactory)) .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true); b.bind().sync().channel().closeFuture().sync(); } finally { b.shutdown(); } }