Changed the protocol (simplified the handshake) and ..

Disabled the driver destroving after controller is set inactive. This
fixes reactivating the controller.
changed remaining println to log.info
This commit is contained in:
Maschell 2017-03-26 22:35:23 +02:00
parent 6626adf4a9
commit 7914c8d633
6 changed files with 26 additions and 45 deletions

View File

@ -36,8 +36,7 @@ import purejavahidapi.InputReportListener;
public class PureJavaHidController extends Controller implements InputReportListener {
public static Controller getInstance(String deviceIdentifier) throws IOException, ControllerInitializationFailedException {
HidDevice device = PureJavaHidApiManager.getDeviceByPath(deviceIdentifier);
// We use a special version to optimize the data for the switch pro
// controller
// We use a special version to optimize the data for the switch pro controller
if (device.getHidDeviceInfo().getVendorId() == SwitchProController.SWITCH_PRO_CONTROLLER_VID
&& device.getHidDeviceInfo().getProductId() == SwitchProController.SWITCH_PRO_CONTROLLER_PID) {
return new SwitchProController(deviceIdentifier);

View File

@ -103,7 +103,7 @@ public class ActiveControllerManager implements Runnable {
synchronized (activeControllers) {
for (Controller c : toRemove) {
NetworkManager.getInstance().removeHIDDevice(activeControllers.get(c));
c.destroyDriver();
//c.destroyDriver(); Removing it from the list doesn't require to close the connection.
activeControllers.remove(c);
}
}

View File

@ -121,8 +121,7 @@ public class ControllerManager {
default:
break;
}
if (c != null) { // I don't like that starting the Thread
// happens here =/
if (c != null) { // I don't like that starting the Thread happens here =/
new Thread(c).start();
attachedControllers.put(deviceIdentifier, c);
}
@ -134,7 +133,7 @@ public class ControllerManager {
public static List<Controller> getAttachedControllers() {
return new ArrayList<Controller>(attachedControllers.values());
}
private static Map<String, ControllerType> detectHIDDevices() {
Map<String, ControllerType> connectedDevices = new HashMap<String, ControllerType>();

View File

@ -278,7 +278,7 @@ public class NetworkManager implements Runnable {
System.out.println("UDP Packet sent: " + Utilities.ByteArrayToString(rawCommand));
}
} catch (IOException e) {
System.out.println("Sending read data failed.");
log.info("Sending read data failed.");
}
}
@ -331,13 +331,13 @@ public class NetworkManager implements Runnable {
log.info("Trying to connect to: " + ip);
try {
tcpClient.connect(ip);
System.out.println("TCP Connected!");
log.info("TCP Connected!");
udpClient = UDPClient.createUDPClient(ip);
if (udpClient != null) {
result = true;
}
} catch (Exception e) {
System.out.println("Error while connecting: " + e.getMessage());
log.info("Error while connecting: " + e.getMessage());
}
return result;
}

View File

@ -28,6 +28,7 @@ import java.nio.ByteBuffer;
import java.util.List;
import lombok.extern.java.Log;
import net.ash.HIDToVPADNetworkClient.network.Protocol.HandshakeReturnCode;
import net.ash.HIDToVPADNetworkClient.network.commands.AttachCommand;
import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand;
import net.ash.HIDToVPADNetworkClient.network.commands.PingCommand;
@ -57,9 +58,9 @@ public class Protocol {
}
public enum HandshakeReturnCode {
BAD_HANDSHAKE, SAME_CLIENT, NEW_CLIENT
GOOD_HANDSHAKE,BAD_HANDSHAKE
}
public static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException {
return ByteBuffer.allocate(9).put(Protocol.TCP_CMD_ATTACH).putInt(command.getHandle()).putShort(command.getVid()).putShort(command.getPid()).array();
}
@ -98,4 +99,5 @@ public class Protocol {
return bos.toByteArray();
}
}

View File

@ -42,7 +42,6 @@ public class TCPClient {
private Socket sock;
private DataInputStream in;
private DataOutputStream out;
@Getter private int clientID = HandleFoundry.next();
@Getter @Setter(AccessLevel.PRIVATE) private int shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
@ -52,46 +51,30 @@ public class TCPClient {
}
public synchronized void connect(String ip) throws Exception {
sock = new Socket();
sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000);
in = new DataInputStream(sock.getInputStream());
out = new DataOutputStream(sock.getOutputStream());
HandshakeReturnCode resultHandshake = doHandshake();
if (resultHandshake == HandshakeReturnCode.BAD_HANDSHAKE) {
log.info("[TCP] Handshaking failed");
throw new Exception();
} else {
if (resultHandshake == HandshakeReturnCode.NEW_CLIENT && this.ip != null) {
// We check the IP to be sure it's the first time we connect to
// a WiiU. //TODO: Sending a ID from the WiiU which will be
// compared?
// we are new to the client.
ActiveControllerManager.getInstance().attachAllActiveControllers();
} else if (resultHandshake == HandshakeReturnCode.SAME_CLIENT) {
}
HandshakeReturnCode resultHandshake = HandshakeReturnCode.GOOD_HANDSHAKE;
if (recvByte() != Protocol.TCP_HANDSHAKE) resultHandshake = HandshakeReturnCode.BAD_HANDSHAKE;
if (resultHandshake == HandshakeReturnCode.GOOD_HANDSHAKE) {
ActiveControllerManager.getInstance().attachAllActiveControllers();
this.ip = ip;
shouldRetry = 0;
}else{
log.info("[TCP] Handshaking failed");
throw new Exception();
}
}
private synchronized HandshakeReturnCode doHandshake() throws Exception {
if (recvByte() != Protocol.TCP_HANDSHAKE) return HandshakeReturnCode.BAD_HANDSHAKE;
send(clientID);
log.info("[TCP] Handshaking...");
HandshakeReturnCode test = (recvByte() == Protocol.TCP_NEW_CLIENT) ? HandshakeReturnCode.NEW_CLIENT : HandshakeReturnCode.SAME_CLIENT;
return test;
}
public synchronized boolean abort() {
try {
shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
sock.close();
clientID = HandleFoundry.next();
} catch (IOException e) {
System.out.println(e.getMessage()); // TODO: handle
log.info(e.getMessage()); // TODO: handle
return false;
}
return true;
@ -104,10 +87,8 @@ public class TCPClient {
} catch (IOException e) {
try {
if (shouldRetry++ < Settings.MAXIMUM_TRIES_FOR_RECONNECTING) {
System.out.println("Trying again to connect! Attempt number " + shouldRetry);
connect(ip); // TODO: this is for reconnecting when the WiiU
// switches the application. But this breaks
// disconnecting, woops.
log.info("Trying again to connect! Attempt number " + shouldRetry);
connect(ip); // TODO: this is for reconnecting when the WiiU switches the application. But this breaks disconnecting, woops.
} else {
abort();
}
@ -126,7 +107,7 @@ public class TCPClient {
try {
return in.readByte();
} catch (IOException e) {
System.out.println(e.getMessage());
log.info(e.getMessage());
throw e;
}
}
@ -135,7 +116,7 @@ public class TCPClient {
try {
return in.readShort();
} catch (IOException e) {
System.out.println(e.getMessage());
log.info(e.getMessage());
throw e;
}
}