本文整理汇总了Java中io.netty.channel.ChannelDuplexHandler类的典型用法代码示例。如果您正苦于以下问题:Java ChannelDuplexHandler类的具体用法?Java ChannelDuplexHandler怎么用?Java ChannelDuplexHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ChannelDuplexHandler类属于io.netty.channel包,在下文中一共展示了ChannelDuplexHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addByteDecoderWhenFullReactorPipeline
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void addByteDecoderWhenFullReactorPipeline() throws Exception {
channel.pipeline()
.addLast(NettyPipeline.HttpCodec, new HttpServerCodec())
.addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler())
.addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
});
ChannelHandler decoder = new LineBasedFrameDecoder(12);
testContext.addHandlerLast("decoder", decoder)
.addHandlerFirst("decoder$extract",
NettyPipeline.inboundHandler(ADD_EXTRACTOR));
assertEquals(channel.pipeline()
.names(),
Arrays.asList(NettyPipeline.HttpCodec,
NettyPipeline.HttpServerHandler,
"decoder$extract",
"decoder",
NettyPipeline.ReactiveBridge,
"DefaultChannelPipeline$TailContext#0"));
}
示例2: addNonByteDecoderWhenFullReactorPipeline
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void addNonByteDecoderWhenFullReactorPipeline() throws Exception {
channel.pipeline()
.addLast(NettyPipeline.HttpCodec, new HttpServerCodec())
.addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler())
.addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
});
ChannelHandler decoder = new ChannelHandlerAdapter() {
};
testContext.addHandlerLast("decoder", decoder);
assertEquals(channel.pipeline()
.names(),
Arrays.asList(NettyPipeline.HttpCodec,
NettyPipeline.HttpServerHandler,
"decoder",
NettyPipeline.ReactiveBridge,
"DefaultChannelPipeline$TailContext#0"));
}
示例3: addByteEncoderWhenFullReactorPipeline
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void addByteEncoderWhenFullReactorPipeline() throws Exception {
channel.pipeline()
.addLast(NettyPipeline.HttpCodec, new HttpServerCodec())
.addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler())
.addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
});
ChannelHandler encoder = new LineBasedFrameDecoder(12);
testContext.addHandlerFirst("encoder", encoder);
assertEquals(channel.pipeline()
.names(),
Arrays.asList(NettyPipeline.HttpCodec,
NettyPipeline.HttpServerHandler,
"encoder",
NettyPipeline.ReactiveBridge,
"DefaultChannelPipeline$TailContext#0"));
}
示例4: addNonByteEncoderWhenFullReactorPipeline
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void addNonByteEncoderWhenFullReactorPipeline() throws Exception {
channel.pipeline()
.addLast(NettyPipeline.HttpCodec, new HttpServerCodec())
.addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler())
.addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
});
ChannelHandler encoder = new ChannelHandlerAdapter() {
};
testContext.addHandlerFirst("encoder", encoder);
assertEquals(channel.pipeline()
.names(),
Arrays.asList(NettyPipeline.HttpCodec,
NettyPipeline.HttpServerHandler,
"encoder",
NettyPipeline.ReactiveBridge,
"DefaultChannelPipeline$TailContext#0"));
}
示例5: addSeveralByteEncodersWhenCodec
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void addSeveralByteEncodersWhenCodec() throws Exception {
ChannelHandler encoder1 = new LineBasedFrameDecoder(12);
ChannelHandler encoder2 = new LineBasedFrameDecoder(13);
channel.pipeline()
.addLast(NettyPipeline.HttpCodec, new HttpServerCodec())
.addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler())
.addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
});
testContext.addHandlerFirst("encoder1", encoder1)
.addHandlerFirst("encoder2", encoder2);
assertEquals(channel.pipeline()
.names(),
Arrays.asList(NettyPipeline.HttpCodec,
NettyPipeline.HttpServerHandler,
"encoder2",
"encoder1",
NettyPipeline.ReactiveBridge,
"DefaultChannelPipeline$TailContext#0"));
}
示例6: initChannel
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Override
protected void initChannel(Channel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new ChannelDuplexHandler() {
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
LOGGER.debug("Disconnected from: {} {}", address, ctx.channel());
outgoingChannels.remove(address);
super.channelInactive(ctx);
}
});
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(serializerHandler);
if (networkEmulatorHandler != null) {
pipeline.addLast(networkEmulatorHandler);
}
pipeline.addLast(exceptionHandler);
}
示例7: handleInitChannel
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
protected void handleInitChannel ( final SocketChannel ch )
{
// add the APCI/APDU handler
ch.pipeline ().addLast ( new APDUDecoder () );
ch.pipeline ().addLast ( new APDUEncoder () );
// add logging
if ( Boolean.getBoolean ( "org.eclipse.scada.protocol.iec60870.trace" ) )
{
ch.pipeline ().addLast ( new LoggingHandler ( LogLevel.TRACE ) );
}
final MessageChannel messageChannel = new MessageChannel ( this.options, this.manager );
// message channel
ch.pipeline ().addLast ( messageChannel );
// now add all server modules
for ( final ServerModule module : this.modules )
{
module.initializeChannel ( ch, messageChannel );
}
// finally add the default exception catcher
ch.pipeline ().addLast ( new ChannelDuplexHandler () {
@Override
public void exceptionCaught ( final ChannelHandlerContext ctx, final Throwable cause ) throws Exception
{
logger.warn ( "Close connection due to uncaught exception", cause );
ctx.close ();
}
} );
}
示例8: initChannel
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
private void initChannel(ChannelPipeline pipeline) {
pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE);
if (this.options.getMaxMessageSize() > 0) {
pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize()));
} else {
// max message size not set, so the default from Netty MQTT codec is used
pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder());
}
// adding the idle state handler for timeout on CONNECT packet
pipeline.addBefore("handler", "idle", new IdleStateHandler(this.options.timeoutOnConnect(), 0, 0));
pipeline.addBefore("handler", "timeoutOnConnect", new ChannelDuplexHandler() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.READER_IDLE) {
// as MQTT 3.1.1 describes, if no packet is sent after a "reasonable" time (here CONNECT timeout)
// the connection is closed
ctx.channel().close();
}
}
}
});
}
示例9: initChannel
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
private void initChannel(ChannelPipeline pipeline) {
// add into pipeline netty's (en/de)coder
pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE);
if (this.options.getMaxMessageSize() > 0) {
pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize()));
} else {
// max message size not set, so the default from Netty MQTT codec is used
pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder());
}
if (this.options.isAutoKeepAlive() &&
this.options.getKeepAliveTimeSeconds() != 0) {
pipeline.addBefore("handler", "idle",
new IdleStateHandler(0, this.options.getKeepAliveTimeSeconds(), 0));
pipeline.addBefore("handler", "keepAliveHandler", new ChannelDuplexHandler() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.WRITER_IDLE) {
ping();
}
}
}
});
}
}
示例10: addSeveralByteDecodersWhenCodec
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void addSeveralByteDecodersWhenCodec() throws Exception {
ChannelHandler decoder1 = new LineBasedFrameDecoder(12);
ChannelHandler decoder2 = new LineBasedFrameDecoder(13);
channel.pipeline()
.addLast(NettyPipeline.HttpCodec, new HttpServerCodec())
.addLast(NettyPipeline.HttpServerHandler, new ChannelDuplexHandler())
.addLast(NettyPipeline.ReactiveBridge, new ChannelHandlerAdapter() {
});
testContext.addHandlerLast("decoder1$extract",
NettyPipeline.inboundHandler(ADD_EXTRACTOR))
.addHandlerLast("decoder1", decoder1)
.addHandlerLast("decoder2$extract",
NettyPipeline.inboundHandler(ADD_EXTRACTOR))
.addHandlerLast("decoder2", decoder2);
assertEquals(channel.pipeline()
.names(),
Arrays.asList(NettyPipeline.HttpCodec,
NettyPipeline.HttpServerHandler,
"decoder1$extract",
"decoder1",
"decoder2$extract",
"decoder2",
NettyPipeline.ReactiveBridge,
"DefaultChannelPipeline$TailContext#0"));
}
示例11: createSubscriber
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Override
public Subscriber<Long> createSubscriber() {
// Embedded channel requires at least one handler when it's created, but HandlerSubscriber
// needs the channels event loop in order to be created, so start with a dummy, then replace.
ChannelHandler dummy = new ChannelDuplexHandler();
EmbeddedChannel channel = new EmbeddedChannel(dummy);
HandlerSubscriber<Long> subscriber = new HandlerSubscriber<>(channel.eventLoop(), 2, 4);
channel.pipeline().replace(dummy, "subscriber", subscriber);
return new SubscriberWithChannel<>(channel, subscriber);
}
开发者ID:playframework,项目名称:netty-reactive-streams,代码行数:12,代码来源:HandlerSubscriberBlackboxVerificationTest.java
示例12: initChannel
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// Initialize our session Object when the channel is initialized, attach
// it to the channel.
ch.attr(NetworkConstants.SESSION_KEY).setIfAbsent(new PlayerIO(ch));
// Initialize the pipeline channel handlers.
ChannelDuplexHandler timeout = new IdleStateHandler(NetworkConstants.INPUT_TIMEOUT, 0, 0);
ByteToMessageDecoder loginHandshakeHandler = new LoginHandshakeHandler();
ch.pipeline().addLast("login-handshake", loginHandshakeHandler);
ch.pipeline().addLast("channel-handler", channelHandler);
ch.pipeline().addLast("timeout", timeout);
}
示例13: testInboundChainNoChanges
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void testInboundChainNoChanges() throws Exception {
List<Interceptor> interceptors = new ArrayList<>();
MockInterceptor interceptor1 = new MockInterceptor();
MockInterceptor interceptor2 = new MockInterceptor();
interceptors.add(interceptor1);
interceptors.add(interceptor2);
EmbeddedChannel channel = new EmbeddedChannel(
new ChannelDuplexHandler() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
InterceptorChain chain = new InterceptorChain(ctx, interceptors, (ResourceRequest) msg);
chain.fireInbound();
}
}
);
ResourceRequest request = new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build();
channel.writeInbound(request);
ResourceRequest endRequest = (ResourceRequest) channel.readInbound();
assertThat(endRequest).isNotNull();
assertThat(interceptor1.requests()).hasSize(1);
assertThat(interceptor1.requests()).contains(request);
assertThat(interceptor2.requests()).hasSize(1);
assertThat(interceptor2.requests()).contains(request);
}
示例14: testOutbound
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void testOutbound() throws Exception {
List<Interceptor> interceptors = new ArrayList<>();
MockInterceptor interceptor1 = new MockInterceptor();
MockInterceptor interceptor2 = new MockInterceptor();
interceptors.add(interceptor1);
interceptors.add(interceptor2);
ResourceRequest request = new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build();
ResourceResponse response = new DefaultResourceErrorResponse(request, ResourceErrorResponse.ErrorType.NOT_AUTHORIZED);
EmbeddedChannel channel = new EmbeddedChannel(
new ChannelDuplexHandler() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
InterceptorChain chain = new InterceptorChain(ctx, interceptors, (ResourceResponse) msg);
chain.fireOutbound();
}
}
);
channel.writeInbound(request);
ResourceRequest endRequest = (ResourceRequest) channel.readInbound();
channel.writeAndFlush(response);
ResourceResponse endResponse = (ResourceResponse) channel.readOutbound();
assertThat(endResponse).isNotNull();
assertThat(interceptor2.responses()).hasSize(1);
assertThat(interceptor2.responses()).contains(response);
assertThat(interceptor1.responses()).hasSize(1);
assertThat(interceptor1.responses()).contains(response);
}
示例15: testInboundChainShortCircuit
import io.netty.channel.ChannelDuplexHandler; //导入依赖的package包/类
@Test
public void testInboundChainShortCircuit() throws Exception {
List<Interceptor> interceptors = new ArrayList<>();
ResourceRequest request = new DefaultResourceRequest.Builder(RequestType.READ, new ResourcePath("/foo/bar")).build();
ResourceResponse response = new DefaultResourceErrorResponse(request, ResourceErrorResponse.ErrorType.NOT_AUTHORIZED);
MockInterceptor interceptor1 = new MockInterceptor();
MockInterceptor interceptor2 = new MockInterceptor() {
@Override
public void onInbound(InboundInterceptorContext context) throws Exception {
requests.add(context.request());
context.replyWith(response);
}
};
MockInterceptor interceptor3 = new MockInterceptor();
interceptors.add(interceptor1);
interceptors.add(interceptor2);
interceptors.add(interceptor3);
EmbeddedChannel channel = new EmbeddedChannel(
new ChannelDuplexHandler() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
InterceptorChain chain = new InterceptorChain(ctx, interceptors, (ResourceRequest) msg);
chain.fireInbound();
}
}
);
channel.writeInbound(request);
ResourceRequest endRequest = (ResourceRequest) channel.readInbound();
Object endResponse = channel.readOutbound();
assertThat(endRequest).isNull();
assertThat(interceptor1.requests()).hasSize(1);
assertThat(interceptor1.requests()).contains(request);
assertThat(interceptor2.requests()).hasSize(1);
assertThat(interceptor2.requests()).contains(request);
assertThat(interceptor3.requests()).isEmpty();
assertThat(interceptor3.responses()).isEmpty();
assertThat(interceptor2.responses()).isEmpty();
assertThat(interceptor1.responses()).hasSize(1);
assertThat(interceptor1.responses()).contains(response);
}