Skip to content

Commit

Permalink
Switch to virtual threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 10, 2024
1 parent c23daaa commit 509b2a4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/github/gaming32/worldhost/IOFunction.java
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;

// TODO: Remove when 1.18.2 is minimum
@FunctionalInterface
public interface IOFunction<T, R> {
R apply(T t) throws IOException;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/io/github/gaming32/worldhost/ProxyClient.java
Expand Up @@ -9,7 +9,8 @@
import java.util.Arrays;
import java.util.function.Supplier;

public class ProxyClient extends Thread {
public final class ProxyClient {
private final Thread thread;
private final Socket socket;
private final InetAddress remoteAddress;
private final long connectionId;
Expand All @@ -23,7 +24,7 @@ public ProxyClient(
long connectionId,
Supplier<ProxyPassthrough> proxy
) throws IOException {
super("ProxyClient for " + connectionId);
thread = Thread.ofVirtual().name("ProxyClient for " + connectionId).unstarted(this::run);
socket = new Socket(InetAddress.getLoopbackAddress(), port);
this.remoteAddress = remoteAddress;
this.connectionId = connectionId;
Expand All @@ -33,8 +34,7 @@ public ProxyClient(
}
}

@Override
public void run() {
private void run() {
WorldHost.LOGGER.info("Starting proxy client from {}", remoteAddress);
try {
final var is = socket.getInputStream();
Expand All @@ -58,6 +58,10 @@ public void run() {
WorldHost.LOGGER.info("Proxy client connection for {} closed", remoteAddress);
}

public void start() {
thread.start();
}

public void close() {
if (closed) return;
closed = true;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/github/gaming32/worldhost/WorldHost.java
Expand Up @@ -455,6 +455,7 @@ public static String getName(GameProfile profile) {
}

// From Apache Commons Lang StringUtils 3.10+
// TODO: Remove when 1.18.2 is minimum
public static <T extends CharSequence> T getIfBlank(final T str, final Supplier<T> defaultSupplier) {
return StringUtils.isBlank(str) ? defaultSupplier == null ? null : defaultSupplier.get() : str;
}
Expand Down
Expand Up @@ -47,7 +47,7 @@ public final class ProtocolClient implements AutoCloseable, ProxyPassthrough {
public ProtocolClient(String host, boolean successToast, boolean failureToast) {
this.originalHost = host;
final HostAndPort target = HostAndPort.fromString(host).withDefaultPort(9646);
final Thread connectionThread = new Thread(() -> {
Thread.ofVirtual().name("WH-ConnectionThread").start(() -> {
Socket socket = null;
try {
socket = new Socket(target.getHost(), target.getPort());
Expand Down Expand Up @@ -91,7 +91,7 @@ public ProtocolClient(String host, boolean successToast, boolean failureToast) {
}
final Socket fSocket = socket;

final Thread sendThread = new Thread(() -> {
final Thread sendThread = Thread.ofVirtual().name("WH-SendThread").start(() -> {
try {
final DataOutputStream dos = new DataOutputStream(fSocket.getOutputStream());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand All @@ -111,9 +111,9 @@ public ProtocolClient(String host, boolean successToast, boolean failureToast) {
WorldHost.LOGGER.error("Critical error in WH send thread", e);
}
close();
}, "WH-SendThread");
});

final Thread recvThread = new Thread(() -> {
Thread.ofVirtual().name("WH-RecvThread").start(() -> {
try {
final DataInputStream dis = new DataInputStream(fSocket.getInputStream());
while (!closed) {
Expand Down Expand Up @@ -151,10 +151,7 @@ public ProtocolClient(String host, boolean successToast, boolean failureToast) {
}
}
close();
}, "WH-RecvThread");

sendThread.start();
recvThread.start();
});

try {
sendThread.join();
Expand All @@ -174,9 +171,7 @@ public ProtocolClient(String host, boolean successToast, boolean failureToast) {
.show();
}
}
}, "WH-ConnectionThread");
connectionThread.setDaemon(true);
connectionThread.start();
});
}

public String getOriginalHost() {
Expand Down
Expand Up @@ -23,7 +23,7 @@ public class ProxyProtocolClient implements AutoCloseable, ProxyPassthrough {
public ProxyProtocolClient(String host, int port, long connectionId, String baseAddr, int mcPort) {
this.baseAddr = baseAddr;
this.mcPort = mcPort;
final Thread connectionThread = new Thread(() -> {
Thread.ofVirtual().name("WHEP-ConnectThread").start(() -> {
Socket socket = null;
try {
socket = new Socket(host, port);
Expand Down Expand Up @@ -52,7 +52,7 @@ public ProxyProtocolClient(String host, int port, long connectionId, String base
}
final Socket fSocket = socket;

final Thread sendThread = new Thread(() -> {
final Thread sendThread = Thread.ofVirtual().name("WHEP-SendThread").start(() -> {
try {
final DataOutputStream dos = new DataOutputStream(fSocket.getOutputStream());
while (!closed) {
Expand All @@ -69,9 +69,9 @@ public ProxyProtocolClient(String host, int port, long connectionId, String base
WorldHost.LOGGER.error("Critical error in WHEP send thread", e);
}
closed = true;
}, "WHEP-SendThread");
});

final Thread recvThread = new Thread(() -> {
Thread.ofVirtual().name("WHEP-RecvThread").start(() -> {
try {
final DataInputStream dis = new DataInputStream(fSocket.getInputStream());
while (!closed) {
Expand All @@ -91,10 +91,7 @@ public ProxyProtocolClient(String host, int port, long connectionId, String base
}
}
closed = true;
}, "WHEP-RecvThread");

sendThread.start();
recvThread.start();
});

try {
sendThread.join();
Expand All @@ -107,9 +104,7 @@ public ProxyProtocolClient(String host, int port, long connectionId, String base
} catch (IOException e) {
WorldHost.LOGGER.error("Failed to close WHEP socket.", e);
}
}, "WHEP-ConnectThread");
connectionThread.setDaemon(true);
connectionThread.start();
});
}

private void enqueue(ProxyMessage message) {
Expand Down
3 changes: 3 additions & 0 deletions version.gradle.kts
Expand Up @@ -435,4 +435,7 @@ tasks.withType<RemapJarTask> {
}
}
from("$rootDir/LICENSE")
mixinRemap {
disableRefmap()
}
}

0 comments on commit 509b2a4

Please sign in to comment.