package com.huangzj.cross.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.cross.cmd.CrossGameCommandRegistry;
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 CrossTcpChannelInitializer extends ChannelInitializer<SocketChannel> {

    private final CrossGameCommandRegistry crossGameCommandRegistry;
    private final ProtoRegistry protoRegistry;

    @Override
    protected void initChannel(SocketChannel ch) {
        ch.pipeline().addLast(LengthPrefixedProtobuf.newTcpFrameDecoder());
        ch.pipeline()
                .addLast(
                        new CmdRpcTcpFrameHandler(
                                crossGameCommandRegistry, null, CommandSource.CROSS_TCP, protoRegistry));
    }
}
