TCP open refactoring

This commit is contained in:
rusefillc 2020-10-08 23:45:51 -04:00
parent fdceaf8c96
commit b5058dff2b
3 changed files with 11 additions and 10 deletions

View File

@ -7,7 +7,6 @@ import com.rusefi.NamedThreadFactory;
import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.binaryprotocol.BinaryProtocolState;
import com.rusefi.core.EngineState;
import com.rusefi.core.MessagesCentral;
import com.rusefi.io.serial.StreamConnector;
import com.rusefi.io.serial.SerialIoStreamJSerialComm;
import com.rusefi.io.tcp.TcpConnector;
@ -15,7 +14,6 @@ import com.rusefi.io.tcp.TcpIoStream;
import org.jetbrains.annotations.NotNull;
import java.io.Closeable;
import java.net.Socket;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.*;
@ -213,14 +211,8 @@ public class LinkManager implements Closeable {
@Override
public IoStream call() {
messageListener.postMessage(getClass(), "Opening port: " + port);
Socket socket;
try {
int portPart = TcpConnector.getTcpPort(port);
String hostname = TcpConnector.getHostname(port);
socket = new Socket(hostname, portPart);
TcpIoStream tcpIoStream = new TcpIoStream("[start] ", socket);
return tcpIoStream;
return TcpIoStream.open(port);
} catch (Throwable e) {
stateListener.onConnectionFailed();
return null;

View File

@ -42,7 +42,7 @@ public class TcpConnector {
return confirmation.substring(0, length);
}
*/
static class InvalidTcpPort extends Exception {
public static class InvalidTcpPort extends Exception {
}
public static int getTcpPort(String port) throws InvalidTcpPort {

View File

@ -39,6 +39,15 @@ public class TcpIoStream extends AbstractIoStream {
this.dataBuffer = IncomingDataBuffer.createDataBuffer(loggingPrefix, this);
}
@NotNull
public static TcpIoStream open(String port) throws TcpConnector.InvalidTcpPort, IOException {
int portPart = TcpConnector.getTcpPort(port);
String hostname = TcpConnector.getHostname(port);
Socket socket = new Socket(hostname, portPart);
return new TcpIoStream("[start] ", socket);
}
@Override
public void close() {
// we need to guarantee only one onDisconnect invocation for retry logic to be healthy