2025-02-07 12:24:31 +03:00

52 lines
1.5 KiB
Java

package com.alterdekim.server.durak.component;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ConcurrentHashMap;
@Slf4j
@Component
public class GameServer {
/* ip: default.durak.rstgames.com */
private final Integer port = 10772; // 80
private final ConcurrentHashMap<Integer, GameListener> clients;
private ServerSocket serverSocket;
public GameServer() {
this.clients = new ConcurrentHashMap<>();
}
public void close( Integer id ) {
clients.remove(id);
}
@Scheduled(fixedDelay = 5000)
public void start() {
try {
this.serverSocket = new ServerSocket(port);
log.info("GameServer.start() started");
while(true) {
Integer guid = (int) (Math.random() * 10000);
GameListener listener = new GameListener(serverSocket.accept(), this, guid);
log.info("GameServer.start() got client");
this.clients.put(guid, listener);
listener.start();
}
} catch (IOException e) {
log.error("GameServer.start() error", e);
} finally {
try {
serverSocket.close();
} catch (IOException e) {
log.error("GameServer.server.close() error", e);
}
}
}
}