package com.huangzj.server.net;

import com.huangzj.base.cmd.CommandSource;
import com.huangzj.base.cmd.ProtoRegistry;
import com.huangzj.base.frame.LengthPrefixedProtobuf;
import com.huangzj.base.net.CmdRpcTcpFrameHandler;
import com.huangzj.comm.crosslink.CrossTcpClient;
import com.huangzj.server.cmd.ServerCommandRegistry;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 区服客户端 TCP：pipeline 上挂帧解码 + CmdRpc 业务 handler。
 */
@Component
@RequiredArgsConstructor
public class ClientTcpChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final ServerCommandRegistry serverCommandRegistry;
    private final CrossTcpClient crossTcpClient;
    private final ProtoRegistry protoRegistry;

    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline().addLast(LengthPrefixedProtobuf.newTcpFrameDecoder());
        ch.pipeline()
                .addLast(new CmdRpcTcpFrameHandler(
                        serverCommandRegistry,
                        crossTcpClient,
                        CommandSource.Client_TCP,
                        protoRegistry));
    }
}
