[Twisted-Python] LineReceiver and setRawMode()

Pavel Pergamenshchik ppergame at gmail.com
Thu Sep 7 13:17:06 MDT 2006


On 9/7/06, Thomas HERVE <therve at free.fr> wrote:
>
> Quoting jarrod roberson <jarrod at 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20060907/33dd05fa/attachment.html>


More information about the Twisted-Python mailing list