@@ -16,6 +16,8 @@ public abstract class UdpConnection extends MavLinkConnection {
16
16
17
17
private int hostPort ;
18
18
private InetAddress hostAdd ;
19
+ private DatagramPacket sendPacket ;
20
+ private DatagramPacket receivePacket ;
19
21
20
22
private void getUdpStream () throws IOException {
21
23
final DatagramSocket socket = new DatagramSocket (serverPort );
@@ -46,8 +48,14 @@ public final void sendBuffer(byte[] buffer) throws IOException {
46
48
try {
47
49
if (hostAdd != null ) { // We can't send to our sister until they
48
50
// have connected to us
49
- DatagramPacket packet = new DatagramPacket (buffer , buffer .length , hostAdd , hostPort );
50
- socket .send (packet );
51
+ if (sendPacket == null )
52
+ sendPacket = new DatagramPacket (buffer , buffer .length , hostAdd , hostPort );
53
+ else {
54
+ sendPacket .setData (buffer , 0 , buffer .length );
55
+ sendPacket .setAddress (hostAdd );
56
+ sendPacket .setPort (hostPort );
57
+ }
58
+ socket .send (sendPacket );
51
59
}
52
60
} catch (Exception e ) {
53
61
e .printStackTrace ();
@@ -65,15 +73,19 @@ public void sendBuffer(InetAddress targetAddr, int targetPort, byte[] buffer) th
65
73
66
74
@ Override
67
75
public final int readDataBlock (byte [] readData ) throws IOException {
68
- final DatagramSocket socket = socketRef .get ();
69
- if (socket == null )
70
- return 0 ;
71
-
72
- DatagramPacket packet = new DatagramPacket (readData , readData .length );
73
- socket .receive (packet );
74
- hostAdd = packet .getAddress ();
75
- hostPort = packet .getPort ();
76
- return packet .getLength ();
76
+ final DatagramSocket socket = socketRef .get ();
77
+ if (socket == null )
78
+ return 0 ;
79
+
80
+ if (receivePacket == null )
81
+ receivePacket = new DatagramPacket (readData , readData .length );
82
+ else
83
+ receivePacket .setData (readData );
84
+
85
+ socket .receive (receivePacket );
86
+ hostAdd = receivePacket .getAddress ();
87
+ hostPort = receivePacket .getPort ();
88
+ return receivePacket .getLength ();
77
89
}
78
90
79
91
@ Override
0 commit comments