I’ve almost got it — but I’m missing key points…..
I’m trying to figure out how one might built “layered” protocol stacks in Twisted. I can understand how one attached a single protocol and handles events with Defers, but with if I want the traditional OSI layers model? Assume I have a line-oriented protocol at the bottom on a TCP connector, and I want multiple line-oriented protocols stacked on top until I get to application space.
(Think of me wanted to implement the TCP/IP stack itself in twisted)
The OSI layering model is mostly irrelevant to Twisted (and, I would posit, mostly irrelevant to software in general). Looking at each layer at a time:
IProtocolandITransport. The transport delivers bytes from the transport to anIProtocolviadataReceived, and the application delivers bytes to the transport viaITransport.write(). (This relationship is then inverted on the other end of the wire.)In-protocol layering, however, is somewhat more ad-hoc. The usual idiom right now is to simply subclass Protocol and then delegate from
dataReceivedto a new method, specific to your layering idiom, such aslineReceived, then have the next layer up subclass that.If you actually want a TCP implementation that uses Twisted, look here.
If you want to see a proposal for interfaces that will improve layering within Twisted itself, see here instead.