Cleaned up implementation of getDREFs in the Java client.
- Removed unnecessary loop to try to read several times. - Fixed the way that readUDP detects the length of received packets.
This commit is contained in:
@@ -175,12 +175,12 @@ public class XPlaneConnect implements AutoCloseable
|
|||||||
*/
|
*/
|
||||||
private byte[] readUDP() throws IOException
|
private byte[] readUDP() throws IOException
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[2048];
|
byte[] buffer = new byte[65536];
|
||||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
return Arrays.copyOf(buffer, buffer[4]);
|
return Arrays.copyOf(buffer, packet.getLength());
|
||||||
}
|
}
|
||||||
catch (SocketTimeoutException ex)
|
catch (SocketTimeoutException ex)
|
||||||
{
|
{
|
||||||
@@ -293,20 +293,14 @@ public class XPlaneConnect implements AutoCloseable
|
|||||||
sendUDP(os.toByteArray());
|
sendUDP(os.toByteArray());
|
||||||
|
|
||||||
//Read response
|
//Read response
|
||||||
for(int i = 0; i < 40; ++i)
|
|
||||||
{
|
|
||||||
byte[] data = readUDP();
|
byte[] data = readUDP();
|
||||||
if(data.length == 0)
|
if(data.length == 0)
|
||||||
{
|
{
|
||||||
continue;
|
throw new IOException("No response received.");
|
||||||
}
|
}
|
||||||
if(data.length < 6)
|
if(data.length < 6)
|
||||||
{
|
{
|
||||||
throw new Error("Response too short"); //TODO: Make custom error type
|
throw new IOException("Response too short");
|
||||||
}
|
|
||||||
if(data[5] != drefs.length)
|
|
||||||
{
|
|
||||||
throw new Error("Unexpected response length"); //TODO: Make custom error type
|
|
||||||
}
|
}
|
||||||
float[][] result = new float[drefs.length][];
|
float[][] result = new float[drefs.length][];
|
||||||
ByteBuffer bb = ByteBuffer.wrap(data);
|
ByteBuffer bb = ByteBuffer.wrap(data);
|
||||||
@@ -323,8 +317,6 @@ public class XPlaneConnect implements AutoCloseable
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
throw new IOException("No response received.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void sendDREF(String dref, float value) throws IOException
|
public void sendDREF(String dref, float value) throws IOException
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user