[Twisted-Python] Setting socket options before bind/listen
Tobias Oberstein
tobias.oberstein at tavendo.de
Mon Feb 3 16:15:55 MST 2014
> >Would you mind giving a tip on how to make use of a CustomPort deriving
> >of Port? How to "plug" that into Twisted?
>
> I have two suggestions, one of which I hope you'll like and the other of which
> you might not. :)
Awesome! This is enough hints to get me started.
I already have Twisted endpoint plugins, and I will follow that road, something like
tcpwithopts:tcp:9000:interface=0.0.0.0:reuse-port:true:disable-nagle:true
Subclassing Port / adoptStream: I see;) Not sure if this SO_REUSEPORT works
on Windows - if it does, another aspect then is that adoptStream is Unix-only ..
I will see. Thanks again!
/Tobias
>
> First, endpoints are the intended extension point for this sort of thing now. You
> can write a plugin for the parser so that `serverFromString` will give out server
> endpoints for your port type (giving an appropriate string description).
> Applications won't be able to tell what's going on because the server endpoint
> interface is just `.listen(factory)ยด.
>
> Second, please don't subclass `Port`. It's true there are no underscores
> anywhere in its name (`twisted.internet.tcp.Port`) making it part of Twisted's
> public API. Nevertheless, it's very much a reactor implementation detail. It's a
> mistake that it's public. To compounded this, the exactly interface between a
> class and its subclasses is hazy and gross at best. I'd discourage you (and
> everyone else) from subclassing *most* things in Twisted these days (lots of
> our APIs are still subclassing-oriented so it's not always possible to avoid, of
> course).
>
> In this case, I think you might actually be able to re-use all of the important
> parts of `Port` without subclassing it. You can create a bare-bones
> implementation of `IListeningPort` that creates a socket and sets the flags you
> want on it. Then, use `reactor.adoptStreamPort` to get the reactor to create
> and initialize a new `Port` with your socket.
> This leaves you with a little code to duplicate (basically
> `createInternetSocket`) but a pretty small amount - and the upside is that
> you're totally isolated from the internals of `Port`, from the accidentally-public
> implementation details of `Port`, and even from the implementation detail of
> whether the reactor even *uses* `Port` or not.
> All you rely on is `reactor.adoptStreamPort` which is a nice, documented,
> tested, intentionally-public interface. :)
>
> (For SSL, then you can wrap your own twisted.protocols.tls wrapper around the
> factory - which is all the reactor's listenSSL does these days, anyway).
>
> Hope this helps,
>
> Jean-Paul
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
More information about the Twisted-Python
mailing list