package com.its.op.xnettcp.client.codec;

import com.its.utils.NettyUtils;
import com.its.utils.SysUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
public class NettyTcpClientEncoder extends MessageToByteEncoder<Object> {

    @Override
    protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf outByteBuf) {

        String ipAddress = NettyUtils.getRemoteIpAddress(ctx.channel());
        //Channel channel = ctx.channel();

        ByteBuffer buffer = (ByteBuffer)msg;
        byte[] sendBytes = buffer.array();
        outByteBuf.writeBytes(sendBytes);
        log.info("SEND: [{}], {} Bytes. {}", ipAddress, sendBytes.length, SysUtils.byteArrayToHex(sendBytes));
    }
}
