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

Philippe Frycia phil-twisted at tuxeo.org
Fri Feb 22 14:54:30 EST 2008


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.
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)

    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 ?
If not, what other methods of self.transport do I need to override ?
If yes, what is the correct way to do this ?

Thanks.





More information about the Twisted-Python mailing list