detect console disconnection faster (#3926)

* optimize reads

* try to reconnect in 1 second

* print more

* inspection results

* put the timeout back

* note about race

* s
This commit is contained in:
Matthew Kennedy 2022-02-12 03:44:58 -08:00 committed by GitHub
parent 837d228159
commit 60560c29d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 9 deletions

View File

@ -253,7 +253,7 @@ public class LinkManager implements Closeable {
@Override
public IoStream call() {
messageListener.postMessage(getClass(), "Opening port: " + port);
IoStream stream = ((Callable<IoStream>) () -> BufferedSerialIoStream.openPort(port)).call();
IoStream stream = BufferedSerialIoStream.openPort(port);
if (stream == null) {
// error already reported
return null;
@ -293,7 +293,7 @@ public class LinkManager implements Closeable {
close(); // Explicitly kill the connection (call connectors destructor??????)
String[] ports = getCommPorts();
boolean isPortAvailableAgain = Arrays.stream(ports).anyMatch(lastTriedPort::equals);
boolean isPortAvailableAgain = Arrays.asList(ports).contains(lastTriedPort);
if (isPortAvailableAgain) {
connect(lastTriedPort);
}

View File

@ -55,10 +55,16 @@ public class SerialIoStream extends AbstractIoStream {
}
@Override
public void write(byte[] bytes) {
public void write(byte[] bytes) throws IOException {
if (Bug3923.obscene)
log.info("Writing " + bytes.length + " byte(s)");
sp.writeBytes(bytes, bytes.length);
int written = sp.writeBytes(bytes, bytes.length);
// If we failed to write all the bytes, the ECU probably disconnected
if (written != bytes.length) {
throw new IOException("write failed: wrote " + written + " but expected " + bytes.length);
}
}
@Override
@ -87,7 +93,7 @@ public class SerialIoStream extends AbstractIoStream {
@Override
public void serialEvent(SerialPortEvent event) {
if (Bug3923.obscene)
log.info("serialEvent " + event);
log.info("serialEvent " + event.getEventType());
if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
return;
if (isFirstEvent) {
@ -100,10 +106,16 @@ public class SerialIoStream extends AbstractIoStream {
log.info("serialEvent bytesAvailable " + bytesAvailable);
if (bytesAvailable <= 0)
return; // sometimes negative value is returned at least on Mac
byte[] newData = new byte[bytesAvailable];
int numRead = sp.readBytes(newData, newData.length);
byte[] data = new byte[numRead];
System.arraycopy(newData, 0, data, 0, numRead);
byte[] data = new byte[bytesAvailable];
int numRead = sp.readBytes(data, data.length);
// Copy in to a smaller array if the read was incomplete
if (numRead != bytesAvailable) {
byte[] dataSmaller = new byte[numRead];
System.arraycopy(data, 0, dataSmaller, 0, numRead);
data = dataSmaller;
}
listener.onDataArrived(data);
}
});

View File

@ -86,6 +86,7 @@ public class ConsoleUI {
if (LinkManager.isLogViewerMode(port))
tabbedPane.addTab("Log Viewer", new LogViewer(uiContext, engineSnifferPanel));
// TODO: this is a race if the ECU is slow to connect
new ConnectionWatchdog(Timeouts.CONNECTION_RESTART_DELAY, () -> {
uiContext.getLinkManager().execute(() -> {
FileLog.MAIN.logLine("ConnectionWatchdog.reconnectTimer restarting: " + Timeouts.CONNECTION_RESTART_DELAY);