List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.lampard.netty4.protocol.netty.server.NettyServer.java
License:Apache License
public void bind() throws Exception { // ??NIO//from w w w . ja v a 2 s . c o m EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); 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 IOException { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast(new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast(new LoginAuthRespHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler()); } }); // ??? b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync(); LOG.info("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT)); }
From source file:com.lb.netty.protoc.SubReqClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO/*from w w w.j a va 2 s.com*/ EventLoopGroup group = new NioEventLoopGroup(); try { System.out.println("12312312312312"); 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 { System.out.println("123123123123123"); ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); ch.pipeline().addLast( new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance())); ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); ch.pipeline().addLast(new ProtobufEncoder()); ch.pipeline().addLast(new SubReqClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:com.lb.netty.protoc.SubReqServer.java
License:Apache License
public void bind(int port) throws Exception { // ??NIO/*from w w w . ja va 2 s. co m*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { 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) { ch.pipeline().addLast(new ProtobufVarint32FrameDecoder()); // protobuffer ch.pipeline().addLast( new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance())); ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender()); ch.pipeline().addLast(new ProtobufEncoder()); ch.pipeline().addLast(new SubReqServerHandler()); } }); // ??? ChannelFuture f = b.bind(port).sync(); // ??? f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.lbwan.game.client.WebSocketClientRunner.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/* w w w . j av a 2s . co m*/ // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. // If you change it to V00, ping is not supported and remember to change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); final String protocol = uri.getScheme(); int defaultPort; ChannelInitializer<SocketChannel> initializer; // Normal WebSocket if ("ws".equals(protocol)) { initializer = new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-codec", new HttpClientCodec()) .addLast("aggregator", new HttpObjectAggregator(8192)) .addLast("ws-handler", handler); } }; defaultPort = 80; // Secure WebSocket } else { throw new IllegalArgumentException("Unsupported protocol: " + protocol); } Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(initializer); int port = uri.getPort(); // If no port was specified, we'll try the default port: https://tools.ietf.org/html/rfc6455#section-1.7 if (uri.getPort() == -1) { port = defaultPort; } Channel ch = b.connect(uri.getHost(), port).sync().channel(); handler.handshakeFuture().sync(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); while (true) { String msg = console.readLine(); if (msg == null) { break; } else if ("bye".equals(msg.toLowerCase())) { ch.writeAndFlush(new CloseWebSocketFrame()); ch.closeFuture().sync(); break; } else if ("ping".equals(msg.toLowerCase())) { WebSocketFrame frame = new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 8, 1, 8, 1 })); ch.writeAndFlush(frame); } else { WebSocketFrame frame = new TextWebSocketFrame(msg); ch.writeAndFlush(frame); } } } finally { group.shutdownGracefully(); } }
From source file:com.liferay.nativity.control.findersync.FSNativityControlImpl.java
License:Open Source License
@Override public boolean connect() { if (_connected) { return true; }/*from w w w . java2 s.com*/ _childEventLoopGroup = new NioEventLoopGroup(); _parentEventLoopGroup = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(_parentEventLoopGroup, _childEventLoopGroup); serverBootstrap.channel(NioServerSocketChannel.class); ChannelInitializer channelInitializer = new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { DelimiterBasedFrameDecoder messageDecoder = new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()); FinderSyncChannelHandler finderSyncChannelHandler = new FinderSyncChannelHandler(); socketChannel.pipeline().addLast(messageDecoder, finderSyncChannelHandler); } }; serverBootstrap.childHandler(channelInitializer); serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture channelFuture = serverBootstrap.bind(0).sync(); InetSocketAddress inetSocketAddress = (InetSocketAddress) channelFuture.channel().localAddress(); _writePortToFile(inetSocketAddress.getPort()); } catch (Exception e) { _logger.error(e.getMessage(), e); _connected = false; return false; } _connected = true; return true; }
From source file:com.lin.studytest.netty.server.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// ww w . j a v a 2s. c o 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(); 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.linecorp.armeria.client.endpoint.dns.TestDnsServer.java
License:Apache License
TestDnsServer(Map<DnsQuestion, DnsResponse> responses) { this.responses = ImmutableMap.copyOf(responses); final Bootstrap b = new Bootstrap(); b.channel(TransportType.datagramChannelType(CommonPools.workerGroup())); b.group(CommonPools.workerGroup());// w w w .java2 s. co m b.handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { final ChannelPipeline p = ch.pipeline(); p.addLast(new DatagramDnsQueryDecoder()); p.addLast(new DatagramDnsResponseEncoder()); p.addLast(new DnsServerHandler()); } }); channel = b.bind(NetUtil.LOCALHOST, 0).syncUninterruptibly().channel(); }
From source file:com.linecorp.armeria.client.http.HttpSessionChannelFactory.java
License:Apache License
private Bootstrap bootstrap(SessionProtocol sessionProtocol) { return bootstrapMap.computeIfAbsent(sessionProtocol, sp -> { Bootstrap bs = baseBootstrap.clone(); bs.handler(new ChannelInitializer<Channel>() { @Override//from w w w.ja v a 2s . co m protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new HttpClientPipelineConfigurator(sp, options)); } }); return bs; }); }
From source file:com.linecorp.armeria.client.HttpSessionChannelFactory.java
License:Apache License
private Bootstrap bootstrap(SessionProtocol sessionProtocol) { return bootstrapMap.computeIfAbsent(sessionProtocol, sp -> { final Bootstrap bs = baseBootstrap.clone(); bs.handler(new ChannelInitializer<Channel>() { @Override//w w w.ja va 2s. c o m protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new HttpClientPipelineConfigurator(clientFactory, sp)); } }); return bs; }); }
From source file:com.linkedin.mitm.proxy.channel.ChannelMediator.java
License:Open Source License
/** * Establishing TCP connection to server * * @param remoteAddress remote address/* ww w . j a va2 s .co m*/ * */ public ChannelFuture connectToServer(final InetSocketAddress remoteAddress) { if (remoteAddress == null) { throw new IllegalStateException("remote address is null"); } Bootstrap bootstrap = new Bootstrap().group(_upstreamWorkerGroup); bootstrap.channelFactory(NioSocketChannel::new); ServerChannelHandler serverChannelHandler = new ServerChannelHandler(this); bootstrap.handler(new ChannelInitializer<Channel>() { protected void initChannel(Channel ch) throws Exception { initChannelPipeline(ch.pipeline(), serverChannelHandler, _serverConnectionIdleTimeoutMsec); _serverChannel = ch; } }); LOG.debug("Server channel is ready. About to connect...."); return bootstrap.connect(remoteAddress); }