Search in sources :

Example 1 with RetCode

use of io.transport.core.protocol.message.internal.ResultRespMessage.RetCode in project transporter by wang4ever.

the class TransportMessageHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (logger.isDebugEnabled())
        logger.debug("Read消息: {}", JSON.toJSONString(msg));
    // 1.1 转发给具体业务方法
    if (msg == null)
        logger.warn("On channelRead(msg) is 'msg' null.");
    else {
        SubmitTaskExecutors.getLimitExecutor().submit(() -> {
            // 结果码
            RetCode retCode = RetCode.OK;
            // 处理消息类型
            String type = null;
            // 异常信息
            String err = null;
            try {
                this.dispatch(ctx, msg);
            } catch (Exception e) {
                err = ExceptionUtils.getRootCauseMessage(e).split("\\:")[1];
                // 1.1 认证异常.
                if (e instanceof TransportAuthenticationException) {
                    // 认证失败消息
                    retCode = RetCode.AUTH_FAIL;
                    type = String.valueOf(MsgType.CONNECT_IN.getActionId());
                    logger.warn("认证失败: {}", err);
                } else {
                    // 系统异常结果码
                    retCode = RetCode.SYS_ERR;
                    logger.error("处理失败.{}", err);
                }
            } finally {
                // 1.3 Echo系统错误消息
                ChannelFuture cf = this.echoWrite(ctx, new ResultRespMessage(retCode, type, err));
                if (retCode == RetCode.AUTH_FAIL) {
                    // 认证失败则关闭通道
                    cf.addListener((ChannelFuture f) -> {
                        if (f.isSuccess())
                            // 返回消息完成后再关闭通道
                            super.closeClient();
                    });
                }
            }
        });
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ResultRespMessage(io.transport.core.protocol.message.internal.ResultRespMessage) RetCode(io.transport.core.protocol.message.internal.ResultRespMessage.RetCode) TransportException(io.transport.core.exception.TransportException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException) TransportAuthenticationException(io.transport.core.exception.TransportAuthenticationException)

Aggregations

ChannelFuture (io.netty.channel.ChannelFuture)1 TransportAuthenticationException (io.transport.core.exception.TransportAuthenticationException)1 TransportException (io.transport.core.exception.TransportException)1 ResultRespMessage (io.transport.core.protocol.message.internal.ResultRespMessage)1 RetCode (io.transport.core.protocol.message.internal.ResultRespMessage.RetCode)1