Search in sources :

Example 6 with ObjectEncoder

use of io.netty.handler.codec.serialization.ObjectEncoder in project netty by netty.

the class ObjectEchoClient method main.

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                }
                p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoClientHandler());
            }
        });
        // Start the connection attempt.
        b.connect(HOST, PORT).sync().channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 7 with ObjectEncoder

use of io.netty.handler.codec.serialization.ObjectEncoder in project netty by netty.

the class SocketObjectEchoTest method testObjectEcho.

private static void testObjectEcho(ServerBootstrap sb, Bootstrap cb, boolean autoRead) throws Throwable {
    sb.childOption(ChannelOption.AUTO_READ, autoRead);
    cb.option(ChannelOption.AUTO_READ, autoRead);
    final EchoHandler sh = new EchoHandler(autoRead);
    final EchoHandler ch = new EchoHandler(autoRead);
    sb.childHandler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            sch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())), new ObjectEncoder(), sh);
        }
    });
    cb.handler(new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel sch) throws Exception {
            sch.pipeline().addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())), new ObjectEncoder(), ch);
        }
    });
    Channel sc = sb.bind().sync().channel();
    Channel cc = cb.connect(sc.localAddress()).sync().channel();
    for (String element : data) {
        cc.writeAndFlush(element);
    }
    while (ch.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    while (sh.counter < data.length) {
        if (sh.exception.get() != null) {
            break;
        }
        if (ch.exception.get() != null) {
            break;
        }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
        // Ignore.
        }
    }
    sh.channel.close().sync();
    ch.channel.close().sync();
    sc.close().sync();
    if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) {
        throw ch.exception.get();
    }
    if (sh.exception.get() != null) {
        throw sh.exception.get();
    }
    if (ch.exception.get() != null) {
        throw ch.exception.get();
    }
}
Also used : ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) Channel(io.netty.channel.Channel) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) IOException(java.io.IOException) IOException(java.io.IOException)

Example 8 with ObjectEncoder

use of io.netty.handler.codec.serialization.ObjectEncoder in project lightconf by haifeiWu.

the class ClientInitializer method initChannel.

@Override
protected void initChannel(Channel channel) throws Exception {
    // IdleStateHandler检测心跳.
    ChannelPipeline p = channel.pipeline();
    p.addLast(new IdleStateHandler(20, 10, 0));
    // p.addLast(new MessageDecoder());
    // p.addLast(new MessageEncoder());
    p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
    p.addLast(new ObjectEncoder());
    p.addLast(new ClientHandler());
}
Also used : ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 9 with ObjectEncoder

use of io.netty.handler.codec.serialization.ObjectEncoder in project lightconf by haifeiWu.

the class ServerInitializer method initChannel.

@Override
protected void initChannel(Channel channel) throws Exception {
    ChannelPipeline p = channel.pipeline();
    // p.addLast(new MessageEncoder());
    // p.addLast(new MessageDecoder());
    p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
    p.addLast(new ObjectEncoder());
    p.addLast(new ServerHandler());
}
Also used : ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 10 with ObjectEncoder

use of io.netty.handler.codec.serialization.ObjectEncoder in project lightconf by haifeiWu.

the class LightConfServerBootstrap method bind.

private void bind() throws InterruptedException {
    EventLoopGroup boss = new NioEventLoopGroup();
    EventLoopGroup worker = new NioEventLoopGroup();
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(boss, worker).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            ChannelPipeline p = socketChannel.pipeline();
            p.addLast(new ObjectEncoder());
            p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
            p.addLast(new ServerHandler());
        }
    });
    ChannelFuture f = bootstrap.bind(port).sync();
    if (f.isSuccess()) {
        log.info(String.format(">>>>>>>>>>>> ligthconf server started, port:%s", port));
    }
}
Also used : NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ObjectDecoder(io.netty.handler.codec.serialization.ObjectDecoder) ObjectEncoder(io.netty.handler.codec.serialization.ObjectEncoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Aggregations

ObjectDecoder (io.netty.handler.codec.serialization.ObjectDecoder)10 ObjectEncoder (io.netty.handler.codec.serialization.ObjectEncoder)10 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)7 SocketChannel (io.netty.channel.socket.SocketChannel)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)4 LoggingHandler (io.netty.handler.logging.LoggingHandler)4 Bootstrap (io.netty.bootstrap.Bootstrap)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 ChannelFuture (io.netty.channel.ChannelFuture)2 SslContext (io.netty.handler.ssl.SslContext)2 Channel (io.netty.channel.Channel)1 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 IOException (java.io.IOException)1