Example usage for io.netty.handler.codec DelimiterBasedFrameDecoder DelimiterBasedFrameDecoder

List of usage examples for io.netty.handler.codec DelimiterBasedFrameDecoder DelimiterBasedFrameDecoder

Introduction

In this page you can find the example usage for io.netty.handler.codec DelimiterBasedFrameDecoder DelimiterBasedFrameDecoder.

Prototype

public DelimiterBasedFrameDecoder(int maxFrameLength, boolean stripDelimiter, boolean failFast,
        ByteBuf... delimiters) 

Source Link

Document

Creates a new instance.

Usage

From source file:chat.viska.xmpp.NettyTcpSession.java

License:Apache License

@SchedulerSupport(SchedulerSupport.CUSTOM)
@Override/*from  www .  j  a  v  a 2s .c o m*/
protected Completable openConnection(final Compression connectionCompression,
        final Compression tlsCompression) {
    final Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(nettyEventLoopGroup);
    bootstrap.channel(NioSocketChannel.class);

    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(final SocketChannel channel) throws SSLException {
            if (getConnection().getTlsMethod() == Connection.TlsMethod.DIRECT) {
                tlsHandler = TLS_CONTEXT_BUILDER.build().newHandler(channel.alloc());
                channel.pipeline().addLast(PIPE_TLS, tlsHandler);
            } else {
                channel.pipeline().addLast(PIPE_TLS, new ChannelDuplexHandler());
            }
            channel.pipeline().addLast(PIPE_COMPRESSOR, new ChannelDuplexHandler());
            channel.pipeline().addLast(new DelimiterBasedFrameDecoder(MAX_STANZA_SIZE_BYTE, false, false,
                    Unpooled.wrappedBuffer(">".getBytes(StandardCharsets.UTF_8))));
            channel.pipeline().addLast(new StreamClosingDetector());
            channel.pipeline().addLast(PIPE_DECODER_XML, new XmlDecoder());
            channel.pipeline().addLast(new SimpleChannelInboundHandler<XmlDocumentStart>() {
                @Override
                protected void channelRead0(final ChannelHandlerContext ctx, final XmlDocumentStart msg)
                        throws Exception {
                    if (!"UTF-8".equalsIgnoreCase(msg.encoding())) {
                        sendError(new StreamErrorException(StreamErrorException.Condition.UNSUPPORTED_ENCODING,
                                "Only UTF-8 is supported in XMPP stream."));
                    }
                }
            });
            channel.pipeline().addLast(new StreamOpeningDetector());
            channel.pipeline().addLast(new XmlFrameDecoder(MAX_STANZA_SIZE_BYTE));
            channel.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
            channel.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                @Override
                protected void channelRead0(final ChannelHandlerContext ctx, final String msg)
                        throws Exception {
                    try {
                        feedXmlPipeline(preprocessInboundXml(msg));
                    } catch (SAXException ex) {
                        sendError(new StreamErrorException(StreamErrorException.Condition.BAD_FORMAT));
                    }
                }
            });
            channel.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
            channel.pipeline().addLast(new SimpleChannelInboundHandler<Object>() {
                @Override
                protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) {
                }

                @Override
                public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
                    if (cause instanceof Exception) {
                        triggerEvent(new ExceptionCaughtEvent(NettyTcpSession.this, (Exception) cause));
                    } else {
                        throw new RuntimeException(cause);
                    }
                }
            });
        }
    });

    final ChannelFuture channelFuture = bootstrap.connect(getConnection().getDomain(),
            getConnection().getPort());
    return Completable.fromFuture(channelFuture).andThen(Completable.fromAction(() -> {
        this.nettyChannel = (SocketChannel) channelFuture.channel();
        nettyChannel.closeFuture().addListener(it -> changeStateToDisconnected());
    }));
}

From source file:org.maodian.flyingcat.xmpp.XmppServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();/*  w  ww.  jav a 2s.c o m*/

    String delimiter = ">";// XmppMessageInboundHandler.STREAM_NAME + ">";
    p.addLast("Frame", new DelimiterBasedFrameDecoder(8192, false, true,
            Unpooled.wrappedBuffer(delimiter.getBytes(StandardCharsets.UTF_8))));
    p.addLast("Logger", DECODER);
    p.addLast("XmlFragment", new XMLFragmentDecoder());
    p.addLast("Encoder", ENCODER);
    // p.addLast("Echo", new XmppMessageInboundHandler());
    // p.addLast("Echo", new StreamNegotiationHandler());
    XmppXMLStreamHandler streamHandler = beanFactory.getBean(XmppXMLStreamHandler.class);
    p.addLast("Echo", streamHandler);
}