chat sync reconnect system
This commit is contained in:
parent
f06e6a6632
commit
62b0cf15bc
|
|
@ -30,7 +30,7 @@ mod_name=PlayerSync
|
||||||
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
|
||||||
mod_license=GPL-3.0 license
|
mod_license=GPL-3.0 license
|
||||||
# The mod version. See https://semver.org/
|
# The mod version. See https://semver.org/
|
||||||
mod_version=2.1.3
|
mod_version=2.1.4
|
||||||
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
|
||||||
# This should match the base package used for the mod sources.
|
# This should match the base package used for the mod sources.
|
||||||
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
|
||||||
|
|
|
||||||
|
|
@ -11,28 +11,45 @@ import java.io.IOException;
|
||||||
|
|
||||||
public class ChatSync {
|
public class ChatSync {
|
||||||
public static final Logger LOGGER = LogUtils.getLogger();
|
public static final Logger LOGGER = LogUtils.getLogger();
|
||||||
|
private static ChatSyncServer chatSyncServer;
|
||||||
|
private static ChatSyncClient chatSyncClient;
|
||||||
|
|
||||||
public static void register(){
|
public static void register(){
|
||||||
if(JdbcConfig.IS_CHAT_SERVER.get()) {
|
if(JdbcConfig.IS_CHAT_SERVER.get()) {
|
||||||
LOGGER.info("Trying to setup chat server at port " + JdbcConfig.CHAT_SERVER_PORT.get());
|
LOGGER.info("Trying to setup chat server at port " + JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
new Thread(()->{
|
new Thread(()->{
|
||||||
ChatSyncServer chatSyncServer = new ChatSyncServer();
|
chatSyncServer = new ChatSyncServer();
|
||||||
try {
|
try {
|
||||||
chatSyncServer.run();
|
chatSyncServer.run();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.error("Unable to start chat server", e);
|
LOGGER.error("Unable to start chat server", e);
|
||||||
}
|
}
|
||||||
}).start();
|
}, "ChatSync-Server").start();
|
||||||
}
|
}
|
||||||
|
|
||||||
new Thread(()->{
|
new Thread(()->{
|
||||||
|
try {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
LOGGER.info("Trying to connect to chat server "
|
LOGGER.info("Trying to connect to chat server "
|
||||||
+ JdbcConfig.CHAT_SERVER_IP.get()
|
+ JdbcConfig.CHAT_SERVER_IP.get()
|
||||||
+ ":"
|
+ ":"
|
||||||
+ JdbcConfig.CHAT_SERVER_PORT.get());
|
+ JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
ChatSyncClient chatSyncClient = new ChatSyncClient();
|
chatSyncClient = new ChatSyncClient();
|
||||||
chatSyncClient.run();
|
chatSyncClient.run();
|
||||||
}).start();
|
}, "ChatSync-Client").start();
|
||||||
NeoForge.EVENT_BUS.register(ChatSyncClient.class);
|
NeoForge.EVENT_BUS.register(ChatSyncClient.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void shutdown() {
|
||||||
|
if (chatSyncServer != null) {
|
||||||
|
chatSyncServer.shutdown();
|
||||||
|
}
|
||||||
|
if (chatSyncClient != null) {
|
||||||
|
chatSyncClient.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,14 @@ import net.neoforged.neoforge.event.ServerChatEvent;
|
||||||
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
|
||||||
import vip.fubuki.playersync.PlayerSync;
|
import vip.fubuki.playersync.PlayerSync;
|
||||||
import vip.fubuki.playersync.config.JdbcConfig;
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
import vip.fubuki.playersync.sync.ChatSync;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ChatSyncClient {
|
public class ChatSyncClient {
|
||||||
|
|
@ -21,31 +22,97 @@ public class ChatSyncClient {
|
||||||
static Socket clientSocket;
|
static Socket clientSocket;
|
||||||
static PrintWriter out;
|
static PrintWriter out;
|
||||||
|
|
||||||
|
private static volatile boolean running = true;
|
||||||
|
private static final int RECONNECT_DELAY = 5000;
|
||||||
|
private static final int MAX_RECONNECT_ATTEMPTS = 10;
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
int reconnectAttempts = 0;
|
||||||
clientSocket = new Socket(JdbcConfig.CHAT_SERVER_IP.get(), JdbcConfig.CHAT_SERVER_PORT.get());
|
|
||||||
out = new PrintWriter(clientSocket.getOutputStream(),true);
|
|
||||||
|
|
||||||
BufferedReader in = new BufferedReader(
|
while (running && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||||
new InputStreamReader(clientSocket.getInputStream()));
|
try {
|
||||||
|
PlayerSync.LOGGER.info("Connecting to chat server {}:{}",
|
||||||
|
JdbcConfig.CHAT_SERVER_IP.get(),
|
||||||
|
JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
|
|
||||||
String serverMessage;
|
clientSocket = new Socket();
|
||||||
while ((serverMessage = in.readLine()) != null) {
|
|
||||||
PlayerSync.LOGGER.info("Received message from chat server: " + serverMessage);
|
clientSocket.connect(
|
||||||
Component textComponents = Component.nullToEmpty(serverMessage);
|
new InetSocketAddress(
|
||||||
if(playerList!=null){
|
JdbcConfig.CHAT_SERVER_IP.get(),
|
||||||
playerList.broadcastSystemMessage(textComponents,false);
|
JdbcConfig.CHAT_SERVER_PORT.get()
|
||||||
|
),
|
||||||
|
10000
|
||||||
|
);
|
||||||
|
|
||||||
|
clientSocket.setSoTimeout(30000);
|
||||||
|
|
||||||
|
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||||
|
BufferedReader in = new BufferedReader(
|
||||||
|
new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
|
||||||
|
PlayerSync.LOGGER.info("Successfully connected to chat server");
|
||||||
|
reconnectAttempts = 0;
|
||||||
|
|
||||||
|
String serverMessage;
|
||||||
|
while (running && (serverMessage = in.readLine()) != null) {
|
||||||
|
PlayerSync.LOGGER.info("Received message from chat server: " + serverMessage);
|
||||||
|
Component textComponents = Component.nullToEmpty(serverMessage);
|
||||||
|
if(playerList != null){
|
||||||
|
if (playerList.getServer().isSameThread()) {
|
||||||
|
playerList.broadcastSystemMessage(textComponents, false);
|
||||||
|
} else {
|
||||||
|
playerList.getServer().execute(() ->
|
||||||
|
playerList.broadcastSystemMessage(textComponents, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (SocketTimeoutException e) {
|
||||||
|
PlayerSync.LOGGER.warn("Chat server connection timeout, reconnecting...");
|
||||||
|
} catch (IOException e) {
|
||||||
|
PlayerSync.LOGGER.error("Chat client connection error: {}", e.getMessage());
|
||||||
|
} finally {
|
||||||
|
closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (running && reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||||
|
reconnectAttempts++;
|
||||||
|
PlayerSync.LOGGER.warn("Attempting to reconnect to chat server ({}/{})",
|
||||||
|
reconnectAttempts, MAX_RECONNECT_ATTEMPTS);
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(RECONNECT_DELAY);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
e.printStackTrace();
|
|
||||||
reconnectClient();
|
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
|
||||||
|
PlayerSync.LOGGER.error("Failed to connect to chat server after {} attempts", MAX_RECONNECT_ATTEMPTS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reconnectClient() {
|
private void closeConnection() {
|
||||||
ChatSync.LOGGER.warn("TODO: implement reconnectClient()");
|
try {
|
||||||
//TODO
|
if (out != null) {
|
||||||
|
out.close();
|
||||||
|
out = null;
|
||||||
|
}
|
||||||
|
if (clientSocket != null && !clientSocket.isClosed()) {
|
||||||
|
clientSocket.close();
|
||||||
|
clientSocket = null;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
PlayerSync.LOGGER.error("Error closing connection: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
running = false;
|
||||||
|
closeConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeEvent
|
@SubscribeEvent
|
||||||
|
|
|
||||||
|
|
@ -1,63 +1,131 @@
|
||||||
package vip.fubuki.playersync.sync.chat;
|
package vip.fubuki.playersync.sync.chat;
|
||||||
|
|
||||||
|
import vip.fubuki.playersync.PlayerSync;
|
||||||
import vip.fubuki.playersync.config.JdbcConfig;
|
import vip.fubuki.playersync.config.JdbcConfig;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStream;
|
import java.io.PrintWriter;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketTimeoutException;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class ChatSyncServer {
|
public class ChatSyncServer {
|
||||||
static ServerSocket serverSocket;
|
static ServerSocket serverSocket;
|
||||||
static final Set<Socket> SocketList = ConcurrentHashMap.newKeySet();
|
static final Set<Socket> SocketList = ConcurrentHashMap.newKeySet();
|
||||||
static final ExecutorService executorService = Executors.newCachedThreadPool();
|
static final ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
|
private volatile boolean running = true;
|
||||||
|
|
||||||
public void run() throws IOException {
|
public void run() throws IOException {
|
||||||
serverSocket = new ServerSocket(JdbcConfig.CHAT_SERVER_PORT.get());
|
try {
|
||||||
while (!Thread.currentThread().isInterrupted()) {
|
serverSocket = new ServerSocket(JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
Socket newSocket = serverSocket.accept();
|
serverSocket.setReuseAddress(true);
|
||||||
SocketList.add(newSocket);
|
PlayerSync.LOGGER.info("Chat server started successfully on port {}", JdbcConfig.CHAT_SERVER_PORT.get());
|
||||||
executorService.submit(() -> handleClient(newSocket));
|
|
||||||
|
while (running && !Thread.currentThread().isInterrupted()) {
|
||||||
|
try {
|
||||||
|
Socket newSocket = serverSocket.accept();
|
||||||
|
newSocket.setSoTimeout(30000);
|
||||||
|
SocketList.add(newSocket);
|
||||||
|
executorService.submit(() -> handleClient(newSocket));
|
||||||
|
PlayerSync.LOGGER.info("New client connected, total clients: {}", SocketList.size());
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (running) {
|
||||||
|
PlayerSync.LOGGER.error("Error accepting client connection: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
shutdown();
|
||||||
}
|
}
|
||||||
serverSocket.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleClient(Socket socket) {
|
private void handleClient(Socket socket) {
|
||||||
try (InputStream inputStream = socket.getInputStream()) {
|
String clientInfo = socket.getInetAddress() + ":" + socket.getPort();
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int bytesRead;
|
try (BufferedReader reader = new BufferedReader(
|
||||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
new InputStreamReader(socket.getInputStream()))) {
|
||||||
String message = new String(buffer, 0, bytesRead);
|
|
||||||
|
String message;
|
||||||
|
while (running && (message = reader.readLine()) != null) {
|
||||||
|
PlayerSync.LOGGER.info("Received message from {}: {}", clientInfo, message);
|
||||||
broadcastMessage(socket, message);
|
broadcastMessage(socket, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} catch (SocketTimeoutException e) {
|
||||||
|
PlayerSync.LOGGER.warn("Client {} timeout", clientInfo);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
PlayerSync.LOGGER.error("Error handling client {}: {}", clientInfo, e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
SocketList.remove(socket);
|
SocketList.remove(socket);
|
||||||
try {
|
try {
|
||||||
socket.close();
|
if (!socket.isClosed()) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
PlayerSync.LOGGER.error("Error closing client socket: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
PlayerSync.LOGGER.info("Client disconnected, remaining clients: {}", SocketList.size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void broadcastMessage(Socket sender, String message) {
|
private void broadcastMessage(Socket sender, String message) {
|
||||||
for (Socket socket : SocketList) {
|
Iterator<Socket> iterator = SocketList.iterator();
|
||||||
if (!socket.equals(sender)) {
|
while (iterator.hasNext()) {
|
||||||
|
Socket socket = iterator.next();
|
||||||
|
if (!socket.equals(sender) && !socket.isClosed()) {
|
||||||
try {
|
try {
|
||||||
OutputStream outputStream = socket.getOutputStream();
|
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
|
||||||
outputStream.write(message.getBytes());
|
writer.println(message);
|
||||||
outputStream.flush();
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
PlayerSync.LOGGER.error("Error broadcasting to client, removing: {}", e.getMessage());
|
||||||
|
iterator.remove();
|
||||||
|
try {
|
||||||
|
socket.close();
|
||||||
|
} catch (IOException ex) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void shutdown() {
|
||||||
|
running = false;
|
||||||
|
try {
|
||||||
|
if (serverSocket != null && !serverSocket.isClosed()) {
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
PlayerSync.LOGGER.error("Error closing server socket: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Socket socket : SocketList) {
|
||||||
|
try {
|
||||||
|
if (!socket.isClosed()) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SocketList.clear();
|
||||||
|
|
||||||
|
executorService.shutdown();
|
||||||
|
try {
|
||||||
|
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
|
||||||
|
executorService.shutdownNow();
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
executorService.shutdownNow();
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user