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

View File

@ -103,7 +103,7 @@ public class ActiveControllerManager implements Runnable {
synchronized (activeControllers) { synchronized (activeControllers) {
for (Controller c : toRemove) { for (Controller c : toRemove) {
NetworkManager.getInstance().removeHIDDevice(activeControllers.get(c)); 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); activeControllers.remove(c);
} }
} }

View File

@ -121,8 +121,7 @@ public class ControllerManager {
default: default:
break; break;
} }
if (c != null) { // I don't like that starting the Thread if (c != null) { // I don't like that starting the Thread happens here =/
// happens here =/
new Thread(c).start(); new Thread(c).start();
attachedControllers.put(deviceIdentifier, c); attachedControllers.put(deviceIdentifier, c);
} }

View File

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

View File

@ -28,6 +28,7 @@ import java.nio.ByteBuffer;
import java.util.List; import java.util.List;
import lombok.extern.java.Log; 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.AttachCommand;
import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand; import net.ash.HIDToVPADNetworkClient.network.commands.DetachCommand;
import net.ash.HIDToVPADNetworkClient.network.commands.PingCommand; import net.ash.HIDToVPADNetworkClient.network.commands.PingCommand;
@ -57,7 +58,7 @@ public class Protocol {
} }
public enum HandshakeReturnCode { public enum HandshakeReturnCode {
BAD_HANDSHAKE, SAME_CLIENT, NEW_CLIENT GOOD_HANDSHAKE,BAD_HANDSHAKE
} }
public static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException { public static byte[] getRawAttachDataToSend(AttachCommand command) throws IOException {
@ -98,4 +99,5 @@ public class Protocol {
return bos.toByteArray(); return bos.toByteArray();
} }
} }

View File

@ -42,7 +42,6 @@ public class TCPClient {
private Socket sock; private Socket sock;
private DataInputStream in; private DataInputStream in;
private DataOutputStream out; private DataOutputStream out;
@Getter private int clientID = HandleFoundry.next();
@Getter @Setter(AccessLevel.PRIVATE) private int shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; @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 { public synchronized void connect(String ip) throws Exception {
sock = new Socket(); sock = new Socket();
sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000); sock.connect(new InetSocketAddress(ip, Protocol.TCP_PORT), 2000);
in = new DataInputStream(sock.getInputStream()); in = new DataInputStream(sock.getInputStream());
out = new DataOutputStream(sock.getOutputStream()); out = new DataOutputStream(sock.getOutputStream());
HandshakeReturnCode resultHandshake = doHandshake(); HandshakeReturnCode resultHandshake = HandshakeReturnCode.GOOD_HANDSHAKE;
if (resultHandshake == HandshakeReturnCode.BAD_HANDSHAKE) { if (recvByte() != Protocol.TCP_HANDSHAKE) 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) {
} if (resultHandshake == HandshakeReturnCode.GOOD_HANDSHAKE) {
ActiveControllerManager.getInstance().attachAllActiveControllers();
this.ip = ip; this.ip = ip;
shouldRetry = 0; 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() { public synchronized boolean abort() {
try { try {
shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING; shouldRetry = Settings.MAXIMUM_TRIES_FOR_RECONNECTING;
sock.close(); sock.close();
clientID = HandleFoundry.next();
} catch (IOException e) { } catch (IOException e) {
System.out.println(e.getMessage()); // TODO: handle log.info(e.getMessage()); // TODO: handle
return false; return false;
} }
return true; return true;
@ -104,10 +87,8 @@ public class TCPClient {
} catch (IOException e) { } catch (IOException e) {
try { try {
if (shouldRetry++ < Settings.MAXIMUM_TRIES_FOR_RECONNECTING) { if (shouldRetry++ < Settings.MAXIMUM_TRIES_FOR_RECONNECTING) {
System.out.println("Trying again to connect! Attempt number " + shouldRetry); log.info("Trying again to connect! Attempt number " + shouldRetry);
connect(ip); // TODO: this is for reconnecting when the WiiU connect(ip); // TODO: this is for reconnecting when the WiiU switches the application. But this breaks disconnecting, woops.
// switches the application. But this breaks
// disconnecting, woops.
} else { } else {
abort(); abort();
} }
@ -126,7 +107,7 @@ public class TCPClient {
try { try {
return in.readByte(); return in.readByte();
} catch (IOException e) { } catch (IOException e) {
System.out.println(e.getMessage()); log.info(e.getMessage());
throw e; throw e;
} }
} }
@ -135,7 +116,7 @@ public class TCPClient {
try { try {
return in.readShort(); return in.readShort();
} catch (IOException e) { } catch (IOException e) {
System.out.println(e.getMessage()); log.info(e.getMessage());
throw e; throw e;
} }
} }