List of usage examples for io.netty.channel ChannelPipeline addLast
ChannelPipeline addLast(EventExecutorGroup group, ChannelHandler... handlers);
From source file:com.nitesh.netty.snoop.client.HttpSnoopClientInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { // Create a default pipeline implementation. ChannelPipeline p = ch.pipeline(); p.addLast("log", new LoggingHandler(LogLevel.DEBUG)); p.addLast("codec", new HttpClientCodec()); // Remove the following line if you don't want automatic content decompression. p.addLast("inflater", new HttpContentDecompressor()); // Uncomment the following line if you don't want to handle HttpChunks. //p.addLast("aggregator", new HttpObjectAggregator(1048576)); p.addLast("handler", new HttpSnoopClientHandler()); }
From source file:com.nitesh.netty.snoop.server.HttpSnoopServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { // Create a default pipeline implementation. ChannelPipeline p = ch.pipeline(); // Uncomment the following line if you want HTTPS //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine(); //engine.setUseClientMode(false); //p.addLast("ssl", new SslHandler(engine)); p.addLast("log", new LoggingHandler(LogLevel.DEBUG)); p.addLast("decoder", new HttpRequestDecoder()); // Uncomment the following line if you don't want to handle HttpChunks. //p.addLast("aggregator", new HttpObjectAggregator(1048576)); p.addLast("encoder", new HttpResponseEncoder()); // Remove the following line if you don't want automatic content compression. //p.addLast("deflater", new HttpContentCompressor()); p.addLast("handler", new HttpSnoopServerHandler()); }
From source file:com.nus.mazegame.client.GameClient.java
public static void init(String host, final int port, final String type, SocketAddress localAddr) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override/*from www .ja va2 s . c o m*/ protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // Decoders p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); p.addLast("bytesDecoder", new ByteArrayDecoder()); // Encoder p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("bytesEncoder", new ByteArrayEncoder()); p.addLast(GameClientHandler.instance); GameInfo.instance.setType(type); GameInfo.instance.setHostPort(port); } }); // Make the connection attempt. SocketAddress address = new InetSocketAddress(host, port); ChannelFuture f; if (localAddr == null) f = b.connect(address).sync(); else f = b.connect(address, localAddr).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); }
From source file:com.nus.mazegame.server.GameServer.java
public static void init(int port, int x, int y, int treasure, boolean isSlave) throws Exception { GameService.gameService = new GameService(x, y, treasure, isSlave); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w ww . ja v a 2 s . co m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // Decoders p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); p.addLast("bytesDecoder", new ByteArrayDecoder()); // Encoder p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("bytesEncoder", new ByteArrayEncoder()); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new GameServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); Logger.getLogger(GameServerHandler.class.getName()).log(Level.INFO, "Server started..."); // 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.onion.worker.PipelineHandler.java
License:Apache License
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("nioChunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("frameDecoder", RPCMessage.createFrameDecoder()); pipeline.addLast("RPCMessageDecoder", new RPCMessageDecoder()); pipeline.addLast("RPCMessageEncoder", new RPCMessageEncoder()); pipeline.addLast("dataServerHandler", mWorkerDataServerHandler); }
From source file:com.phei.netty.protocol.websocket.server.WebSocketServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww.ja v a2 s . c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //???http? pipeline.addLast("http-codec", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); //???html5????websocket ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); pipeline.addLast("handler", new WebSocketServerHandler()); } }); Channel ch = b.bind(port).sync().channel(); System.out.println("Web socket server started at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.publicCaptivation.stream2me.client.communication.Initialiser.java
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc(), Connection.HOST, Connection.PORT)); }// www.jav a2 s. co m pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader()))); pipeline.addLast("encoder", new ObjectEncoder()); pipeline.addLast("handler", new Handler()); }
From source file:com.qq.servlet.demo.netty.sample.http.file.HttpStaticFileServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { // Create a default pipeline implementation. ChannelPipeline pipeline = ch.pipeline(); // Uncomment the following line if you want HTTPS //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine(); //engine.setUseClientMode(false); //pipeline.addLast("ssl", new SslHandler(engine)); pipeline.addLast("decoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("handler", new HttpStaticFileServerHandler(true)); // Specify false if SSL. }
From source file:com.qq.servlet.demo.netty.sample.http.helloworld.HttpHelloWorldClientIntializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { // Create a default pipeline implementation. ChannelPipeline pipeline = ch.pipeline(); 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 HttpHelloWorldClientHandler()); }
From source file:com.qq.servlet.demo.netty.sample.http.helloworld.HttpHelloWorldServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // Uncomment the following line if you want HTTPS //SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine(); //engine.setUseClientMode(false); //p.addLast("ssl", new SslHandler(engine)); p.addLast("codec", new HttpServerCodec()); p.addLast("handler", new HttpHelloWorldServerHandler()); }