Search in sources :

Example 1 with PingHandler

use of com.github.jvm.io.protocol.c2d.heart.PingHandler 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

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