Request: provide plugin point for custom socket factory in OIO

chudak meadandale at gmail.com
Mon Jun 27 14:44:52 EDT 2011


Some context history...

We have a communication protocol that we've built on top of Netty and allows
us to use both Android and Standard JDK clients to connect to our server
application. We've been using NIO channels since our migration from MINA but
there are several show stopper bugs in Harmony that is causing us issues in
using NIO. Apparently The SSLEngine and related classes as well as some of
the NIO code in Harmony have bugs that have long gone ignored --most likely
because the primary user of Harmony is Android and most Android users aren't
using NIO or the core security classes but rather are using HTTP connections
and sockets directly (both with and without SSL).

We want to switch to OIO so that we can use the more well tested and less
buggy socket implementation in Harmony. Unfortunately, we need to use SSL
and there is no plugin point in the OIO classes to customize the way sockets
are created (in my case, via a configured SSLSocketFactory).

In the OioClientSocketChannelFactory the OioClientSocketChannel is
constructed using:

    public SocketChannel newChannel(ChannelPipeline pipeline) {
        return new OioClientSocketChannel(this, pipeline, sink);
    }

In the OioClientSocketChannel a plain socket is constructed and passed
directly to the parent class in the constructor:

    OioClientSocketChannel(
            ChannelFactory factory,
            ChannelPipeline pipeline,
            ChannelSink sink) {

        super(null, factory, pipeline, sink, new Socket());

        fireChannelOpen(this);
    }

I'd like to propose making the following changes:

1) Support passing a SocketFactory in to the OioClientSocketChannelFactory
constructor (either by changing the current constructor or adding another
one:



    private final SocketFactory socketFactory;

    .....

    public OioClientSocketChannelFactory(Executor workerExecutor,
SocketFactory socketFactory) {
        if (workerExecutor == null) {
            throw new NullPointerException("workerExecutor");
        }
        this.workerExecutor = workerExecutor;
        this.socketFactory = socketFactory;
        sink = new OioClientSocketPipelineSink(workerExecutor);
    }

If you add a new constructor the old one can just construct a default
factory (SocketFactory.getDefault()).

2) Support passing a SocketFactory into the OioClientSocketChannel:

    OioClientSocketChannel(
            ChannelFactory factory,
            ChannelPipeline pipeline,
            ChannelSink sink,
            SocketFactory socketFactory) {

        super(null, factory, pipeline, sink, socketFactory);

        fireChannelOpen(this);
    }
You can add a new constructor and have the old one just pass the default
socket factory (per above) into the OioSocketChannel

3) Support passing the socket factory into the OioSocketChannel (sample
implementation):

    OioSocketChannel(Channel parent, ChannelFactory factory, ChannelPipeline
pipeline, ChannelSink sink, SocketFactory socketFactory)
    {
        super(parent, factory, pipeline, sink);
        
        try
        {
            this.socket = socketFactory.createSocket();
        }
        catch (Exception e)
        {
            throw new RuntimeException("Unable to create socket", e);
        }
        config = new DefaultSocketChannelConfig(socket);
    }

You can add a new constructor that takes a SocketFactory and constructs the
socket (as above) or allow the user to pass the socket directly in (the
current behavior) to maintain compatability.

There was no easy way for me to do any of this except to pull in all of the
above classes into my code base and rename them since the core classes are
all package private and inaccessible. I'd prefer that I not have to maintain
Netty code locally and it's a rather simple change. Depending on how much
you want to maintain backwards compatibility it would be fairly easy to add
this via new constructors and defaults (e.g. get the current behavior by
just using the default socket factory) to the existing OIO classes.

Thanks,

Charles


--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Request-provide-plugin-point-for-custom-socket-factory-in-OIO-tp6521792p6521792.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list