当前位置: 首页>>代码示例>>Java>>正文


Java SimpleChannelInboundHandler类代码示例

本文整理汇总了Java中io.netty.channel.SimpleChannelInboundHandler的典型用法代码示例。如果您正苦于以下问题:Java SimpleChannelInboundHandler类的具体用法?Java SimpleChannelInboundHandler怎么用?Java SimpleChannelInboundHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


SimpleChannelInboundHandler类属于io.netty.channel包,在下文中一共展示了SimpleChannelInboundHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createListenerHandler

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
private SimpleChannelInboundHandler<DatagramPacket> createListenerHandler(SeedNode thisNode, ByteBuf seedNodeInfo) {
    return new SimpleChannelInboundHandler<DatagramPacket>() {
        @Override
        public void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
            ByteBuf buf = msg.content();

            if (buf.readableBytes() > 4 && buf.readInt() == Utils.MAGIC_BYTES) {
                MessageTYpe msgType = MessageTYpe.values()[buf.readByte()];

                if (msgType == MessageTYpe.DISCOVERY) {
                    String cluster = decodeUtf(buf);
                    InetSocketAddress address = decodeAddress(buf);

                    if (thisNode.cluster().equals(cluster) && !address.equals(thisNode.address())) {
                        onDiscoveryMessage(address);

                        DatagramPacket response = new DatagramPacket(seedNodeInfo.copy(), msg.sender());

                        ctx.writeAndFlush(response);
                    }
                }
            }
        }
    };
}
 
开发者ID:hekate-io,项目名称:hekate,代码行数:26,代码来源:MulticastSeedNodeProvider.java

示例2: createNettyHttpClientBootstrap

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
public static Bootstrap createNettyHttpClientBootstrap() {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(new NioEventLoopGroup())
             .channel(NioSocketChannel.class)
             .handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 protected void initChannel(SocketChannel ch) throws Exception {
                     ChannelPipeline p = ch.pipeline();
                     p.addLast(new HttpClientCodec());
                     p.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
                     p.addLast("clientResponseHandler", new SimpleChannelInboundHandler<HttpObject>() {
                         @Override
                         protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
                             throw new RuntimeException("Client response handler was not setup before the call");
                         }
                     });
                 }
             });

    return bootstrap;
}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:22,代码来源:ComponentTestUtils.java

示例3: setupNettyHttpClientResponseHandler

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
public static CompletableFuture<NettyHttpClientResponse> setupNettyHttpClientResponseHandler(
    Channel ch, Consumer<ChannelPipeline> pipelineAdjuster
) {
    CompletableFuture<NettyHttpClientResponse> responseFromServerFuture = new CompletableFuture<>();
    ch.pipeline().replace("clientResponseHandler", "clientResponseHandler", new SimpleChannelInboundHandler<HttpObject>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
            throws Exception {
            if (msg instanceof FullHttpResponse) {
                // Store the proxyServer response for asserting on later.
                responseFromServerFuture.complete(new NettyHttpClientResponse((FullHttpResponse) msg));
            } else {
                // Should never happen.
                throw new RuntimeException("Received unexpected message type: " + msg.getClass());
            }
        }
    });

    if (pipelineAdjuster != null)
        pipelineAdjuster.accept(ch.pipeline());
    
    return responseFromServerFuture;
}
 
开发者ID:Nike-Inc,项目名称:riposte,代码行数:24,代码来源:ComponentTestUtils.java

示例4: exceptionCaught

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (log.isDebugEnabled()) {
        log.error("Unhandled exception caught within the pipeline {} for Channel {}, Id: {}", cause, ctx.channel(), ctx.channel().id());
        if (ctx.channel().hasAttr(ChannelAttributes.LAST_REQUEST_SENT)) {
            AbstractRequest request = ctx.channel().attr(ChannelAttributes.LAST_REQUEST_SENT).get();
            if (request != null && SocketChannel.class.isAssignableFrom(ctx.channel().getClass())) {
                Throwable ex = new ResponseException(request, cause);
                SimpleChannelInboundHandler responseRouter = ctx.pipeline().get(SimpleChannelInboundHandler.class);
                responseRouter.channelRead(ctx, ex);
                return;
            }
        }
        throw new TransportException(cause);
    }
}
 
开发者ID:ribasco,项目名称:async-gamequery-lib,代码行数:17,代码来源:ErrorHandler.java

示例5: initChannel

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
  ChannelPipeline pipeline = ch.pipeline();

  SSLEngine engine = context.createSSLEngine();
  engine.setUseClientMode(true);
  SslHandler sslHandler = new SslHandler(engine);
  //pipeline.addLast(sslHandler);
  pipeline.addLast(new SimpleChannelInboundHandler<Object>() {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
      System.out.println(msg);
    }
  });
  //pipeline.addLast(new HttpRequestDecoder());
  //pipeline.addLast(new HttpResponseEncoder());
  //pipeline.addLast(new HttpContentCompressor());
  //pipeline.addLast(new HTTPClientHandler());
}
 
开发者ID:elastic,项目名称:tealess,代码行数:21,代码来源:HTTPSInitializer.java

示例6: newBootstrapUDP

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
public static ChannelFuture newBootstrapUDP(EventLoopGroup loopGroup, SimpleChannelInboundHandler<DatagramPacket> handler, int port){
	return new Bootstrap().group(loopGroup)
			.channel(NioDatagramChannel.class)
			.option(ChannelOption.SO_BROADCAST, true)
			.handler(handler)
			.bind(port);
}
 
开发者ID:duchien85,项目名称:netty-cookbook,代码行数:8,代码来源:BootstrapTemplate.java

示例7: initializerFactory

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
@Bean(name = "channelInitializer")
public ChannelInitializer<SocketChannel> initializerFactory(final ApplicationContext contxt) {
	return new ChannelInitializer<SocketChannel>() {
		@Override
		public void initChannel(SocketChannel ch) throws Exception {
			SimpleChannelInboundHandler<?> channelInboundHandler = contxt.getBean(NettyHttpChannelHandler.class);
			ChannelPipeline pipeline = ch.pipeline();
			// HTTP
			pipeline.addLast(new HttpRequestDecoder());
			pipeline.addLast(new HttpResponseEncoder());
			pipeline.addLast(new HttpContentCompressor());
			pipeline.addLast(new ChunkedWriteHandler());
			// 设置处理的Handler
			pipeline.addLast(channelInboundHandler);
		}
	};
}
 
开发者ID:bleast,项目名称:netty-http-server,代码行数:18,代码来源:ServerConfig.java

示例8: SimpleLineBasedSerialChannel

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
public SimpleLineBasedSerialChannel(String port, final SimpleStringChannelHandler stringHandler) {
	group = new OioEventLoopGroup();
       Bootstrap b = new Bootstrap();
       b.group(group)
        .channel(JsscChannel.class)
        .handler(new ChannelInitializer<JsscChannel>() {
            @Override
            public void initChannel(JsscChannel ch) throws Exception {
                ch.pipeline().addLast(
                    new LineBasedFrameDecoder(Integer.MAX_VALUE),
                    new StringDecoder(),
                    new SimpleChannelInboundHandler<String>() {
                   	 @Override
                   	 protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, String msg) throws Exception {
                   		stringHandler.channelRead(ctx, msg); 
                   	 }
				 }
                );
            }
        });

        f = b.connect(new JsscDeviceAddress(port)).syncUninterruptibly();
}
 
开发者ID:jkschneider,项目名称:netty-jssc,代码行数:24,代码来源:SimpleLineBasedSerialChannel.java

示例9: createTCPListeningPoint

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
private ServerBootstrap createTCPListeningPoint(final SimpleChannelInboundHandler<SipMessageEvent> handler) {
    final ServerBootstrap b = new ServerBootstrap();

    b.group(this.bossGroup, this.workerGroup)
    .channel(NioServerSocketChannel.class)
    .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(final SocketChannel ch) throws Exception {
            final ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("decoder", new SipMessageStreamDecoder());
            pipeline.addLast("encoder", new SipMessageEncoder());
            pipeline.addLast("handler", handler);
        }
    })
    .option(ChannelOption.SO_BACKLOG, 128)
    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
    .childOption(ChannelOption.SO_KEEPALIVE, true)
    .childOption(ChannelOption.TCP_NODELAY, true);
    return b;
}
 
开发者ID:aboutsip,项目名称:sipstack,代码行数:21,代码来源:SimpleSipStack.java

示例10: protoBuf

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
/**
 * Returns a new channel initializer suited to encode and decode a protocol
 * buffer message.
 * <p/>
 * <p>Message sizes over 10 MB are not supported.</p>
 * <p/>
 * <p>The handler will be executed on the I/O thread. Blocking operations
 * should be executed in their own thread.</p>
 *
 * @param defaultInstance an instance of the message to handle
 * @param handler the handler implementing the application logic
 * @param <M> the type of the support protocol buffer message
 */
public static final <M extends Message> ChannelInitializer<Channel> protoBuf(
    final M defaultInstance, final SimpleChannelInboundHandler<M> handler) {
  return new ChannelInitializer<Channel>() {

    @Override
    protected void initChannel(Channel channel) throws Exception {
      channel.pipeline().addLast("frameDecoder",
          new LengthFieldBasedFrameDecoder(10 * 1024 * 1024, 0, 4, 0, 4));
      channel.pipeline().addLast("protobufDecoder",
          new ProtobufDecoder(defaultInstance));
      channel.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4));
      channel.pipeline().addLast("protobufEncoder", new ProtobufEncoder());
      channel.pipeline().addLast("applicationHandler", handler);
    }
  };
}
 
开发者ID:jsilland,项目名称:piezo,代码行数:30,代码来源:ChannelInitializers.java

示例11: secureHttpServer

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
/**
 * Returns a server-side channel initializer capable of securely receiving
 * and sending HTTP requests and responses
 * <p/>
 * <p>Communications will be encrypted as per the configured SSL context</p>
 *
 * @param handler the handler implementing the business logic.
 * @param sslContext the SSL context which drives the security of the
 * link to the client.
 */
public static final ChannelInitializer<Channel> secureHttpServer(
    final SimpleChannelInboundHandler<HttpRequest> handler,
    final SSLContext sslContext) {
  return new ChannelInitializer<Channel>() {
    @Override
    protected void initChannel(Channel channel) throws Exception {
      ChannelPipeline pipeline = channel.pipeline();
      SSLEngine sslEngine = sslContext.createSSLEngine();
      sslEngine.setUseClientMode(false);
      pipeline.addLast("ssl", new SslHandler(sslEngine));
      pipeline.addLast("httpCodec", new HttpServerCodec());
      pipeline.addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024));
      pipeline.addLast("httpServerHandler", handler);
    }
  };
}
 
开发者ID:jsilland,项目名称:piezo,代码行数:27,代码来源:ChannelInitializers.java

示例12: secureHttpClient

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
/**
 * Returns a client-side channel initializer capable of securely sending
 * and receiving HTTP requests and responses.
 * <p/>
 * <p>Communications will be encrypted as per the configured SSL context</p>
 *
 * @param handler the handler in charge of implementing the business logic
 * @param sslContext the SSL context which drives the security of the
 * link to the server.
 */
