Class HeartbeatHandler
- All Implemented Interfaces:
Runnable
This component listens for heartbeat PING messages from the client,
replies with PONG messages, and periodically checks whether the client
has remained silent for too long. If a timeout or communication error occurs,
both the heartbeat connection and the main client connection are disconnected.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final InputStreamInput stream ofsocketused to receivePINGbytes from the client.private longTimestamp of the last received heartbeat message.private final ClientHandlerMain client handler associated with this heartbeat connection.private final OutputStreamOutput stream ofsocketused to sendPONGbytes back to the client.private static final intByte value representing a heartbeat ping message.private static final intByte value representing a heartbeat pong response.private booleanIndicates whether the heartbeat handler is still active.private final SocketDedicated socket used solely for heartbeat ping/pong exchange.private final StringUsername of the client associated with this heartbeat connection.private final ScheduledExecutorServiceExecutor used to periodically check heartbeat timeouts. -
Constructor Summary
ConstructorsConstructorDescriptionHeartbeatHandler(String username, Socket socket, ClientHandler mainHandler) Constructs a heartbeat handler for the specified client. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidDisconnects the heartbeat channel and the associated main client connection.voidrun()Starts the heartbeat listening loop.private voidStarts the periodic watchdog task that detects heartbeat timeouts.
-
Field Details
-
username
Username of the client associated with this heartbeat connection. -
socket
Dedicated socket used solely for heartbeat ping/pong exchange. -
in
Input stream ofsocketused to receivePINGbytes from the client. -
out
Output stream ofsocketused to sendPONGbytes back to the client. -
PING
private static final int PINGByte value representing a heartbeat ping message.- See Also:
-
PONG
private static final int PONGByte value representing a heartbeat pong response.- See Also:
-
mainHandler
Main client handler associated with this heartbeat connection. -
lastReceivedTime
private volatile long lastReceivedTimeTimestamp of the last received heartbeat message. -
running
private volatile boolean runningIndicates whether the heartbeat handler is still active. -
watchdog
Executor used to periodically check heartbeat timeouts.
-
-
Constructor Details
-
HeartbeatHandler
public HeartbeatHandler(String username, Socket socket, ClientHandler mainHandler) throws IOException Constructs a heartbeat handler for the specified client.- Parameters:
username- the username of the connected client.socket- the socket dedicated to heartbeat communication.mainHandler- the main client handler associated with the same player.- Throws:
IOException- if the input or output stream cannot be obtained from the socket.
-
-
Method Details
-
run
public void run()Starts the heartbeat listening loop.The method activates the watchdog task, then waits for incoming heartbeat messages. When a
PINGis received, the last-received timestamp is updated and aPONGresponse is sent back to the client. If the stream is closed or an I/O error occurs, the client is disconnected. -
startWatchdog
private void startWatchdog()Starts the periodic watchdog task that detects heartbeat timeouts.If no heartbeat message is received within the configured silence threshold, the associated client is disconnected.
-
disconnect
private void disconnect()Disconnects the heartbeat channel and the associated main client connection.The handler is stopped, the watchdog task is terminated, the main
ClientHandleris disconnected, and the heartbeat socket is closed.
-