package com.fr.tx.common.netty.eqt;

import org.apache.log4j.Logger;

import com.fr.tx.common.util.ConvertUtil;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

/**
 * 
 * 发送数据handler
 * 
 * @author Administrator
 */
public class TcpServerOutHandler extends ChannelOutboundHandlerAdapter {
    
    /**
     * 日志
     */
    public static final Logger logger = Logger.getLogger(TcpServerOutHandler.class
            .getSimpleName());

    /**
     * 构造函数
     * @param shortName
     */
	public TcpServerOutHandler() {
	}

	/**
     * 向client发送消息
     * 
     * @param ctx 端口
     * @param msg 数据
     * @param promise ChannelPromise
     */
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        String response = (String)msg;
        ByteBuf encoded = ctx.alloc().buffer(4 * response.length());
        encoded.writeBytes(ConvertUtil.HexStrToAscii(response));
        ctx.write(encoded);
        ctx.flush();
        encoded.clear();
    }
}
