List of usage examples for io.netty.channel CombinedChannelDuplexHandler CombinedChannelDuplexHandler
public CombinedChannelDuplexHandler(I inboundHandler, O outboundHandler)
From source file:com.spotify.netty4.handler.codec.zmtp.ZMTPCodec.java
License:Apache License
@Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception { // Discard input if handshake failed. It is expected that the user will close the channel. if (session.handshakeFuture().isDone()) { assert !session.handshakeFuture().isSuccess(); in.skipBytes(in.readableBytes()); }// w w w . j a v a 2 s . c o m // Shake hands final ZMTPHandshake handshake; try { handshake = handshaker.handshake(in, ctx); if (handshake == null) { // Handshake is not yet done. Await more input. return; } } catch (Exception e) { session.handshakeFailure(e); ctx.fireUserEventTriggered(new ZMTPHandshakeFailure(session)); throw e; } // Handshake is done. session.handshakeSuccess(handshake); // Replace this handler with the framing encoder and decoder if (actualReadableBytes() > 0) { out.add(in.readBytes(actualReadableBytes())); } final ZMTPDecoder decoder = config.decoder().decoder(session); final ZMTPEncoder encoder = config.encoder().encoder(session); final ZMTPWireFormat wireFormat = ZMTPWireFormats.wireFormat(session.negotiatedVersion()); final ChannelHandler handler = new CombinedChannelDuplexHandler<ZMTPFramingDecoder, ZMTPFramingEncoder>( new ZMTPFramingDecoder(wireFormat, decoder), new ZMTPFramingEncoder(wireFormat, encoder)); ctx.pipeline().replace(this, ctx.name(), handler); // Tell the user that the handshake is complete ctx.fireUserEventTriggered(new ZMTPHandshakeSuccess(session, handshake)); }