From 27a36df5be061ca68b88f1ffb0f88e6089d9ef38 Mon Sep 17 00:00:00 2001 From: Shuiling <1668589925@qq.com> Date: Thu, 12 Sep 2024 22:14:21 -0700 Subject: [PATCH] =?UTF-8?q?refactor(Communication):=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BB=A5=E5=A2=9E=E5=8A=A0=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=92=8C=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../catseedlogin/bukkit/Communication.java | 111 ++++++++++-------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java b/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java index 31e2833..e1e60a5 100644 --- a/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java +++ b/src/main/java/cc/baka9/catseedlogin/bukkit/Communication.java @@ -26,7 +26,7 @@ public static void socketServerStopAsync() { CatSeedLogin.instance.runTaskAsync(Communication::socketServerStop); } - public static void socketServerStop() { + public static synchronized void socketServerStop() { closeServerSocket(serverSocket); } @@ -34,68 +34,75 @@ public static void socketServerStartAsync() { CatSeedLogin.instance.runTaskAsync(Communication::socketServerStart); } - private static void socketServerStart() { - try { - InetAddress inetAddress = InetAddress.getByName(Config.BungeeCord.Host); - serverSocket = new ServerSocket(Integer.parseInt(Config.BungeeCord.Port), 50, inetAddress); - acceptConnections(serverSocket); - } catch (UnknownHostException e) { - CatSeedLogin.instance.getLogger().warning("无法解析域名或IP地址: " + e.getMessage()); - } catch (IOException e) { - CatSeedLogin.instance.getLogger().warning("启动Socket服务器时发生错误: " + e.getMessage()); - } +private static synchronized void socketServerStart() { + try { + InetAddress inetAddress = InetAddress.getByName(Config.BungeeCord.Host); + serverSocket = new ServerSocket(Integer.parseInt(Config.BungeeCord.Port), 50, inetAddress); + acceptConnections(serverSocket); + } catch (UnknownHostException e) { + CatSeedLogin.instance.getLogger().warning("无法解析域名或IP地址: " + e.getMessage()); + } catch (IOException e) { + CatSeedLogin.instance.getLogger().warning("启动Socket服务器时发生错误: " + e.getMessage()); } +} - private static void acceptConnections(ServerSocket serverSocket) { - while (!serverSocket.isClosed()) { - try { - Socket socket = serverSocket.accept(); - executorService.submit(() -> handleRequest(socket)); - } catch (IOException e) { - CatSeedLogin.instance.getLogger().warning("接受Socket连接时发生错误: " + e.getMessage()); +private static void acceptConnections(ServerSocket serverSocket) { + while (!serverSocket.isClosed()) { + try { + Socket socket = serverSocket.accept(); + executorService.submit(() -> handleRequest(socket)); + } catch (IOException e) { + if (serverSocket.isClosed()) { + break; } + CatSeedLogin.instance.getLogger().warning("接受Socket连接时发生错误: " + e.getMessage()); } } +} - private static void handleRequest(Socket socket) { - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { - String requestType = bufferedReader.readLine(); - if (requestType == null) { - return; - } - String playerName = bufferedReader.readLine(); - switch (requestType) { - case "Connect": - handleConnectRequest(socket, playerName); - break; - case "KeepLoggedIn": - String time = bufferedReader.readLine(); - String sign = bufferedReader.readLine(); - handleKeepLoggedInRequest(playerName, time, sign); - break; - default: - CatSeedLogin.instance.getLogger().warning("未知请求类型: " + requestType); - break; - } - } catch (IOException e) { - CatSeedLogin.instance.getLogger().warning("处理请求时发生错误: " + e.getMessage()); + +private static void handleRequest(Socket socket) { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + Socket autoCloseSocket = socket) { + String requestType = bufferedReader.readLine(); + if (requestType == null) { + return; } + String playerName = bufferedReader.readLine(); + switch (requestType) { + case "Connect": + handleConnectRequest(autoCloseSocket, playerName); + break; + case "KeepLoggedIn": + String time = bufferedReader.readLine(); + String sign = bufferedReader.readLine(); + handleKeepLoggedInRequest(playerName, time, sign); + break; + default: + CatSeedLogin.instance.getLogger().warning("未知请求类型: " + requestType); + break; + } + } catch (IOException e) { + CatSeedLogin.instance.getLogger().warning("处理请求时发生错误: " + e.getMessage()); } +} - private static void handleKeepLoggedInRequest(String playerName, String time, String sign) { - if (CommunicationAuth.encryption(playerName, time, Config.BungeeCord.AuthKey).equals(sign)) { - CatScheduler.runTask(() -> { - LoginPlayer lp = Cache.getIgnoreCase(playerName); - if (lp != null) { - LoginPlayerHelper.add(lp); - Player player = Bukkit.getPlayerExact(playerName); - if (player != null) { - player.updateInventory(); - } +private static void handleKeepLoggedInRequest(String playerName, String time, String sign) { + if (CommunicationAuth.encryption(playerName, time, Config.BungeeCord.AuthKey).equals(sign)) { + CatScheduler.runTask(() -> { + LoginPlayer lp = Cache.getIgnoreCase(playerName); + if (lp != null) { + LoginPlayerHelper.add(lp); + Player player = Bukkit.getPlayerExact(playerName); + if (player != null) { + player.updateInventory(); } - }); - } + } else { + CatSeedLogin.instance.getLogger().warning("玩家 " + playerName + " 未找到在缓存中。"); + } + }); } +} private static void handleConnectRequest(Socket socket, String playerName) { CatScheduler.runTask(() -> {