package com.hh.common.codec;
import java.net.InetSocketAddress;
import com.hh.common.server.UdpServer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class UdpDecoder extends SimpleChannelInboundHandler<DatagramPacket>{
private InetSocketAddress inetAddress;
private Channel ch ;
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg)
throws Exception {
// 接受client的消息
// log.info("开始接收来自client的数据");
final ByteBuf buf = msg.content();
int readableBytes = buf.readableBytes();
byte[] content = new byte[readableBytes];
buf.readBytes(content);
String clientMessage = new String(content,"GBK");
if(clientMessage.contains("UDP_MAIN_CHANNEL_CLIENT")){
inetAddress=msg.sender();
}else{
ctx.writeAndFlush(new DatagramPacket(Unpooled.wrappedBuffer(content),this.inetAddress));
}
log.info("msg recv :{}",clientMessage);
// ch.writeAndFlush(new DatagramPacket(Unpooled.wrappedBuffer(("channel"+ch.id()+":helloClient").getBytes()),msg.sender()));
}
public UdpDecoder() {
super();
}
public UdpDecoder(Channel ch,InetSocketAddress inetAddress) {
super();
this.ch=ch;
this.inetAddress = inetAddress;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
super.exceptionCaught(ctx, cause);
}
// /**
// * 发送数据包并监听结果
// * @param datagramPacket
// */
// public static void senderInternal(final DatagramPacket datagramPacket,List<Channel> channelList) {
// for (Channel channel : NettyTCPClient.channelList) {
// if(channel != null){
// channel.writeAndFlush(datagramPacket).addListener(new GenericFutureListener<ChannelFuture>() {
// @Override
// public void operationComplete(ChannelFuture future)
// throws Exception {
// boolean success = future.isSuccess();
// if(logger.isInfoEnabled()){
// logger.info("Sender datagramPacket result : "+success);
// }
// }
// });
// }
// }
// }
//
// /**
// * 组装数据包
// * @param msg 消息串
// * @param inetSocketAddress 服务器地址
// * @return DatagramPacket
// */
// private static DatagramPacket datagramPacket(String msg, InetSocketAddress inetSocketAddress){
// ByteBuf dataBuf = Unpooled.copiedBuffer(msg,Charset.forName("UTF-8"));
// DatagramPacket datagramPacket = new DatagramPacket(dataBuf, inetSocketAddress);
// return datagramPacket;
// }
//
// /**
// * 发送数据包服务器无返回结果
// * @param datagramPacket
// */
// private static void senderInternal(final DatagramPacket datagramPacket) {
// logger.info("LogPushUdpClient.channel"+LogPushUdpClient.channel);
// if(LogPushUdpClient.channel != null){
// LogPushUdpClient.channel.writeAndFlush(datagramPacket).addListener(new GenericFutureListener<ChannelFuture>() {
// @Override
// public void operationComplete(ChannelFuture future)
// throws Exception {
// boolean success = future.isSuccess();
// if(logger.isInfoEnabled()){
// logger.info("Sender datagramPacket result : "+success);
// }
// }
// });
// }else{
// throw new NullPointerException("channel is null");
// }
// }
}