package com.alterdekim.game.component; import com.alterdekim.game.config.ServerConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.IOException; import java.net.ServerSocket; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; @Slf4j @Component public class RTMPServer { @Autowired private ServerConfig config; private final ConcurrentHashMap clients; private ServerSocket serverSocket; @Autowired private GameServer gameServer; public RTMPServer() { this.clients = new ConcurrentHashMap<>(); } public void close( Integer id ) { clients.remove(id); } @Scheduled(fixedDelay = 5000) public void start() { try { this.serverSocket = new ServerSocket(config.getGameServerPort()); log.info("RTMPServer.start() started"); while(true) { Integer guid = (int) (Math.random() * 10000); RTMPListener listener = new RTMPListener(serverSocket.accept(), this, guid, gameServer); log.info("RTMPServer.start() got client"); this.clients.put(guid, listener); listener.start(); } } catch (IOException e) { log.error("RTMPServer.start() error", e); } finally { try { serverSocket.close(); } catch (IOException e) { log.error("RTMPServer.server.close() error", e); } } } }