public static final ChannelInitializer<Channel> secureHttpClient(
    final SimpleChannelInboundHandler<HttpResponse> handler,
    final SSLContext sslContext) {
  return new ChannelInitializer<Channel>() {

    @Override
    protected void initChannel(Channel channel) throws Exception {
      ChannelPipeline pipeline = channel.pipeline();
      SSLEngine sslEngine = sslContext.createSSLEngine();
      sslEngine.setUseClientMode(true);
      pipeline.addLast("ssl", new SslHandler(sslEngine));
      pipeline.addLast("httpCodec", new HttpClientCodec());
      pipeline.addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024));
      pipeline.addLast("httpClientHandler", handler);
    }
  };
}
 
开发者ID:jsilland,项目名称:piezo,代码行数:28,代码来源:ChannelInitializers.java

示例13: initChannel

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
@Override
protected void initChannel(Channel channel) throws Exception {
    InetSocketAddress address = (InetSocketAddress) channel.remoteAddress();
    Address clientAddress = new Address(address.getHostName(), address.getPort());
    channel.pipeline().addLast(
            master.proxyHandler(clientAddress),
            new SimpleChannelInboundHandler<Object>() {
                @Override
                protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o)
                        throws Exception {
                    LOGGER.info("[Client ({})] => Unhandled inbound: {}", clientAddress, o);
                }
            });
}
 
开发者ID:chhsiao90,项目名称:nitmproxy,代码行数:15,代码来源:NitmProxyInitializer.java

示例14: EpollConnDroppingServer

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
public EpollConnDroppingServer(final int port, final int dropEveryRequest)
throws InterruptedException {
	dispatchGroup = new EpollEventLoopGroup();
	workerGroup = new EpollEventLoopGroup();
	final ServerBootstrap bootstrap = new ServerBootstrap()
		.group(dispatchGroup, workerGroup)
		.channel(EpollServerSocketChannel.class)
		.childHandler(
			new ChannelInitializer<SocketChannel>() {
				@Override
				public final void initChannel(final SocketChannel ch) {
					if(dropEveryRequest > 0) {
						ch.pipeline().addLast(
							new SimpleChannelInboundHandler<Object>() {
								@Override
								protected final void channelRead0(
									final ChannelHandlerContext ctx, final Object msg
								) throws Exception {
									if(0 == reqCounter.incrementAndGet() % dropEveryRequest) {
										final Channel conn = ctx.channel();
										System.out.println("Dropping the connection " + conn);
										conn.close();
									}
								}
							}
						);
					}
				}
			}
		);

	bindFuture = bootstrap.bind(port).sync();
}
 
开发者ID:akurilov,项目名称:netty-connection-pool,代码行数:34,代码来源:EpollConnDroppingServer.java

示例15: NioConnDroppingServer

import io.netty.channel.SimpleChannelInboundHandler; //导入依赖的package包/类
public NioConnDroppingServer(final int port, final int dropEveryRequest)
throws InterruptedException {
	dispatchGroup = new NioEventLoopGroup();
	workerGroup = new NioEventLoopGroup();
	final ServerBootstrap bootstrap = new ServerBootstrap()
		.group(dispatchGroup, workerGroup)
		.channel(NioServerSocketChannel.class)
		.childHandler(
			new ChannelInitializer<SocketChannel>() {
				@Override
				public final void initChannel(final SocketChannel ch) {
					ch.pipeline().addLast(
						new SimpleChannelInboundHandler<Object>() {
							@Override
							protected final void channelRead0(
								final ChannelHandlerContext ctx, final Object msg
							) throws Exception {
								if(0 == reqCounter.incrementAndGet() % dropEveryRequest) {
									final Channel conn = ctx.channel();
									System.out.println("Dropping the connection " + conn);
									conn.close();
								}
							}
						}
					);
				}
			}
		);

	bindFuture = bootstrap.bind(port).sync();
}
 
开发者ID:akurilov,项目名称:netty-connection-pool,代码行数:32,代码来源:NioConnDroppingServer.java


注:本文中的io.netty.channel.SimpleChannelInboundHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。