[Twisted-Python] Supporting a two-part client protocol.

Barry Scott barry.scott at forcepoint.com
Thu Feb 6 11:29:41 MST 2020


On Thursday, 6 February 2020 16:02:40 GMT Go Luhng wrote:
> Thanks for the detailed responses, Colin and Barry. I have a followup
> 
> question about sans-io. From the document:
> > For input (that is, receiving data from the network), the
> 
> calling code is responsible for delivering code to the
> implementation via a single input (often via a method called
> receive_bytes, or something similar).
> 
> This sounds very nice, however with certain protocols, you canonly
> decode serialized events based on context.

That's not a protocol problem its an implementation problem surely?

> For example, the parser needs to know if we're at the handshake stage,
> or the regular communication stage, or the shutdown stage, because
> different formats of messages are sent by the server at each stage.
> 
> How does this elegantly fit into the scheme of sans-io?

The sans-io (as I remember it) says don't put IO details into your
protocol code, abstract it/have clean API boundaries. (Makes it
easy to write unit tests as you do not need to have a network stack.
Just make the API calls: dataReceived, connectionLost, timeout etc).

Its the job of your protocol code to have a state machine and
know where is at any point in time. What you are calling "stage"
sounds like a "state" of the state machine.

The pattern in very crude outline is:
Define all the events that the protocol must handle.
Define all the states the protocol needs. When each event is
received do the state specific action and change state.

You can see this in the chunked encoding code, with one
way to implement the state machine.

Also note that its usual for there is be more then one state machine
in most protocols. Using HTTP as an example it needs to handle the
command, headers then the body
handling the command and headers means splitting the byte stream into lines.
Once you have the headers you can figure out how to process the body.

1. State machine for overall HTTP status, headers, body
2. State machine to split bytes received into header lines
3. State machine for chunked body encoding
4. State machine for Content-Length body encoding.

Barry


> 
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python






More information about the Twisted-Python mailing list