Skip to content

Commit

Permalink
Changed behavior of sockets so that the plugin will correctly recogni…
Browse files Browse the repository at this point in the history
…zed different clients.
  • Loading branch information
jason-watkins committed Apr 3, 2015
1 parent 6901ebc commit 2790f55
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions Java/src/XPlaneConnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
public class XPlaneConnect implements AutoCloseable
{
private DatagramSocket socket;
private static int clientNum;
private DatagramSocket outSocket;
private DatagramSocket inSocket;
private InetAddress xplaneAddr;
private int xplanePort;

Expand All @@ -27,7 +29,7 @@ public class XPlaneConnect implements AutoCloseable
*
* @return The incoming port number.
*/
public int getRecvPort() { return socket.getLocalPort(); }
public int getRecvPort() { return inSocket.getLocalPort(); }

/**
* Gets the port on which the client sends data to X-Plane.
Expand Down Expand Up @@ -89,10 +91,11 @@ public XPlaneConnect() throws SocketException
*/
public XPlaneConnect(int timeout) throws SocketException
{
this.socket = new DatagramSocket(49008);
this.inSocket = new DatagramSocket(49008);
this.outSocket = new DatagramSocket(50000 + ++clientNum);
this.xplaneAddr = InetAddress.getLoopbackAddress();
this.xplanePort = 49009;
this.socket.setSoTimeout(timeout);
this.inSocket.setSoTimeout(timeout);
}

/**
Expand Down Expand Up @@ -123,21 +126,27 @@ public XPlaneConnect(int port, String xplaneHost, int xplanePort)
public XPlaneConnect(int port, String xplaneHost, int xplanePort, int timeout)
throws java.net.SocketException, java.net.UnknownHostException
{
this.socket = new DatagramSocket(port);
this.inSocket = new DatagramSocket(port);
this.outSocket = new DatagramSocket(50000 + ++clientNum);
this.xplaneAddr = InetAddress.getByName(xplaneHost);
this.xplanePort = xplanePort;
this.socket.setSoTimeout(timeout);
this.inSocket.setSoTimeout(timeout);
}

/**
* Closes the underlying socket.
* Closes the underlying inSocket.
*/
public void close()
{
if(socket != null)
if(inSocket != null)
{
socket.close();
socket = null;
inSocket.close();
inSocket = null;
}
if(outSocket != null)
{
outSocket.close();
outSocket = null;
}
}

Expand All @@ -153,7 +162,7 @@ private byte[] readUDP() throws IOException //TODO: Store data in a class level
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
try
{
socket.receive(packet);
inSocket.receive(packet);
return Arrays.copyOf(buffer, buffer[4]);
}
catch (SocketTimeoutException ex)
Expand All @@ -178,7 +187,7 @@ private void sendUDP(byte[] buffer) throws IOException
buffer[4] = (byte)buffer.length;

DatagramPacket packet = new DatagramPacket(buffer, buffer.length, xplaneAddr, xplanePort);
socket.send(packet);
outSocket.send(packet);
}

/**
Expand Down Expand Up @@ -583,9 +592,9 @@ public void setCONN(int recvPort) throws IOException
os.write((byte) (recvPort >> 8));
sendUDP(os.toByteArray());

int soTimeout = socket.getSoTimeout();
socket.close();
socket = new DatagramSocket(recvPort);
socket.setSoTimeout(soTimeout);
int soTimeout = inSocket.getSoTimeout();
inSocket.close();
inSocket = new DatagramSocket(recvPort);
inSocket.setSoTimeout(soTimeout);
}
}

0 comments on commit 2790f55

Please sign in to comment.