fix(multiplayer): does not show correct kick message.

This commit is contained in:
huanghongxun
2021-10-30 22:13:32 +08:00
parent f35073ffb5
commit 7380423eee
3 changed files with 19 additions and 4 deletions

View File

@@ -87,6 +87,7 @@ public class MultiplayerClient extends Thread {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8))) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8))) {
MultiplayerServer.Endpoint endpoint = new MultiplayerServer.Endpoint(socket, writer); MultiplayerServer.Endpoint endpoint = new MultiplayerServer.Endpoint(socket, writer);
socket.setSoTimeout(30000);
LOG.info("Connected to 127.0.0.1:" + port); LOG.info("Connected to 127.0.0.1:" + port);
endpoint.write(new HandshakeRequest()); endpoint.write(new HandshakeRequest());
@@ -107,7 +108,7 @@ public class MultiplayerClient extends Thread {
} catch (IOException e) { } catch (IOException e) {
LOG.log(Level.WARNING, "Failed to close socket", e); LOG.log(Level.WARNING, "Failed to close socket", e);
} }
}, 15 * 1000); }, 25 * 1000);
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {

View File

@@ -44,7 +44,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermission;
import java.util.*; import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.logging.Level; import java.util.logging.Level;
@@ -198,7 +197,7 @@ public final class MultiplayerManager {
task.cancel(); task.cancel();
}); });
client.onKicked().register(kickedEvent -> { client.onKicked().register(kickedEvent -> {
future.completeExceptionally(new CancellationException()); future.completeExceptionally(new KickedException(kickedEvent.getReason()));
session.stop(); session.stop();
task.cancel(); task.cancel();
}); });
@@ -596,6 +595,18 @@ public final class MultiplayerManager {
public static class ConnectionErrorException extends RuntimeException { public static class ConnectionErrorException extends RuntimeException {
} }
public static class KickedException extends RuntimeException {
private final String reason;
public KickedException(String reason) {
this.reason = reason;
}
public String getReason() {
return reason;
}
}
public static class CatoNotExistsException extends RuntimeException { public static class CatoNotExistsException extends RuntimeException {
private final Path file; private final Path file;

View File

@@ -345,7 +345,10 @@ public class MultiplayerPage extends DecoratorAnimatedPage implements DecoratorP
Throwable e = resolveException(t); Throwable e = resolveException(t);
if (e instanceof CancellationException) { if (e instanceof CancellationException) {
LOG.info("Connection rejected by the server"); LOG.info("Connection rejected by the server");
return i18n("multiplayer.session.join.rejected"); return i18n("message.cancelled");
} else if (e instanceof MultiplayerManager.KickedException) {
LOG.info("Kicked by server");
return i18n("multiplayer.session.join.kicked", localizeKickMessage(((MultiplayerManager.KickedException) e).getReason()));
} else if (e instanceof MultiplayerManager.CatoAlreadyStartedException) { } else if (e instanceof MultiplayerManager.CatoAlreadyStartedException) {
LOG.info("Cato already started"); LOG.info("Cato already started");
return i18n("multiplayer.session.error.already_started"); return i18n("multiplayer.session.error.already_started");