Automatically close forked beacon connections, fix connection forwards

This commit is contained in:
Christopher Schnick 2022-08-19 16:14:49 +02:00
parent e0266fec6b
commit 6efad1fe42
6 changed files with 10 additions and 19 deletions

View file

@ -30,7 +30,7 @@ public class BeaconConfig {
public static final String BEACON_PORT_PROP = "io.xpipe.beacon.port";
public static final int DEFAULT_PORT = 21721;
public static final int DEFAULT_PORT = System.getProperty("os.name").startsWith("Windows") ? 21721 : 21722;
public static int getUsedPort() {
if (System.getProperty(BEACON_PORT_PROP) != null) {

View file

@ -6,8 +6,6 @@ import lombok.experimental.UtilityClass;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
@ -18,19 +16,14 @@ import java.util.Optional;
@UtilityClass
public class BeaconServer {
private static boolean isPortAvailable(int port) {
try (var ignored = new ServerSocket(port); var ignored1 = new DatagramSocket(port)) {
public static boolean isRunning() {
try (var socket = new BeaconClient()) {
return true;
} catch (IOException e) {
} catch (Exception e) {
return false;
}
}
public static boolean isRunning() {
var port = BeaconConfig.getUsedPort();
return !isPortAvailable(port);
}
private static void startFork(String custom) throws IOException {
boolean print = BeaconConfig.execDebugEnabled();
if (print) {
@ -72,14 +65,17 @@ public class BeaconServer {
}, "daemon fork syserr").start();
}
public static boolean tryStart() throws Exception {
public static boolean tryStartFork() throws Exception {
var custom = BeaconConfig.getCustomExecCommand();
if (custom != null) {
System.out.println("Starting fork: " + custom);
startFork(custom);
return true;
}
return false;
}
public static boolean tryStart() throws Exception {
var daemonExecutable = getDaemonExecutable();
if (daemonExecutable.isPresent()) {
if (BeaconConfig.debugEnabled()) {
@ -99,7 +95,7 @@ public class BeaconServer {
public static boolean tryStop(BeaconClient client) throws Exception {
client.sendRequest(StopExchange.Request.builder().build());
StopExchange.Response res =client.receiveResponse();
StopExchange.Response res = client.receiveResponse();
return res.isSuccess();
}

View file

@ -18,7 +18,6 @@ public interface RawReadConnection extends DataSourceReadConnection {
default void forward(DataSourceConnection con) throws Exception {
try (var tCon = (RawWriteConnection) con) {
tCon.init();
byte[] b;
while ((b = readBytes(BUFFER_SIZE)).length > 0) {
tCon.write(b);

View file

@ -11,7 +11,6 @@ public interface StructureReadConnection extends DataSourceReadConnection {
default void forward(DataSourceConnection con) throws Exception {
try (var tCon = (StructureWriteConnection) con) {
tCon.init();
tCon.write(read());
}
}

View file

@ -28,7 +28,6 @@ public interface TextReadConnection extends DataSourceReadConnection {
default void forward(DataSourceConnection con) throws Exception {
try (var tCon = (TextWriteConnection) con) {
tCon.init();
for (var it = lines().iterator(); it.hasNext(); ) {
tCon.writeLine(it.next());
}

View file

@ -78,9 +78,7 @@ public interface DataStoreProvider {
return null;
}
default DataStore defaultStore() {
throw new ExtensionException("CLI Dialog not implemented by provider");
}
DataStore defaultStore();
default String display(DataStore store) {
return store.toSummaryString();