Search code examples
pythonqttwisted

Detect the end of a raw data


Twisted has two data reception modes: a Line Mode and a Raw Mode, and we can switch between them using setRawMode() and setLineMode() functions.

the line mode detects and end of line and then calls the lineReceived() function.

From Twisted doc:

def rawDataReceived(self, data):

Override this for when raw data is received.

How can Twisted detect the end of a raw data and then call rawDataReceived() ?

EDIT:

I'll add this to to complete my question.

I'm using this Qt function to send data to the Twisted server

qint64 QIODevice::write(const QByteArray & byteArray)

I thought that using write() two times means that the Twisted server will trigger the rawDataReceived() functions two times too.

write( "raw1" );
write( "raw2" ); 

but data are received in one time.


Solution

  • You asked:

    How can Twisted detect the end of a raw data and then call rawDataReceived() ?

    In short, when you turn on raw your asking Twisted not to detect.

    ... but let me explain

    When you talk about 'detecting the end of data' inside of a connection (I.E. if your not closing the connection at the end of data), your talking about an idea that is normally referred to as framing.

    Framing is one of the primary issues you have to keep in mind when your doing application level networking programming, because most (networking) protocols don't guarantee data framing to the application.

    Confusingly many networking protocols (of which TCP is one of the most notorious) will often but not always present data to the receiver in the same way as it is transmitted (I.E. As though it had framing, each write will cause one read to happen - but only in cases of slow-use and low-load). Because of this maybe-it-will-work-maybe-it-won't behavior the best practice is to always explicitly add/build-in some sort of framing.

    The most common method to add application-level framing in TCP/Serial/Keyboard style interfaces is to use line-breaks as end-of-frame makers, which is what LineMode is about.

    Turning on raw mode in Twisted is like saying 'I want to write my own framing', but I doubt thats really what your after.

    Instead you probably want to look at some of the other helper protocols (netstring, prefixed-message-length) that Twisted offers that will do binary framing for you (also see SO: Fragmented data in Twisted dataRecivied by Twisted's author Glyph)