All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.prettypb.rpc.client.ProtobufChannelInitializer Maven / Gradle / Ivy

There is a newer version: 0.3.5
Show newest version
package net.dongliu.prettypb.rpc.client;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.ssl.SslHandler;
import net.dongliu.prettypb.rpc.coder.ProtobufDecoder;
import net.dongliu.prettypb.rpc.coder.ProtobufEncoder;
import net.dongliu.prettypb.rpc.coder.VarIntFrameDecoder;
import net.dongliu.prettypb.rpc.coder.VarIntLengthFieldPrepender;
import net.dongliu.prettypb.rpc.protocol.WirePayload;
import net.dongliu.prettypb.rpc.utils.RpcSSLContext;
import net.dongliu.prettypb.rpc.utils.Handlers;
import net.dongliu.prettypb.runtime.ExtensionRegistry;

/**
 * @author Dong Liu
 */
public class ProtobufChannelInitializer extends ChannelInitializer {

    private RpcSSLContext sslContext;
    /**
     * extension registry for wire payload
     */
    private ExtensionRegistry wpExtensionRegistry;

    /**
     * @param sslContext        the ssl context. null if not use ssl
     * @param wpExtensionRegistry wirePayload extension. not if not use
     */
    public ProtobufChannelInitializer(RpcSSLContext sslContext,
                                      ExtensionRegistry wpExtensionRegistry) {
        this.sslContext = sslContext;
        this.wpExtensionRegistry = wpExtensionRegistry;
    }

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

        if (sslContext != null) {
            p.addLast(Handlers.SSL, new SslHandler(sslContext.createClientEngine()));
        }
        p.addLast(Handlers.FRAME_DECODER, new VarIntFrameDecoder());
        p.addLast(Handlers.PROTOBUF_DECODER, new ProtobufDecoder(WirePayload.class,
                wpExtensionRegistry));

        p.addLast(Handlers.FRAME_ENCODER, new VarIntLengthFieldPrepender());
        p.addLast(Handlers.PROTOBUF_ENCODER, new ProtobufEncoder());
        // the connectResponseHandler is swapped after the client connection
        // handshake with the RpcClient for the Channel
        p.addLast(Handlers.CLIENT_CONNECT, new ClientConnectResponseHandler());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy