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:books.netty.frame.correct.TimeClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO/* w w w . ja va 2 s . c o m*/ 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) { ch.pipeline().addLast(new LineBasedFrameDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new TimeClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:books.netty.frame.delimiter.EchoClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO// w w w . jav a 2 s . c om 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) { ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:books.netty.frame.fault.TimeClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO//from w ww . j ava 2s.co m 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) { ch.pipeline().addLast(new TimeClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:books.netty.protocol.http.xml.client.HttpXmlClient.java
License:Apache License
public void connect(int port) throws Exception { // ?NIO/*from w w w . j a va 2 s.c om*/ 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) { ch.pipeline().addLast("http-decoder", new HttpResponseDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); // XML? ch.pipeline().addLast("xml-decoder", new HttpXmlResponseDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpRequestEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlRequestEncoder()); ch.pipeline().addLast("xmlClientHandler", new HttpXmlClientHandle()); } }); // ?? ChannelFuture f = b.connect(new InetSocketAddress(port)).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:books.netty.protocol.netty.client.NettyClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO// w w w. j a v a 2 s.c o m 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 { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast("MessageEncoder", new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast("LoginAuthHandler", new LoginAuthReqHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatReqHandler()); } }); // ?? ChannelFuture future = b.connect(new InetSocketAddress(host, port), new InetSocketAddress(NettyConstant.LOCALIP, NettyConstant.LOCAL_PORT)).sync(); future.channel().closeFuture().sync(); } finally { // ???????? executor.execute(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(1); try { connect(NettyConstant.PORT, NettyConstant.REMOTEIP);// ??? } catch (Exception e) { e.printStackTrace(); } } catch (InterruptedException e) { e.printStackTrace(); } } }); } }
From source file:c5db.control.ControlService.java
License:Apache License
private void startHttpRpc() { try {/*from w w w. jav a2 s .c o m*/ ServerBootstrap serverBootstrap = new ServerBootstrap(); ServerBootstrap serverBootstrap1 = serverBootstrap.group(acceptConnectionGroup, ioWorkerGroup) .channel(NioServerSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_BACKLOG, 100).childOption(ChannelOption.TCP_NODELAY, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // pipeline.addLast("logger", new LoggingHandler(LogLevel.DEBUG)); pipeline.addLast("http-server", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE)); pipeline.addLast("encode", new ServerHttpProtostuffEncoder()); pipeline.addLast("decode", new ServerHttpProtostuffDecoder()); pipeline.addLast("translate", new ServerDecodeCommandRequest()); pipeline.addLast("inc-messages", new MessageHandler()); } }); serverBootstrap.bind(modulePort).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // yay listenChannel = future.channel(); notifyStarted(); } else { LOG.error("Unable to bind to port {}", modulePort); notifyFailed(future.cause()); } } }); } catch (Exception e) { notifyFailed(e); } }
From source file:c5db.control.SimpleControlClient.java
License:Apache License
private void createClient() { client.group(ioWorkerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() { @Override//from w w w . j a v a 2 s .c om protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // pipeline.addLast("logger", new LoggingHandler(LogLevel.WARN)); pipeline.addLast("http-client", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE)); pipeline.addLast("encode", new ClientHttpProtostuffEncoder()); pipeline.addLast("decode", new ClientHttpProtostuffDecoder()); pipeline.addLast("translate", new ClientEncodeCommandRequest()); } }); }
From source file:c5db.regionserver.RegionServerService.java
License:Apache License
@Override protected void doStart() { fiber.start();//from www . j a va 2s. c o m fiber.execute(() -> { // we need the tablet module: ListenableFuture<C5Module> f = server.getModule(ModuleType.Tablet); Futures.addCallback(f, new FutureCallback<C5Module>() { @Override public void onSuccess(final C5Module result) { tabletModule = (TabletModule) result; bootstrap.group(acceptGroup, workerGroup).option(ChannelOption.SO_REUSEADDR, true) .childOption(ChannelOption.TCP_NODELAY, true).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("http-server-codec", new HttpServerCodec()); p.addLast("http-agg", new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE)); p.addLast("websocket-agg", new WebSocketFrameAggregator(C5ServerConstants.MAX_CALL_SIZE)); p.addLast("decoder", new WebsocketProtostuffDecoder("/websocket")); p.addLast("encoder", new WebsocketProtostuffEncoder()); p.addLast("handler", new RegionServerHandler(RegionServerService.this)); } }); bootstrap.bind(port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { listenChannel = future.channel(); notifyStarted(); } else { LOG.error("Unable to find Region Server to {} {}", port, future.cause()); notifyFailed(future.cause()); } } }); } @Override public void onFailure(Throwable t) { notifyFailed(t); } }, fiber); }); }
From source file:c5db.replication.ReplicatorService.java
License:Apache License
/** * ********** Service startup/registration and shutdown/termination ************** *//*from w ww . j av a 2s . c om*/ @Override protected void doStart() { // must start the fiber up early. fiber = fiberSupplier.getNewFiber(this::failModule); setupEventChannelSubscription(); fiber.start(); C5Futures.addCallback(getDependedOnModules(), (ignore) -> { ChannelInitializer<SocketChannel> initer = new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("frameDecode", new ProtobufVarint32FrameDecoder()); p.addLast("pbufDecode", new ProtostuffDecoder<>(ReplicationWireMessage.getSchema())); p.addLast("frameEncode", new ProtobufVarint32LengthFieldPrepender()); p.addLast("pbufEncoder", new ProtostuffEncoder<ReplicationWireMessage>()); p.addLast(new MessageHandler()); } }; serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 100) .childOption(ChannelOption.TCP_NODELAY, true).childHandler(initer); //noinspection RedundantCast serverBootstrap.bind(port).addListener((ChannelFutureListener) future -> { if (future.isSuccess()) { LOG.info("successfully bound node {} port {} ", nodeId, port); listenChannel = future.channel(); } else { LOG.error("Unable to bind! ", future.cause()); failModule(future.cause()); } }); outgoingBootstrap.group(workerGroup).channel(NioSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.TCP_NODELAY, true) .handler(initer); //noinspection Convert2MethodRef outgoingRequests.subscribe(fiber, message -> handleOutgoingMessage(message), // Clean up cancelled requests. message -> handleCancelledSession(message.getSession())); notifyStarted(); }, (Throwable t) -> { LOG.error("ReplicatorService unable to retrieve modules!", t); failModule(t); }, fiber); }
From source file:cat.tbq.hospital.nio.echoExample.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {/* w w w . java 2s. c om*/ 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()); // p.addLast(new EchoClient2Handler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }