[Twisted-Python] Multiple clients and ports

Phil Mayers p.mayers at imperial.ac.uk
Fri Apr 19 04:20:24 EDT 2013


On 04/19/2013 01:33 AM, Carlos Eduardo Sotelo Pinto wrote:
> I am doing a gps tracker wich use multiple protocols and multiple
> clients, my problen is when I have more than one client on more than
> one protocol at the same time, data and validation start to crashing
> one to other

I don't understand what you mean. Can you be more specific?

>
>      def __init__(self, decoder):
>          """Class Constructor."""
>          decoderModule = __import__('listener.protocols.%sDecoder' %
> (decoder, ), fromlist=['%sDecoder' % (decoder, )])
>          decoderClass = getattr(decoderModule, '%sDecoder' % (decoder, ))

You might want to look at "twisted.python.reflect" which contains utils 
to do that kind of "give me the python object at this import"


>      def dataReceived(self, data):
>          Protocol.dataReceived(self, data)
>
>          """DataReceived Twisted event."""
>          try:
>              self.sendResponse(self.decoder.processDatagram(data))

Not sure if this is your problem - but stream protocols (TCP) don't 
guarantee that "message" boundaries will be preserved - that is, if the 
sender does:

write(100 bytes)
write(100 bytes)

...you might get

dataReceived(10 bytes)
dataReceived(39 bytes)
dataReceived(27 bytes)

...or any other variation - the data can arrive in arbitrary chunks. 
Unless your processDatagram is doing the reassembly, you need to 
implement message framing in your dataReceived method.

Note that the commented out versions of your base classes were all for 
protocols which do framing (e.g. line-based, etc.)

Normally this looks something like:

  def dataReceived(self, data):
     self.buffer += data
     if hasFullMessage(self.buffer):
       msg, self.buffer = extractMessage(self.buffer)
       processMsg(msg)





More information about the Twisted-Python mailing list