All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.godmao.netty.client.base.ClientBaseHandler Maven / Gradle / Ivy

There is a newer version: 2.3.4.7.Beat
Show newest version
package cn.godmao.netty.client.base;

import cn.godmao.netty.handler.IChannelHandler;
import cn.godmao.netty.handler.IConnect;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;

public class ClientBaseHandler extends ChannelInboundHandlerAdapter implements IChannelHandler {
    private final Logger   log = LoggerFactory.getLogger(this.getClass());
    private final IConnect connect;

    public ClientBaseHandler(IConnect connect) {
        this.connect = connect;
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        connect.getChannelService().bind(channel, channel.id().asLongText());
        connect.getChannelService().submit(ctx, () -> connect.getConnectHandler().onOpen(ctx)).addListener(future -> {
            if (!future.isSuccess()) exceptionCaught(ctx, future.cause());
        });
        super.channelActive(ctx);
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        connect.getChannelService().submit(ctx, () -> connect.getConnectHandler().onError(ctx, cause)).addListener(future -> {
            if (!future.isSuccess()) future.cause().printStackTrace();
        });
    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        connect.getChannelService().submit(ctx, () -> connect.getConnectHandler().onClose(ctx)).addListener(future -> {
            if (!future.isSuccess()) exceptionCaught(ctx, future.cause());
        });
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        connect.getChannelService().submit(ctx, () -> connect.getConnectHandler().onMessage(ctx, msg)).addListener(future -> {
            if (!future.isSuccess()) exceptionCaught(ctx, future.cause());
        });
        super.channelRead(ctx, msg);
    }


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT.equals(evt)) {
            log.debug("FIRST_ALL_IDLE_STATE_EVENT {}", evt);
//                NettyUtil.close(ctx.channel());
        } else if (IdleStateEvent.WRITER_IDLE_STATE_EVENT.equals(evt)) {
            log.debug("WRITER_IDLE_STATE_EVENT {} {}", LocalDateTime.now(), evt);
        } else if (IdleStateEvent.ALL_IDLE_STATE_EVENT.equals(evt)) {
            log.debug("ALL_IDLE_STATE_EVENT {}", evt);
        } else if (IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT.equals(evt)) {
            log.debug("FIRST_READER_IDLE_STATE_EVENT {}", evt);
        } else if (IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT.equals(evt)) {
            log.debug("FIRST_WRITER_IDLE_STATE_EVENT {}", evt);
        } else if (IdleStateEvent.READER_IDLE_STATE_EVENT.equals(evt)) {
            log.debug("READER_IDLE_STATE_EVENT {}", evt);
        } else {
            log.debug("IDLE_STATE_EVENT {}", evt);
        }
        super.userEventTriggered(ctx, evt);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy