List of usage examples for io.netty.channel.socket SocketChannel alloc
ByteBufAllocator alloc();
From source file:HttpUploadClientIntializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc())); }/*from w w w. ja va 2 s .c o m*/ pipeline.addLast("codec", new HttpClientCodec()); // Remove the following line if you don't want automatic content decompression. pipeline.addLast("inflater", new HttpContentDecompressor()); // to be used since huge file transfer pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("handler", new HttpUploadClientHandler()); }
From source file:Http2ClientInitializer.java
License:Apache License
/** * Configure the pipeline for TLS NPN negotiation to HTTP/2. *//* ww w . j a va 2 s . c o m*/ private void configureSsl(SocketChannel ch) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), connectionHandler); }
From source file:Http2ServerInitializer.java
License:Apache License
/** * Configure the pipeline for TLS NPN negotiation to HTTP/2. *///w w w. j a va 2s .c o m private void configureSsl(SocketChannel ch) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new Http2OrHttpHandler()); }
From source file:SecureChatServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add SSL handler first to encrypt and decrypt everything. // In this example, we use a bogus certificate in the server side // and accept any invalid certificates in the client side. // You will need something more complicated to identify both // and server in the real world. pipeline.addLast(sslCtx.newHandler(ch.alloc())); // On top of the SSL handler, add the text line codec. pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); // and then business logic. pipeline.addLast(new SecureChatServerHandler()); }
From source file:TelnetClientInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc(), TelnetClient.HOST, TelnetClient.PORT)); }// w w w .j av a2 s . c o m // Add the text line codec combination first, pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(DECODER); pipeline.addLast(ENCODER); // and then business logic. pipeline.addLast(CLIENT_HANDLER); }
From source file:NettyHttpTransportHandlerInitializer.java
License:Apache License
/** * * @param ch/*from ww w.j ava2 s . com*/ * @throws Exception */ @Override protected void initChannel(SocketChannel ch) throws Exception { // System.out.println("Initlizing Source channel pool"); ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new HttpServerCodec(102400, 102400, 102400)); // p.addLast(new HttpObjectAggregator(Constants.MAXIMUM_CHUNK_SIZE_AGGREGATOR)); p.addLast(new NettyHttpTransportSourceHandler(connections)); }
From source file:blazingcache.network.netty.NettyChannelAcceptor.java
License:Apache License
public void start() throws Exception { if (ssl) {/*from w ww . ja v a 2 s . c o m*/ if (sslCertFile == null) { LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate"); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } SelfSignedCertificate ssc = new SelfSignedCertificate(); try { sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).ciphers(sslCiphers) .build(); } finally { ssc.delete(); } } else { LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath() + " chain file " + sslCertChainFile.getAbsolutePath()); if (sslCiphers != null) { LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers); } sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword) .ciphers(sslCiphers).build(); } } if (callbackThreads == 0) { callbackExecutor = Executors.newCachedThreadPool(); } else { callbackExecutor = Executors.newFixedThreadPool(callbackThreads, new ThreadFactory() { private final AtomicLong count = new AtomicLong(); @Override public Thread newThread(Runnable r) { return new Thread(r, "blazingcache-callbacks-" + count.incrementAndGet()); } }); } bossGroup = new NioEventLoopGroup(workerThreads); workerGroup = new NioEventLoopGroup(workerThreads); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { NettyChannel session = new NettyChannel("unnamed", ch, callbackExecutor, null); if (acceptor != null) { acceptor.createConnection(session); } // ch.pipeline().addLast(new LoggingHandler()); // Add SSL handler first to encrypt and decrypt everything. if (ssl) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc())); } ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4)); ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); // ch.pipeline().addLast("messageencoder", new DataMessageEncoder()); ch.pipeline().addLast("messagedecoder", new DataMessageDecoder()); ch.pipeline().addLast(new InboundMessageHandler(session)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(host, port).sync(); // (7) this.channel = f.channel(); }
From source file:blazingcache.network.netty.NettyConnector.java
License:Apache License
public NettyChannel connect() throws Exception { if (ssl) {/*from www . jav a2 s .c o m*/ this.sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { channel = new NettyChannel(host + ":" + port, ch, callbackExecutor, NettyConnector.this); channel.setMessagesReceiver(receiver); if (ssl) { ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), host, port)); } if (socketTimeout > 0) { ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(socketTimeout)); } ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4)); ch.pipeline().addLast("lengthbaseddecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); // ch.pipeline().addLast("messageencoder", new DataMessageEncoder()); ch.pipeline().addLast("messagedecoder", new DataMessageDecoder()); ch.pipeline().addLast(new InboundMessageHandler(channel)); } }); ChannelFuture f = b.connect(host, port).sync(); socketchannel = f.channel(); return channel; }
From source file:bzh.ygu.fun.chitchat.HttpChitChatServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline();/*w w w .j ava 2s . com*/ if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new HttpRequestDecoder()); // Uncomment the following line if you don't want to handle HttpChunks. //p.addLast(new HttpObjectAggregator(1048576)); p.addLast(new HttpResponseEncoder()); // Remove the following line if you don't want automatic content compression. //p.addLast(new HttpContentCompressor()); p.addLast(new HttpChitChatServerHandler()); }
From source file:ca.lambtoncollege.hauntedhouse.client.ClientInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add SSL handler first to encrypt and decrypt everything. // In this example, we use a bogus certificate in the server side // and accept any invalid certificates in the client side. // You will need something more complicated to identify both // and server in the real world. if (sslCtx != null) pipeline.addLast(sslCtx.newHandler(ch.alloc(), Client.HOST, Client.PORT)); // On top of the SSL handler, add the text line codec. pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); // and then business logic. pipeline.addLast(new ClientHandler()); }