package cn.component.message.udp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.springframework.stereotype.Component;
import cn.util.core.LogKit;
import java.util.Arrays;

/**
 * @Author BJTU
 * @Deprecated UDP组播服务器处理消息
 */
@Component
public class GroupServerHandler extends ChannelInitializer<NioDatagramChannel> 
{
	@Override
    public void initChannel(NioDatagramChannel ch) {
        ChannelPipeline addLast = ch.pipeline().addLast();
        addLast.addLast(new SimpleChannelInboundHandler<DatagramPacket>() {//接收信息处理

            @Override
            protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
                ByteBuf byteBuf = msg.content();
                byte[] bytes = new byte[byteBuf.readableBytes()];
                byteBuf.readBytes(bytes);
                LogKit.info("GroupServer接收到消息:" + msg.sender()+"->" + Arrays.toString(bytes));
            }
        });
    }
}
