From 0cafb807acad831ea229294f9f2629e9200d3404 Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@DESKTOP-2S09UKA>
Date: Wed, 1 Apr 2020 15:51:26 -0700
Subject: [PATCH] slightly slower sending for reliability; keep object
 instantiations outside of loops for best performance

---
 examples/src/OpenDis7Examples/AllPduSender.java | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/examples/src/OpenDis7Examples/AllPduSender.java b/examples/src/OpenDis7Examples/AllPduSender.java
index f6e05370ff..575d9cdc23 100755
--- a/examples/src/OpenDis7Examples/AllPduSender.java
+++ b/examples/src/OpenDis7Examples/AllPduSender.java
@@ -424,23 +424,26 @@ public class AllPduSender
             InetAddress localMulticastAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS);
             MulticastSocket multicastSocket = new MulticastSocket(DEFAULT_MULTICAST_PORT);
             multicastSocket.joinGroup(localMulticastAddress);
+            
+            // keep object instantiations outside of loops for best performance
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            DataOutputStream dos = new DataOutputStream(baos);
+            byte[] buffer;
+            Pdu aPdu;
+            DatagramPacket packet;
 
             for (int idx = 0; idx < generatedPdusList.size(); idx++)
 			{
-                ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                DataOutputStream dos = new DataOutputStream(baos);
-                byte[] buffer;
-
-                Pdu aPdu = generatedPdusList.get(idx);
+                aPdu = generatedPdusList.get(idx);
                 try 
                 {
                     aPdu.marshal(dos);
 
                     buffer = baos.toByteArray();
-                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT);
+                    packet = new DatagramPacket(buffer, buffer.length, localMulticastAddress, DEFAULT_MULTICAST_PORT);
                     multicastSocket.send(packet);
                     try {
-                        Thread.sleep(100L); // TODO this kind of delay timing should be in a DIS sender class?
+                        Thread.sleep(250L); // TODO shouldn't this kind of delay timing be in a DIS sender class?
                     } catch (InterruptedException ex) {
                     }
                     String currentPduTypeValuePadded = String.format("%2s", aPdu.getPduType().getValue());
-- 
GitLab