mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-11 12:56:53 -04:00
fix(multiplayer): forget to respond handshake.
This commit is contained in:
parent
ef4f2ed791
commit
2cb70a41fb
@ -101,6 +101,7 @@ public class MultiplayerClient extends Thread {
|
||||
// We fail to establish the connection with server.
|
||||
|
||||
try {
|
||||
LOG.log(Level.WARNING, "Socket connection timeout, closing socket");
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
LOG.log(Level.WARNING, "Failed to close socket", e);
|
||||
|
@ -20,7 +20,7 @@ package org.jackhuang.hmcl.ui.multiplayer;
|
||||
import com.jfoenix.controls.JFXButton;
|
||||
import com.jfoenix.controls.JFXDialogLayout;
|
||||
import de.javawi.jstun.test.DiscoveryInfo;
|
||||
import de.javawi.jstun.test.FastDiscoveryTest;
|
||||
import de.javawi.jstun.test.DiscoveryTest;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.*;
|
||||
import javafx.collections.FXCollections;
|
||||
@ -128,7 +128,7 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
|
||||
|
||||
private void testNAT() {
|
||||
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();
|
||||
}).whenComplete(Schedulers.javafx(), (info, exception) -> {
|
||||
LOG.log(Level.INFO, "Nat test result " + MultiplayerPageSkin.getNATType(info), exception);
|
||||
|
@ -43,6 +43,7 @@ public class MultiplayerServer extends Thread {
|
||||
private final EventManager<MultiplayerChannel.CatoClient> onClientAdded = new EventManager<>();
|
||||
private final EventManager<MultiplayerChannel.CatoClient> onClientDisconnected = 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> nameClientMap = new ConcurrentHashMap<>();
|
||||
@ -71,6 +72,10 @@ public class MultiplayerServer extends Thread {
|
||||
return onKeepAlive;
|
||||
}
|
||||
|
||||
public EventManager<Event> onHandshake() {
|
||||
return onHandshake;
|
||||
}
|
||||
|
||||
public void startServer() throws IOException {
|
||||
startServer(0);
|
||||
}
|
||||
@ -178,6 +183,10 @@ public class MultiplayerServer extends Thread {
|
||||
endpoint.write(new KeepAliveResponse(System.currentTimeMillis()));
|
||||
|
||||
onKeepAlive.fireEvent(new Event(this));
|
||||
} else if (request instanceof HandshakeRequest) {
|
||||
endpoint.write(new HandshakeResponse());
|
||||
|
||||
onHandshake.fireEvent(new Event(this));
|
||||
} else {
|
||||
LOG.log(Level.WARNING, "Unrecognized packet from client " + targetSocket.getRemoteSocketAddress() + ":" + line);
|
||||
}
|
||||
|
@ -18,26 +18,38 @@
|
||||
package org.jackhuang.hmcl.ui.multiplayer;
|
||||
|
||||
import org.jackhuang.hmcl.util.Logging;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class MultiplayerClientServerTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void startServer() throws Exception {
|
||||
Logging.initForTest();
|
||||
int localPort = MultiplayerManager.findAvailablePort();
|
||||
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();
|
||||
|
||||
AtomicBoolean handshakeReceived = new AtomicBoolean(false);
|
||||
|
||||
server.onHandshake().register(event -> {
|
||||
handshakeReceived.set(true);
|
||||
});
|
||||
|
||||
server.onKeepAlive().register(event -> {
|
||||
client.interrupt();
|
||||
server.interrupt();
|
||||
});
|
||||
|
||||
server.join();
|
||||
|
||||
Assert.assertTrue(handshakeReceived.get());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user