Java 类io.netty.handler.codec.haproxy.HAProxyMessageDecoder 实例源码

项目:BungeeProxy    文件:BungeeProxy.java   
@Override
public void onEnable() {
    try {
        Field remoteAddressField = AbstractChannel.class.getDeclaredField("remoteAddress");
        remoteAddressField.setAccessible(true);

        Field serverChild = PipelineUtils.class.getField("SERVER_CHILD");
        serverChild.setAccessible(true);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(serverChild, serverChild.getModifiers() & ~Modifier.FINAL);

        ChannelInitializer<Channel> bungeeChannelInitializer = PipelineUtils.SERVER_CHILD;

        Method initChannelMethod = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
        initChannelMethod.setAccessible(true);

        serverChild.set(null, new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel channel) throws Exception {
                initChannelMethod.invoke(bungeeChannelInitializer, channel);
                channel.pipeline().addAfter(PipelineUtils.TIMEOUT_HANDLER, "haproxy-decoder", new HAProxyMessageDecoder());
                channel.pipeline().addAfter("haproxy-decoder", "haproxy-handler", new ChannelInboundHandlerAdapter() {
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        if (msg instanceof HAProxyMessage) {
                            HAProxyMessage message = (HAProxyMessage) msg;
                            remoteAddressField.set(channel, new InetSocketAddress(message.sourceAddress(), message.sourcePort()));
                        } else {
                            super.channelRead(ctx, msg);
                        }
                    }
                });
            }
        });
    } catch (Exception e) {
        getLogger().log(Level.SEVERE, e.getMessage(), e);
        getProxy().stop();
    }
}