Example usage for io.netty.handler.codec.serialization ObjectDecoder ObjectDecoder

List of usage examples for io.netty.handler.codec.serialization ObjectDecoder ObjectDecoder

Introduction

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

Prototype

public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) 

Source Link

Document

Creates a new decoder with the specified maximum object size.

Usage

From source file:org.betawares.jorre.Server.java

License:Open Source License

/**
 * Starts the Server with the specified {@link Connection} settings.
 * //  ww w. j  a  v a2 s . co m
 * @param connection  a {@link Connection} instance specifying the connection settings
 * 
 * @throws Exception  thrown if there is an error starting the server
 */
public void start(Connection connection) throws Exception {

    SslContext sslCtx;

    if (connection.isSSL()) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) {
                    if (sslCtx != null) {
                        ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
                    }
                    ch.pipeline()
                            .addLast(new ObjectDecoder(10 * 1024 * 1024, ClassResolvers.cacheDisabled(null)));
                    ch.pipeline().addLast(encoder);
                    ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(connection.getIdleTimeout(),
                            connection.getIdlePingTime(), 0, TimeUnit.MILLISECONDS));
                    ch.pipeline().addLast(handlersExecutor, "heartbeatHandler",
                            new ServerHeartbeatHandler(Server.this));
                    ch.pipeline().addLast("pingMessageHandler", pingMessageHandler);
                    ch.pipeline().addLast("pongMessageHandler", pongMessageHandler);

                    ch.pipeline().addLast("connectionHandler", new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            clients.add(ctx.channel());
                            ctx.pipeline().remove(this);
                            super.channelActive(ctx);
                        }
                    });
                    ch.pipeline().addLast(handlersExecutor, "serverMessageHandler", serverRequestHandler);
                    ch.pipeline().addLast("exceptionHandler", exceptionHandler);
                }
            });
    bootstrap.bind(connection.getPort()).sync();

}

From source file:subterranean.crimson.permajar.stage1.network.Communications.java

License:Open Source License

public static synchronized boolean connect(final String HOST, final int PORT, final boolean SSL) {
    if (SSL) {/*from w w  w.j  a v a2 s. c  o m*/
        Logger.add("Making SSL Connection attempt");
    } else {
        Logger.add("Making Connection attempt");
    }

    handler = new Handler();

    if (SSL) {
        try {
            sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
        } catch (SSLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();

    Bootstrap b = new Bootstrap();
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Environment.reportTimeout);
    b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) {

            if (sslCtx != null) {
                ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
            }
            ch.pipeline().addLast(new ObjectEncoder(),
                    new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)), handler);

        }
    });

    ChannelFuture future = b.connect(new InetSocketAddress(HOST, PORT));
    if (!future.awaitUninterruptibly().isSuccess()) {
        group.shutdownGracefully();
        return false;
    }

    c = future.channel();

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return c.isWritable();
}

From source file:subterranean.crimson.server.network.ClientListener.java

License:Open Source License

@Override
public void run() {
    if (UPNP) {// www.  ja v  a 2s.c  o m
        Logger.add("Attempting to automatically forward the port");
        try {
            PortMapper.forward(PORT);
            Logger.add("Port mapping Success!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Logger.add("Port mapping failed!");
        }
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(final SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast("SSL Handler", sslCtx.newHandler(ch.alloc()));
                        }
                        final ServerHandler sh = new ServerHandler();
                        final ChannelTrafficShapingHandler ctsh = new ChannelTrafficShapingHandler(300);
                        p.addLast(ctsh, new ObjectEncoder(),
                                new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)), sh);
                        // spawn a new connection object
                        new Thread(new Runnable() {
                            public void run() {
                                Connection c = new Connection(key, encryption, sh, ctsh, ch.remoteAddress());
                                c.handshake();
                            }
                        }).start();

                    }
                });

        // Bind and start to accept incoming connections.
        b.bind(PORT).sync().channel().closeFuture().sync();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();

        if (UPNP) {
            Logger.add("Attempting to remove port mapping");
            try {
                PortMapper.unforward(PORT);
                Logger.add("Port mapping removed");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Logger.add("Failed to remove port mapping");
            }
        }
    }

}