[Twisted-Python] How to make a custom transport based on sendmsg/recvmsg?

Drew Smathers drew.smathers at gmail.com
Fri Feb 22 16:35:52 EST 2008


On Fri, Feb 22, 2008 at 2:54 PM, Philippe Frycia <phil-twisted at tuxeo.org> wrote:
> Hi,
>
>  I would like to make a custom protocol, based on Unix sockets, that is
>  able to send and receive file descriptors in addition to regular
>  "stream" data.

You should not make assumptions about the transport layer in your
protocol implementation - be it Unix domain sockets, TCP, etc.

>  At the low level, I can do this with a patched version of eunuchs, that
>  adds sendmsg() and recvmsg() functionality to python.
>
>  I have tried to find my way in the Twisted API documentation and in the
>  sources, and as far as I found out, I have to override the doWrite()
>  method of the abstract.FileDescriptor class, and the doRead() method of
>  one of its subclasses.
>
>
>  The only way I have found until now is to do this in the Protocol
>  subclass like this:
>
>  class TLVProtocol(Protocol):
>     def doWrite(self):
>         if len(self.tlvstosend)==0:
>             self.transport.stopWriting()
>             return 0
>         tlv=self.tlvstosend.pop(0)
>         (files,ancillary)=tlv.encode()
>         sendmsg(self.transport.socket.fileno(), data, ancillary=ancillary)
>

If you don't see an attribute documented in the interface for
something (ITransport here for example), and you're referencing it on
the implementation, then you're most definitely doing something bad.
The attribute I'm talking about here is "socket."

>     def doRead(self):
>         (data, dummy, dummy, ancillary) =
>  recvmsg(self.transport.socket.fileno())
>         if len(data)==0:
>             return 0
>         tlv=encode(data, ancillary)
>         .... # process tlv object
>
>     def connectionMade(self):
>         self.tlvstosend=[]
>         self.transport.doWrite=self.doWrite
>         self.transport.doRead=self.doRead
>         ...
>
>     def sendtlv(self, tlv):
>         self.tlvstosend.append(tlv)
>         self.transport.startWriting()
>
>
>
>  Some of the return values are not completed yet (EOF, ...).
>  Am I completely on the wrong track here ?

It sounds like you're trying to do something relatively low-level
(interfacing with eunuchs directly) at a high-level using Protocols.
So to do something that isn't just a hack, you'll need to do a lot
more work - and it might not be worth the effort.  Can you describe
first why you need to use sendmsg/recvmsg in your application?

Cheers,
-- 
\\\\\/\"/\\\\\\\\\\\
\\\\/ // //\/\\\\\\\
\\\/  \\// /\ \/\\\\
\\/ /\/ / /\/ /\ \\\
\/ / /\/ /\  /\\\ \\
/ /\\\  /\\\ \\\\\/\
\/\\\\\/\\\\\/\\\\\\
               d.p.s




More information about the Twisted-Python mailing list