[Twisted-Python] LineReceiver and setRawMode()
I got a simple line protocol I am trying to implement, blocks . CMD <byte count>\r\n <payload> when CMD 5\r\n is received, I setRawMode() and then inside of rawDataReceived() handle the blob the problem is when my client sends the data and the next CMD line gets eaten by the rawDataReceived() call before I can set it back to lineMode(). so for something like CMD 5\r\n abcde CMD 10\r\n abcdefghij after I get CMD 5\r\n setRawMode() inside rawDataReceived(data) data = abcdeCMD 10\r\n is there something in twisted that will help with this? or do I need to subclass LineReceiver and add a setRawMode(bytesToRead) or something?
Quoting jarrod roberson <jarrod@vertigrated.com>:
I got a simple line protocol I am trying to implement, blocks .
[snip]
inside rawDataReceived(data) data = abcdeCMD 10\r\n
is there something in twisted that will help with this? or do I need to subclass LineReceiver and add a setRawMode(bytesToRead) or something?
What I've done in this case is something like this: def rawDataReceived(self, data): self.buffer += data if len(self.buffer) >= self.lenExpected: buf = self.buffer[:self.lenExpected] rem = self.buffer[self.lenExpected:] doSometingWithBuff(buf) self.buffer = "" self.setLineMode() if rem: self.dataReceived(rem) You need to set the variable self.lenExpected. You read the data and when you can all what you needed, you push it back again with dataReceived. -- Thomas ---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
On 9/7/06, Thomas HERVE <therve@free.fr> wrote:
Quoting jarrod roberson <jarrod@vertigrated.com>:
I got a simple line protocol I am trying to implement, blocks .
[snip]
inside rawDataReceived(data) data = abcdeCMD 10\r\n
is there something in twisted that will help with this? or do I need to subclass LineReceiver and add a setRawMode(bytesToRead) or something?
What I've done in this case is something like this:
def rawDataReceived(self, data): self.buffer += data if len(self.buffer) >= self.lenExpected: buf = self.buffer[:self.lenExpected] rem = self.buffer[self.lenExpected:] doSometingWithBuff(buf) self.buffer = "" self.setLineMode() if rem: self.dataReceived(rem)
You need to set the variable self.lenExpected. You read the data and when you can all what you needed, you push it back again with dataReceived.
setLineMode happens to take an optional argument for the remainder. You should use that instead of calling dataReceived directly.
participants (3)
-
jarrod roberson
-
Pavel Pergamenshchik
-
Thomas HERVE