package com.netty;

import com.haobang.config.AppConfig;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.DatagramChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;

/**
 * UDP 通道初始化器
 */
public class SyslogUdpChannelInitializer extends ChannelInitializer<DatagramChannel> {
    private static final Charset SYSLOG_CHARSET = AppConfig.getSyslogCharset();

    @Override
    protected void initChannel(DatagramChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        // 添加字符串解码器和编码器（使用可配置的字符集）
        pipeline.addLast("decoder", new StringDecoder(SYSLOG_CHARSET));
        pipeline.addLast("encoder", new StringEncoder(SYSLOG_CHARSET));

        // 添加业务处理器
        pipeline.addLast("handler", new SyslogMessageHandler());
    }
}