fix(multiplayer): forget to respond handshake.

This commit is contained in:
huanghongxun 2021-10-13 13:48:47 +08:00
parent ef4f2ed791
commit 2cb70a41fb
4 changed files with 26 additions and 4 deletions

View File

@ -101,6 +101,7 @@ public class MultiplayerClient extends Thread {
// We fail to establish the connection with server. // We fail to establish the connection with server.
try { try {
LOG.log(Level.WARNING, "Socket connection timeout, closing socket");
socket.close(); socket.close();
} catch (IOException e) { } catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close socket", e); LOG.log(Level.WARNING, "Failed to close socket", e);

View File

@ -20,7 +20,7 @@ package org.jackhuang.hmcl.ui.multiplayer;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialogLayout; import com.jfoenix.controls.JFXDialogLayout;
import de.javawi.jstun.test.DiscoveryInfo; import de.javawi.jstun.test.DiscoveryInfo;
import de.javawi.jstun.test.FastDiscoveryTest; import de.javawi.jstun.test.DiscoveryTest;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.*; import javafx.beans.property.*;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
@ -128,7 +128,7 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
private void testNAT() { private void testNAT() {
Task.supplyAsync(() -> { Task.supplyAsync(() -> {
FastDiscoveryTest tester = new FastDiscoveryTest(null, 0, "stun.stunprotocol.org", 3478); DiscoveryTest tester = new DiscoveryTest(null, 0, "stun.stunprotocol.org", 3478);
return tester.test(); return tester.test();
}).whenComplete(Schedulers.javafx(), (info, exception) -> { }).whenComplete(Schedulers.javafx(), (info, exception) -> {
LOG.log(Level.INFO, "Nat test result " + MultiplayerPageSkin.getNATType(info), exception); LOG.log(Level.INFO, "Nat test result " + MultiplayerPageSkin.getNATType(info), exception);

View File

@ -43,6 +43,7 @@ public class MultiplayerServer extends Thread {
private final EventManager<MultiplayerChannel.CatoClient> onClientAdded = new EventManager<>(); private final EventManager<MultiplayerChannel.CatoClient> onClientAdded = new EventManager<>();
private final EventManager<MultiplayerChannel.CatoClient> onClientDisconnected = new EventManager<>(); private final EventManager<MultiplayerChannel.CatoClient> onClientDisconnected = new EventManager<>();
private final EventManager<Event> onKeepAlive = new EventManager<>(); private final EventManager<Event> onKeepAlive = new EventManager<>();
private final EventManager<Event> onHandshake = new EventManager<>();
private final Map<String, Endpoint> clients = new ConcurrentHashMap<>(); private final Map<String, Endpoint> clients = new ConcurrentHashMap<>();
private final Map<String, Endpoint> nameClientMap = new ConcurrentHashMap<>(); private final Map<String, Endpoint> nameClientMap = new ConcurrentHashMap<>();
@ -71,6 +72,10 @@ public class MultiplayerServer extends Thread {
return onKeepAlive; return onKeepAlive;
} }
public EventManager<Event> onHandshake() {
return onHandshake;
}
public void startServer() throws IOException { public void startServer() throws IOException {
startServer(0); startServer(0);
} }
@ -178,6 +183,10 @@ public class MultiplayerServer extends Thread {
endpoint.write(new KeepAliveResponse(System.currentTimeMillis())); endpoint.write(new KeepAliveResponse(System.currentTimeMillis()));
onKeepAlive.fireEvent(new Event(this)); onKeepAlive.fireEvent(new Event(this));
} else if (request instanceof HandshakeRequest) {
endpoint.write(new HandshakeResponse());
onHandshake.fireEvent(new Event(this));
} else { } else {
LOG.log(Level.WARNING, "Unrecognized packet from client " + targetSocket.getRemoteSocketAddress() + ":" + line); LOG.log(Level.WARNING, "Unrecognized packet from client " + targetSocket.getRemoteSocketAddress() + ":" + line);
} }

View File

@ -18,26 +18,38 @@
package org.jackhuang.hmcl.ui.multiplayer; package org.jackhuang.hmcl.ui.multiplayer;
import org.jackhuang.hmcl.util.Logging; import org.jackhuang.hmcl.util.Logging;
import org.junit.Assert;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.util.concurrent.atomic.AtomicBoolean;
public class MultiplayerClientServerTest { public class MultiplayerClientServerTest {
@Test @Test
@Ignore @Ignore
public void startServer() throws Exception { public void startServer() throws Exception {
Logging.initForTest(); Logging.initForTest();
int localPort = MultiplayerManager.findAvailablePort();
MultiplayerServer server = new MultiplayerServer(1000, true); MultiplayerServer server = new MultiplayerServer(1000, true);
server.startServer(44444); server.startServer(localPort);
MultiplayerClient client = new MultiplayerClient("username", 44444); MultiplayerClient client = new MultiplayerClient("username", localPort);
client.start(); client.start();
AtomicBoolean handshakeReceived = new AtomicBoolean(false);
server.onHandshake().register(event -> {
handshakeReceived.set(true);
});
server.onKeepAlive().register(event -> { server.onKeepAlive().register(event -> {
client.interrupt(); client.interrupt();
server.interrupt(); server.interrupt();
}); });
server.join(); server.join();
Assert.assertTrue(handshakeReceived.get());
} }
} }