Class TCPServer
The server also manages a dedicated heartbeat channel used
to detect disconnected clients and associate each heartbeat
connection with the corresponding ClientHandler.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BlockingQueue<NetworkEvent> Queue containing network events received from clients.private final CopyOnWriteArrayList<ClientHandler> List of active TCP client handlers.private final GameControllerGame controller used to manage the server-side game logic.private final intTCP port dedicated to heartbeat communication.private ServerSocketServer socket used to accept heartbeat connections.private final Map<String, ClientHandler> Temporary map associating each username with the correspondingClientHandlerwaiting for its heartbeat connection.private final LimitedMap<String, Boolean> Map storing the online/offline status of connected players.private final intMain TCP port used for standard client-server communication.private ServerSocketMain server socket used to accept client connections. -
Constructor Summary
ConstructorsConstructorDescriptionTCPServer(GameController controller, int port, int heartbeatPort, BlockingQueue<NetworkEvent> actionQueue, LimitedMap<String, Boolean> playerList) Constructs a TCP server with the required game and network components. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidAccepts heartbeat connections and associates them with the correct client handler.private ClientHandlercreateAndRegisterHandler(String username, Socket socket, ObjectOutputStream out, ObjectInputStream in) Creates aClientHandler, registers it inpendingHeartbeatandclientHandlers, but does NOT start its thread — callers start the thread after any extra setup (e.g. sending a model snapshot).disconnectedUsernames(Game game) Returns usernames of players currently marked as disconnected in the given game.voidNotifies all connected TCP clients of a new game model.voidnotifyAll(NetworkEvent event) Notifies connected TCP clients of a new network event.voidstart()Starts the TCP server and begins accepting client connections.
-
Field Details
-
port
private final int portMain TCP port used for standard client-server communication. -
heartbeatPort
private final int heartbeatPortTCP port dedicated to heartbeat communication. -
socketTCP
Main server socket used to accept client connections. -
heartbeatSocketTCP
Server socket used to accept heartbeat connections. -
controller
Game controller used to manage the server-side game logic. -
actionQueue
Queue containing network events received from clients. -
playerList
Map storing the online/offline status of connected players. -
clientHandlers
List of active TCP client handlers. -
pendingHeartbeat
Temporary map associating each username with the correspondingClientHandlerwaiting for its heartbeat connection.
-
-
Constructor Details
-
TCPServer
public TCPServer(GameController controller, int port, int heartbeatPort, BlockingQueue<NetworkEvent> actionQueue, LimitedMap<String, Boolean> playerList) Constructs a TCP server with the required game and network components.- Parameters:
controller- the game controller used to manage the game logic.port- the main TCP port used for client communication.heartbeatPort- the TCP port dedicated to heartbeat connections.actionQueue- the queue containing incoming network events.playerList- the map storing the connection status of the players.
-
-
Method Details
-
start
public void start()Starts the TCP server and begins accepting client connections.The method opens both the main TCP server socket and the dedicated heartbeat server socket. It then starts a separate thread for heartbeat connections and continuously waits for new players or reconnecting clients.
When a new connection is received, the first event must be an
AddPlayerrequest. Depending on the current server state, the connection is handled either as a new player joining the game or as a reconnection attempt. -
acceptHeartbeat
private void acceptHeartbeat()Accepts heartbeat connections and associates them with the correct client handler.Each client immediately sends its username on the heartbeat channel. The method uses that username to retrieve the pending
ClientHandlerand starts a dedicatedHeartbeatHandler. If no pending handler is found, the heartbeat socket is closed. -
notifyAll
Notifies connected TCP clients of a new network event.If the event does not represent an error, it is sent to all connected clients. If it represents an error, it is sent only to the client that requested the action.
- Parameters:
event- the network event to send to the clients.
-
notifyAll
Notifies all connected TCP clients of a new game model.- Parameters:
model- the updated mini model to send to the clients.
-
createAndRegisterHandler
private ClientHandler createAndRegisterHandler(String username, Socket socket, ObjectOutputStream out, ObjectInputStream in) Creates aClientHandler, registers it inpendingHeartbeatandclientHandlers, but does NOT start its thread — callers start the thread after any extra setup (e.g. sending a model snapshot). -
disconnectedUsernames
-