diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java index c197ed5..8a8e6d9 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ActiveControllerManager.java @@ -31,6 +31,7 @@ import lombok.extern.java.Log; import net.ash.HIDToVPADNetworkClient.controller.Controller; import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice; import net.ash.HIDToVPADNetworkClient.network.NetworkManager; +import net.ash.HIDToVPADNetworkClient.network.TCPClient; import net.ash.HIDToVPADNetworkClient.util.Settings; import net.ash.HIDToVPADNetworkClient.util.Utilities; @@ -113,7 +114,8 @@ public class ActiveControllerManager implements Runnable { synchronized (activeControllers) { for (Controller c : toAdd) { NetworkHIDDevice hiddevice = new NetworkHIDDevice(c.getVID(), c.getPID()); - hiddevice.sendAttach(); + if (NetworkManager.getInstance().isConnected()) hiddevice.sendAttach(); // Only send the attach when we're connected. Because on connecting the + // TCPClient will automatically attach all active controller NetworkManager.getInstance().addHIDDevice(hiddevice); activeControllers.put(c, hiddevice); } @@ -141,6 +143,15 @@ public class ActiveControllerManager implements Runnable { } } + public void detachAllActiveControllers() { + synchronized (activeControllers) { + for (Entry entry : activeControllers.entrySet()) { + NetworkHIDDevice device = entry.getValue(); + device.sendDetach(); + } + } + } + /** * * @param HIDhandle @@ -154,4 +165,5 @@ public class ActiveControllerManager implements Runnable { } return null; } + } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java index e14f95b..9af0b94 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkHIDDevice.java @@ -43,6 +43,8 @@ public class NetworkHIDDevice { @Getter @Setter private short deviceslot; @Getter @Setter private byte padslot; + @Getter @Setter private boolean needFirstData = false; + @Getter private int hidHandle = HandleFoundry.next(); @Getter(AccessLevel.PRIVATE) private List commands = new ArrayList(); @@ -74,11 +76,14 @@ public class NetworkHIDDevice { private byte[] lastdata = null; public void sendRead(byte[] data) { - if (!Arrays.equals(lastdata, data)) { + if (!Arrays.equals(lastdata, data) || isNeedFirstData()) { synchronized (readCommandLock) { setLatestRead(new ReadCommand(getHidHandle(), data, this)); // Only get the latest Value. } lastdata = data.clone(); + if (isNeedFirstData()) { + setNeedFirstData(false); + } } } diff --git a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java index b67f59f..0c8bfd6 100644 --- a/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/network/NetworkManager.java @@ -262,11 +262,12 @@ public class NetworkManager implements Runnable { return false; } - // Let's save them for later. + // Let's save them for later and demand the first data packet. NetworkHIDDevice sender = command.getSender(); if (sender != null) { sender.setDeviceslot(deviceslot); sender.setPadslot(padslot); + sender.setNeedFirstData(true); // Please send data after connecting. } else { log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName()); return false; diff --git a/src/net/ash/HIDToVPADNetworkClient/util/HandleFoundry.java b/src/net/ash/HIDToVPADNetworkClient/util/HandleFoundry.java index 7b44bb4..18975d6 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/HandleFoundry.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/HandleFoundry.java @@ -29,7 +29,7 @@ import java.util.Random; public class HandleFoundry { // We start with a random value, so we have at each startup a different clientID! - private static int h = new Random().nextInt(100000); + private static int h = new Random().nextInt(Short.MAX_VALUE); private HandleFoundry() { }