List of usage examples for io.netty.channel ChannelOption TCP_NODELAY
ChannelOption TCP_NODELAY
To view the source code for io.netty.channel ChannelOption TCP_NODELAY.
Click Source Link
From source file:com.corundumstudio.socketio.SocketIOServer.java
License:Apache License
protected void applyConnectionOptions(ServerBootstrap bootstrap) { SocketConfig config = configCopy.getSocketConfig(); bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay()); if (config.getTcpSendBufferSize() != -1) { bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize()); }//from ww w .ja v a 2 s .c o m if (config.getTcpReceiveBufferSize() != -1) { bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getTcpReceiveBufferSize()); bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(config.getTcpReceiveBufferSize())); } bootstrap.childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive()); bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger()); bootstrap.option(ChannelOption.SO_REUSEADDR, config.isReuseAddress()); bootstrap.option(ChannelOption.SO_BACKLOG, config.getAcceptBackLog()); }
From source file:com.couchbase.client.core.endpoint.AbstractEndpoint.java
License:Apache License
/** * Create a new {@link AbstractEndpoint}. * * @param hostname the hostname/ipaddr of the remote channel. * @param bucket the name of the bucket. * @param password the password of the bucket. * @param port the port of the remote channel. * @param environment the environment of the core. * @param responseBuffer the response buffer for passing responses up the stack. */// w w w .j av a2 s . c o m protected AbstractEndpoint(final String hostname, final String bucket, final String password, final int port, final CoreEnvironment environment, final RingBuffer<ResponseEvent> responseBuffer, boolean isTransient) { super(LifecycleState.DISCONNECTED); this.bucket = bucket; this.password = password; this.responseBuffer = responseBuffer; this.env = environment; this.isTransient = isTransient; if (environment.sslEnabled()) { this.sslEngineFactory = new SSLEngineFactory(environment); } Class<? extends Channel> channelClass = NioSocketChannel.class; if (environment.ioPool() instanceof EpollEventLoopGroup) { channelClass = EpollSocketChannel.class; } else if (environment.ioPool() instanceof OioEventLoopGroup) { channelClass = OioSocketChannel.class; } ByteBufAllocator allocator = env.bufferPoolingEnabled() ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT; boolean tcpNodelay = environment().tcpNodelayEnabled(); bootstrap = new BootstrapAdapter( new Bootstrap().remoteAddress(hostname, port).group(environment.ioPool()).channel(channelClass) .option(ChannelOption.ALLOCATOR, allocator).option(ChannelOption.TCP_NODELAY, tcpNodelay) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, env.socketConnectTimeout()) .handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (environment.sslEnabled()) { pipeline.addLast(new SslHandler(sslEngineFactory.get())); } if (LOGGER.isTraceEnabled()) { pipeline.addLast(LOGGING_HANDLER_INSTANCE); } customEndpointHandlers(pipeline); } })); }
From source file:com.couchbase.client.core.io.endpoint.AbstractEndpoint.java
License:Open Source License
/** * Create a new {@link AbstractEndpoint} and supply essential params. * * @param addr the socket address to connect to. * @param env the environment to attach to. * @param group the {@link EventLoopGroup} to use. *///from w ww.jav a 2 s . c o m protected AbstractEndpoint(final InetSocketAddress addr, final Environment env, final EventLoopGroup group) { this.env = env; endpointStateDeferred = Streams.defer(env, defaultPromiseEnv); endpointStateStream = endpointStateDeferred.compose(); connectionBootstrap = new BootstrapAdapter(new Bootstrap().group(group).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(final SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (LOGGER.isTraceEnabled()) { pipeline.addLast(new LoggingHandler(LogLevel.TRACE)); } customEndpointHandlers(pipeline); pipeline.addLast(new GenericEndpointHandler<REQ, RES>()); } }).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.TCP_NODELAY, false).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1500) .remoteAddress(addr)); }
From source file:com.ctrip.xpipe.redis.console.health.netty.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {/* w ww . j av a 2s. c o m*/ sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT)); } // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT); Channel c = f.channel(); while (true) { ByteBuf buf = Unpooled.buffer(8); buf.writeLong(System.nanoTime()); c.writeAndFlush(buf); Thread.sleep(1000); } } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.ctrip.xpipe.redis.console.health.netty.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from www .j a v a 2 s . co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } 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).option(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // 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(); } }
From source file:com.digitalpetri.opcua.stack.client.UaTcpStackClient.java
License:Apache License
public static CompletableFuture<ClientSecureChannel> bootstrap(UaTcpStackClient client, Optional<ClientSecureChannel> existingChannel) { CompletableFuture<ClientSecureChannel> handshake = new CompletableFuture<>(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(client.getConfig().getEventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override/*from ww w.jav a2 s . c om*/ protected void initChannel(SocketChannel channel) throws Exception { UaTcpClientAcknowledgeHandler acknowledgeHandler = new UaTcpClientAcknowledgeHandler(client, existingChannel, handshake); channel.pipeline().addLast(acknowledgeHandler); } }); try { URI uri = URI.create(client.getEndpointUrl()); bootstrap.connect(uri.getHost(), uri.getPort()).addListener((ChannelFuture f) -> { if (!f.isSuccess()) { handshake.completeExceptionally(f.cause()); } }); } catch (Throwable t) { UaException failure = new UaException(StatusCodes.Bad_TcpEndpointUrlInvalid, "endpoint URL invalid: " + client.getEndpointUrl()); handshake.completeExceptionally(failure); } return handshake; }
From source file:com.digitalpetri.opcua.stack.server.tcp.SocketServer.java
License:Apache License
private SocketServer(InetSocketAddress address) { this.address = address; bootstrap.group(Stack.sharedEventLoop()).handler(new LoggingHandler(SocketServer.class)) .channel(NioServerSocketChannel.class) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w. j ava 2 s . c om*/ protected void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new UaTcpServerHelloHandler(SocketServer.this)); } }); }
From source file:com.dinstone.jrpc.transport.netty4.NettyAcceptance.java
License:Apache License
@Override public Acceptance bind() { bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("N4A-Boss")); workGroup = new NioEventLoopGroup(transportConfig.getNioProcessorCount(), new DefaultThreadFactory("N4A-Work")); ServerBootstrap boot = new ServerBootstrap().group(bossGroup, workGroup); boot.channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w ww . j a v a 2s .co m*/ public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(intervalSeconds * 2, 0, 0)); ch.pipeline().addLast("NettyServerHandler", new NettyServerHandler()); } }); boot.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_RCVBUF, 16 * 1024).childOption(ChannelOption.SO_SNDBUF, 16 * 1024) .childOption(ChannelOption.TCP_NODELAY, true); try { boot.bind(serviceAddress).sync(); int processorCount = transportConfig.getBusinessProcessorCount(); if (processorCount > 0) { NamedThreadFactory threadFactory = new NamedThreadFactory("N4A-BusinessProcessor"); executorService = Executors.newFixedThreadPool(processorCount, threadFactory); } } catch (Exception e) { throw new RuntimeException("can't bind service on " + serviceAddress, e); } LOG.info("netty acceptance bind on {}", serviceAddress); return this; }
From source file:com.dinstone.jrpc.transport.netty4.NettyConnector.java
License:Apache License
public NettyConnector(InetSocketAddress isa, final TransportConfig transportConfig) { workerGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("N4C-Work")); clientBoot = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class); clientBoot.option(ChannelOption.TCP_NODELAY, true); clientBoot.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, transportConfig.getConnectTimeout()); clientBoot.option(ChannelOption.SO_RCVBUF, 8 * 1024).option(ChannelOption.SO_SNDBUF, 8 * 1024); clientBoot.handler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w . j a v a 2s . co m*/ public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(0, intervalSeconds, 0)); ch.pipeline().addLast("NettyClientHandler", new NettyClientHandler()); } }); clientBoot.remoteAddress(isa); }
From source file:com.dinstone.jrpc.transport.netty5.NettyAcceptance.java
License:Apache License
@Override public Acceptance bind() { bossGroup = new NioEventLoopGroup(1, new DefaultExecutorServiceFactory("N5A-Boss")); workGroup = new NioEventLoopGroup(transportConfig.getNioProcessorCount(), new DefaultExecutorServiceFactory("N5A-Work")); ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w. j a v a2 s . c o m*/ public void initChannel(SocketChannel ch) throws Exception { TransportProtocolDecoder decoder = new TransportProtocolDecoder(); decoder.setMaxObjectSize(transportConfig.getMaxSize()); TransportProtocolEncoder encoder = new TransportProtocolEncoder(); encoder.setMaxObjectSize(transportConfig.getMaxSize()); ch.pipeline().addLast("TransportProtocolDecoder", decoder); ch.pipeline().addLast("TransportProtocolEncoder", encoder); int intervalSeconds = transportConfig.getHeartbeatIntervalSeconds(); ch.pipeline().addLast("IdleStateHandler", new IdleStateHandler(intervalSeconds * 2, 0, 0)); ch.pipeline().addLast("NettyServerHandler", new NettyServerHandler()); } }); boot.option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_RCVBUF, 16 * 1024).childOption(ChannelOption.SO_SNDBUF, 16 * 1024) .childOption(ChannelOption.TCP_NODELAY, true); try { boot.bind(serviceAddress).sync(); int processorCount = transportConfig.getBusinessProcessorCount(); if (processorCount > 0) { NamedThreadFactory threadFactory = new NamedThreadFactory("N5A-BusinssProcessor"); executorService = Executors.newFixedThreadPool(processorCount, threadFactory); } } catch (Exception e) { throw new RuntimeException("can't bind service on " + serviceAddress, e); } LOG.info("netty5 acceptance bind on {}", serviceAddress); return this; }