views:

1102

answers:

3

Hello,

In Twisted when implementing the dataReceived method, there doesn't seem to be any examples which refer to packets being fragmented. In every other language this is something you manually implement, so I was just wondering if this is done for you in twisted already or what? If so, do I need to prefix my packets with a length header? Or do I have to do this manually? If so, what way would that be?

+3  A: 

In the dataReceived method you get back the data as a string of indeterminate length meaning that it may be a whole message in your protocol or it may only be part of the message that some 'client' sent to you. You will have to inspect the data to see if it comprises a whole message in your protocol.

I'm currently using Twisted on one of my projects to implement a protocol and decided to use the struct module to pack/unpack my data. The protocol I am implementing has a fixed header size so I don't construct any messages until I've read at least HEADER_SIZE amount of bytes. The total message size is declared in this header data portion.

I guess you don't really need to define a message length as part of your protocol but it helps. If you didn't define one you would have to have a special delimiter that determines when a message begins/ends. Sort of how the FIX protocol uses the SOH byte to delimit fields. Though it does have a required field that tells you how long a message is (just not how many fields are in a message).

mabbit
Be very careful with this approach. Trusting the client to tell you how much data it's going to send is a classic way to introduce a buffer overflow or similar attack.
Jesse Weigert
Jesse, I don't think you understand this answer properly. First of all, Twisted is in Python, so it's highly unlikely that you'll ever have a buffer overflow. Second, delimited input is far more likely to cause a buffer overflow than length-prefixed input; see, for example, the security section of http://cr.yp.to/proto/netstrings.txt or just about any book on C network programming.The idea here isn't that you "trust" the client to tell you how much data it's going to send - it's that the client tells you how many bytes (of the arbitrary number it sends) constitute one message.
Glyph
+2  A: 

When dealing with TCP, you should really forget all notion of 'packets'. TCP is a stream protocol - you stream data in and data streams out the other side. Once the data is sent, it is allowed to arrive in as many or as few blocks as it wants, as long as the data all arrives in the right order. You'll have to manually do the delimitation as with other languages, with a length field, or a message type field, or a special delimiter character, etc.

Kylotan
Wish I could add a few more votes to this one, the 'dataReceived' method in twisted's protocol class gets fired every time a packet comes in and you need to make sure that all the data has been received before you can move to write the data received by the protocol.
lewisblackfan
+1  A: 

You can also use a LineReceiver protocol

ajbl