com.github.fartherp.framework.remoting.netty.NettyEncoder Maven / Gradle / Ivy
/*
* Copyright (c) 2017. CK. All rights reserved.
*/
package com.github.fartherp.framework.remoting.netty;
import com.github.fartherp.framework.remoting.common.RemotingHelper;
import com.github.fartherp.framework.remoting.common.RemotingUtil;
import com.github.fartherp.framework.remoting.protocol.RemotingCommand;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
/**
* @author hyssop
*/
public class NettyEncoder extends MessageToByteEncoder {
private static final Logger log = LoggerFactory.getLogger(RemotingHelper.Framework_REMOTING);
@Override
public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out)
throws Exception {
try {
ByteBuffer header = remotingCommand.encodeHeader();
out.writeBytes(header);
byte[] body = remotingCommand.getBody();
if (body != null) {
out.writeBytes(body);
}
} catch (Exception e) {
log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e);
if (remotingCommand != null) {
log.error(remotingCommand.toString());
}
RemotingUtil.closeChannel(ctx.channel());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy