package cn.estsh.i3plus.ext.mes.pcn.apiservice.util.reader;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

/**
 * @author ns
 * @create 2021/11/29 0029 下午 15:18
 */
public class ResponseHandler extends
        SimpleChannelInboundHandler<DatagramPacket> {


        @Override
        protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)
                throws Exception {
            //获得应答，DatagramPacket提供了content()方法取得报文的实际内容

            String response = msg.content().toString(CharsetUtil.UTF_8);
            System.out.println("reponse:");
            byte[] bytes = new byte[msg.content().readableBytes()];
            msg.content().getBytes(0,bytes);
            System.out.println(ReaderUtil.bytes2HexString(bytes));

//            if (response.startsWith(UdpAnswerSide.ANSWER)) {
//                System.out.println(response);
//                ctx.close();
//            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                throws Exception {
            cause.printStackTrace();
            ctx.close();
        }

}
