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, ByteBuf... delimiters) 

Source Link

Document

Creates a new instance.

Usage

From source file:SecureChatServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    pipeline.addLast(sslCtx.newHandler(ch.alloc()));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());

    // and then business logic.
    pipeline.addLast(new SecureChatServerHandler());
}

From source file:TelnetClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();

    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), TelnetClient.HOST, TelnetClient.PORT));
    }/*from   w  w w. ja v a2s . co m*/

    // Add the text line codec combination first,
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(DECODER);
    pipeline.addLast(ENCODER);

    // and then business logic.
    pipeline.addLast(CLIENT_HANDLER);
}

From source file:book.netty.n4delimiterC5P1.EchoClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*from   w w w  . ja v  a  2  s .  co m*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        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 {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:book.netty.n4delimiterC5P1.EchoServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO/*w w  w . j ava2  s.com*/
    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) throws Exception {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:books.netty.frame.delimiter.EchoClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*from www .  jav  a 2 s  .co  m*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        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) {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:books.netty.frame.delimiter.EchoServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//from  w  w w.  ja  va 2s  .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) {
                        ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                        ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:books.netty.ssl.SecureChatClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.

    SSLEngine engine = null;/*w  ww  .  ja  v  a  2 s .c o  m*/
    if (SSLMODE.CA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getClientContext(tlsMode, null,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/client/cChat.jks")
                .createSSLEngine();
    } else if (SSLMODE.CSA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getClientContext(tlsMode,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/cChat.jks",
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/cChat.jks")
                .createSSLEngine();

        // engine = SecureChatSslContextFactory
        // .getClientContext(
        // tlsMode,
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/cChat.jks",
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/cChat.jks")
        // .createSSLEngine();

    } else {
        System.err.println("ERROR : " + tlsMode);
        System.exit(-1);
    }
    engine.setUseClientMode(true);
    pipeline.addLast("ssl", new SslHandler(engine));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    // and then business logic.
    pipeline.addLast("handler", new SecureChatClientHandler());
}

From source file:books.netty.ssl.SecureChatServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    ////w  ww  . ja  v a  2s . co  m
    // Read SecureChatSslContextFactory
    // if you need client certificate authentication.

    SSLEngine engine = null;
    if (SSLMODE.CA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getServerContext(tlsMode,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/client/sChat.jks", null)
                .createSSLEngine();
    } else if (SSLMODE.CSA.toString().equals(tlsMode)) {
        engine = SecureChatSslContextFactory
                .getServerContext(tlsMode,
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/sChat.jks",
                        System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/sChat.jks")
                .createSSLEngine();

        // engine = SecureChatSslContextFactory
        // .getServerContext(
        // tlsMode,
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/sChat.jks",
        // System.getProperty("user.dir")
        // + "/src/com/phei/netty/ssl/conf/client/sChat.jks")
        // .createSSLEngine();
    } else {
        System.err.println("ERROR : " + tlsMode);
        System.exit(-1);
    }
    engine.setUseClientMode(false);

    // Client auth
    if (SSLMODE.CSA.toString().equals(tlsMode))
        engine.setNeedClientAuth(true);
    pipeline.addLast("ssl", new SslHandler(engine));

    // On top of the SSL handler, add the text line codec.
    pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast("decoder", new StringDecoder());
    pipeline.addLast("encoder", new StringEncoder());

    // and then business logic.
    pipeline.addLast("handler", new SecureChatServerHandler());
}

From source file:ca.lambtoncollege.hauntedhouse.client.ClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    if (sslCtx != null)
        pipeline.addLast(sslCtx.newHandler(ch.alloc(), Client.HOST, Client.PORT));
    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());
    // and then business logic.
    pipeline.addLast(new ClientHandler());
}

From source file:ca.lambtoncollege.hauntedhouse.server.ServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // Add SSL handler first to encrypt and decrypt everything.
    // In this example, we use a bogus certificate in the server side
    // and accept any invalid certificates in the client side.
    // You will need something more complicated to identify both
    // and server in the real world.
    if (sslCtx != null)
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    // On top of the SSL handler, add the text line codec.
    pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
    pipeline.addLast(new StringDecoder());
    pipeline.addLast(new StringEncoder());
    // and then business logic.
    pipeline.addLast(new ServerHandler());
}