Does PyDatagramIterator hold multiple messages?

Hi all,

I’ve set up network code per the manual, and I’m experiencing pretty severe latency between the two computers. I’m wondering if this is just because I’m only pulling one string per frame from pyDatagramIterator (while it may be receiving more than one datagram from the second, faster computer).

Can PyDatagramIterator hold multiple strings/datagrams? If so, how can I access everything?

Any help is appreciated.

Thank you for your time,
Logan

PyDatagramIterator is simply a way to walk through the contents of your PyDatagram. The Datagram holds as many strings/items as you put into it on the sending side. If your sender adds one string to a Datagram and then sends it, there will only be one string on the Datagram when you receive it.

You may be able to receive multiple Datagrams in a given frame, though. You should consider reading Datagrams from the queue until there are no more Datagrams to read, instead of reading only one per frame. (Of course, then you risk starving the render process if you get flooded with Datagrams, but dealing properly with a DOS attack is a whole science of itself.)

David

Thanks for the quick reply! Is there any code snippets of how to read the contents of the datagram until there is nothing left? Would I just use PyDatagramIterator until it’s returning null?

Thanks again,
Logan

You have to know a priori how much data is in your Datagram–the Datagram itself won’t tell you. You can try to keep reading until you get an AssertionError, but this will fail if you ever run on an optimized build with assertions compiled out.

One way to know how much data is there is to put some indication there yourself, for instance, begin the Datagram with an Int32 that indicates the number of strings you can expect to read.

David

Pardon me, meant to ask: Is there any code snippets to walk through multiple datagrams? How do I know if there are more datagrams to access and how do I walk through them?

Given this snippet from the manual:

def tskReaderPolling(taskdata):
  if cReader.dataAvailable():
    datagram=NetDatagram()  # catch the incoming data in this instance
    # Check the return value; if we were threaded, someone else could have
    # snagged this data before we did
    if cReader.getData(datagram):
      myProcessDataFunction(datagram)
  return Task.cont

You can make this walk through all datagrams currently available simply by replacing the first “if” with a while:

def tskReaderPolling(taskdata):
  while cReader.dataAvailable():
    datagram=NetDatagram()  # catch the incoming data in this instance
    # Check the return value; if we were threaded, someone else could have
    # snagged this data before we did
    if cReader.getData(datagram):
      myProcessDataFunction(datagram)
  return Task.cont

David

great thanks