Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 412
Registered: ‎09-12-2008
My Device: 9000 9810

Optimize udp send/receiving, any hints ?

Hi all.

 

I have an optimizing question.

 

My app sends and receives UDP packets using the same socket.

Now I am trying to squeeze out every unnecessary handles (created by using operator new).

 

Here's how I receive:

 

 

byte[] buffer = new byte[120];
Datagram packet = fSocket.newDatagram(buffer, buffer.length);

while(true)
{
  fSocket.receive(packet); 
  //byte[] packetData = packet.getData();
  // or byte[] packetData = copyData(packet);
  ... 
  handlePacket(packetData)
  ...
}

 This way I am reusing the same byte[] buffer over and over, and the same datagram (both are instantiated only once)

 

1) I suppose getData will return a reference to buffer ?

2) This means I need to copy the data since the new received datagram will overwrite that data ?

 

And here's the sending side:

 

 

send(buffer, offset, length)
{
  // ... check arguments

  if (fOutgoingDatagramPacket == null)
  { 
    fOutgoingDatagramPacket = fSocket.newDatagram(120);
    fOutgoingDatagramPacket.setAddress(someAddress);
  }

  fOutgoingDatagramPacket.setData(buffer, offset, length);
  fSocket.send(fOutgoingDatagramPacket);
}

 

 

3) To me, it seems as though the sending side is optimized at it's best, don't you think?

 

4) Does anyone have any ideas how to further (even by the tiniest fraction) improve any of the mentioned practices ?

 

 



"Listen carefully, I shall say this only once"