Fixed bug with the connecting when auto activating is enabled

- Force the controller to send their data once after they connected.
- Limiting the maximum handle size to (2^15)-1.
This commit is contained in:
Maschell 2017-03-27 22:06:31 +02:00
parent 72fd042a3a
commit 5f9da81af9
4 changed files with 22 additions and 4 deletions

View File

@ -31,6 +31,7 @@ import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.controller.Controller; import net.ash.HIDToVPADNetworkClient.controller.Controller;
import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice; import net.ash.HIDToVPADNetworkClient.network.NetworkHIDDevice;
import net.ash.HIDToVPADNetworkClient.network.NetworkManager; import net.ash.HIDToVPADNetworkClient.network.NetworkManager;
import net.ash.HIDToVPADNetworkClient.network.TCPClient;
import net.ash.HIDToVPADNetworkClient.util.Settings; import net.ash.HIDToVPADNetworkClient.util.Settings;
import net.ash.HIDToVPADNetworkClient.util.Utilities; import net.ash.HIDToVPADNetworkClient.util.Utilities;
@ -113,7 +114,8 @@ public class ActiveControllerManager implements Runnable {
synchronized (activeControllers) { synchronized (activeControllers) {
for (Controller c : toAdd) { for (Controller c : toAdd) {
NetworkHIDDevice hiddevice = new NetworkHIDDevice(c.getVID(), c.getPID()); 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); NetworkManager.getInstance().addHIDDevice(hiddevice);
activeControllers.put(c, hiddevice); activeControllers.put(c, hiddevice);
} }
@ -141,6 +143,15 @@ public class ActiveControllerManager implements Runnable {
} }
} }
public void detachAllActiveControllers() {
synchronized (activeControllers) {
for (Entry<Controller, NetworkHIDDevice> entry : activeControllers.entrySet()) {
NetworkHIDDevice device = entry.getValue();
device.sendDetach();
}
}
}
/** /**
* *
* @param HIDhandle * @param HIDhandle
@ -154,4 +165,5 @@ public class ActiveControllerManager implements Runnable {
} }
return null; return null;
} }
} }

View File

@ -43,6 +43,8 @@ public class NetworkHIDDevice {
@Getter @Setter private short deviceslot; @Getter @Setter private short deviceslot;
@Getter @Setter private byte padslot; @Getter @Setter private byte padslot;
@Getter @Setter private boolean needFirstData = false;
@Getter private int hidHandle = HandleFoundry.next(); @Getter private int hidHandle = HandleFoundry.next();
@Getter(AccessLevel.PRIVATE) private List<DeviceCommand> commands = new ArrayList<DeviceCommand>(); @Getter(AccessLevel.PRIVATE) private List<DeviceCommand> commands = new ArrayList<DeviceCommand>();
@ -74,11 +76,14 @@ public class NetworkHIDDevice {
private byte[] lastdata = null; private byte[] lastdata = null;
public void sendRead(byte[] data) { public void sendRead(byte[] data) {
if (!Arrays.equals(lastdata, data)) { if (!Arrays.equals(lastdata, data) || isNeedFirstData()) {
synchronized (readCommandLock) { synchronized (readCommandLock) {
setLatestRead(new ReadCommand(getHidHandle(), data, this)); // Only get the latest Value. setLatestRead(new ReadCommand(getHidHandle(), data, this)); // Only get the latest Value.
} }
lastdata = data.clone(); lastdata = data.clone();
if (isNeedFirstData()) {
setNeedFirstData(false);
}
} }
} }

View File

@ -262,11 +262,12 @@ public class NetworkManager implements Runnable {
return false; return false;
} }
// Let's save them for later. // Let's save them for later and demand the first data packet.
NetworkHIDDevice sender = command.getSender(); NetworkHIDDevice sender = command.getSender();
if (sender != null) { if (sender != null) {
sender.setDeviceslot(deviceslot); sender.setDeviceslot(deviceslot);
sender.setPadslot(padslot); sender.setPadslot(padslot);
sender.setNeedFirstData(true); // Please send data after connecting.
} else { } else {
log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName()); log.info("Something really went wrong. Got an attach event with out an " + NetworkHIDDevice.class.getSimpleName());
return false; return false;

View File

@ -29,7 +29,7 @@ import java.util.Random;
public class HandleFoundry { public class HandleFoundry {
// We start with a random value, so we have at each startup a different clientID! // 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() { private HandleFoundry() {
} }