fix(multiplayer): connection.

This commit is contained in:
huanghongxun 2022-09-24 23:03:07 +08:00
parent 0b61f9856c
commit 499a65d5fb
2 changed files with 22 additions and 12 deletions

View File

@ -22,10 +22,13 @@ import org.jackhuang.hmcl.event.EventManager;
import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.Lang;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*; import java.net.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel; import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel; import java.nio.channels.SocketChannel;
import java.nio.channels.UnresolvedAddressException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -74,20 +77,26 @@ public class LocalServerBroadcaster implements AutoCloseable {
if (!matcher.find()) { if (!matcher.find()) {
throw new MalformedURLException(); throw new MalformedURLException();
} }
try (SocketChannel forwardingSocket = SocketChannel.open(new InetSocketAddress(matcher.group(0), Lang.parseInt(matcher.group(1), 0))); try (Socket forwardingSocket = new Socket();
ServerSocketChannel serverSocket = ServerSocketChannel.open()) { ServerSocket serverSocket = new ServerSocket()) {
serverSocket.socket().bind(null); forwardingSocket.setSoTimeout(30000);
forwardingSocket.connect(new InetSocketAddress(matcher.group(1), Lang.parseInt(matcher.group(2), 0)));
Thread broadcastMOTDThread = new Thread(threadGroup, () -> broadcastMOTD(serverSocket.socket().getLocalPort()), "BroadcastMOTD"); serverSocket.bind(null);
Thread broadcastMOTDThread = new Thread(threadGroup, () -> broadcastMOTD(serverSocket.getLocalPort()), "BroadcastMOTD");
broadcastMOTDThread.start(); broadcastMOTDThread.start();
LOG.log(Level.INFO, "Listening " + serverSocket.getLocalSocketAddress());
while (running) { while (running) {
SocketChannel forwardedSocket = serverSocket.accept(); Socket forwardedSocket = serverSocket.accept();
LOG.log(Level.INFO, "Accepting client");
new Thread(threadGroup, () -> forwardTraffic(forwardingSocket, forwardedSocket), "Forward S->D").start(); new Thread(threadGroup, () -> forwardTraffic(forwardingSocket, forwardedSocket), "Forward S->D").start();
new Thread(threadGroup, () -> forwardTraffic(forwardedSocket, forwardingSocket), "Forward D->S").start(); new Thread(threadGroup, () -> forwardTraffic(forwardedSocket, forwardingSocket), "Forward D->S").start();
} }
} }
} catch (IOException e) { } catch (IOException | UnresolvedAddressException e) {
LOG.log(Level.WARNING, "Error in forwarding port", e); LOG.log(Level.WARNING, "Error in forwarding port", e);
} finally { } finally {
close(); close();
@ -95,13 +104,14 @@ public class LocalServerBroadcaster implements AutoCloseable {
} }
} }
private void forwardTraffic(SocketChannel src, SocketChannel dest) { private void forwardTraffic(Socket src, Socket dest) {
try { try(InputStream is = src.getInputStream(); OutputStream os = dest.getOutputStream()) {
ByteBuffer buf = ByteBuffer.allocate(1024); byte[] buf = new byte[1024];
while (true) { while (true) {
int len = src.read(buf); int len = is.read(buf, 0, buf.length);
if (len < 0) break; if (len < 0) break;
dest.write(buf); LOG.log(Level.INFO, "Forwarding buffer " + len);
os.write(buf, 0, len);
} }
} catch (IOException e) { } catch (IOException e) {
LOG.log(Level.WARNING, "Disconnected", e); LOG.log(Level.WARNING, "Disconnected", e);

View File

@ -243,7 +243,7 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
} }
private void onBroadcasterExit(Event event) { private void onBroadcasterExit(Event event) {
this.broadcaster.set(null); runInFX(() -> this.broadcaster.set(null));
} }
private void clearSession() { private void clearSession() {