Search in sources :

Example 1 with C2DHessianMsgEncoder

use of com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder in project jdepth by Crab2died.

the class C2DServer method bind.

private void bind(String host, int port) throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new C2DHessianMsgDecoder(1024 * 1024, 4, 4, -8, 0)).addLast("MessageEncoder", new C2DHessianMsgEncoder()).addLast("ReadTimeoutHandler", new ReadTimeoutHandler(TIME_OUT)).addLast("LoginAuthResp", new LoginAuthRespHandler()).addLast("Pong", new PongHandler());
            }
        });
        ChannelFuture future = bootstrap.bind(host, port).sync();
        future.channel().closeFuture().sync();
        logger.info("server is started");
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PongHandler(com.github.jvm.io.protocol.c2d.heart.PongHandler) C2DHessianMsgDecoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgDecoder) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) C2DHessianMsgEncoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) LoginAuthRespHandler(com.github.jvm.io.protocol.c2d.auth.LoginAuthRespHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 2 with C2DHessianMsgEncoder

use of com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder in project jdepth by Crab2died.

the class C2DClient method connect.

public void connect(String remoteHost, int remotePort, String localeHost, int localPort) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new LoggingHandler(LogLevel.WARN)).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new C2DHessianMsgDecoder(1024 * 1024, 4, 4, -8, 0)).addLast("MessageEncoder", new C2DHessianMsgEncoder()).addLast("ReadTimeoutHandler", new ReadTimeoutHandler(TIME_OUT)).addLast("LoginAuthReq", new LoginAuthReqHandler()).addLast("Ping", new PingHandler());
            }
        });
        // 发起异步连接操作
        ChannelFuture future = bootstrap.connect(new InetSocketAddress(remoteHost, remotePort), new InetSocketAddress(localeHost, localPort)).sync();
        // 当对应的channel关闭的时候,就会返回对应的channel。
        future.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
        // 所有资源释放完成之后,清空资源,再次发起重连操作
        executor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                try {
                    // 发起重连操作
                    connect(REMOTE_HOST, REMOTE_PORT, LOCAL_HOST, LOCAL_PORT);
                } catch (Exception e) {
                    logger.error("reconnect error", e);
                }
            } catch (InterruptedException e) {
                logger.error("InterruptedException", e);
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) PingHandler(com.github.jvm.io.protocol.c2d.heart.PingHandler) InetSocketAddress(java.net.InetSocketAddress) C2DHessianMsgDecoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgDecoder) C2DHessianMsgEncoder(com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder) LoginAuthReqHandler(com.github.jvm.io.protocol.c2d.auth.LoginAuthReqHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

C2DHessianMsgDecoder (com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgDecoder)2 C2DHessianMsgEncoder (com.github.jvm.io.protocol.c2d.codc.hessian.C2DHessianMsgEncoder)2 ChannelFuture (io.netty.channel.ChannelFuture)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)2 LoginAuthReqHandler (com.github.jvm.io.protocol.c2d.auth.LoginAuthReqHandler)1 LoginAuthRespHandler (com.github.jvm.io.protocol.c2d.auth.LoginAuthRespHandler)1 PingHandler (com.github.jvm.io.protocol.c2d.heart.PingHandler)1 PongHandler (com.github.jvm.io.protocol.c2d.heart.PongHandler)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 InetSocketAddress (java.net.InetSocketAddress)1