Class NeonClient
java.lang.Object
com.quietterminal.neon.core.AbstractLifecycle
com.quietterminal.neon.client.NeonClient
- All Implemented Interfaces:
Lifecycle, AutoCloseable
Relay-connected UDP client for the Neon multiplayer protocol.
A NeonClient connects to a session hosted by a NeonHost
through a NeonRelay. The connection handshake is synchronous;
ongoing packet processing is driven either by calling run() in a dedicated thread or by
periodically calling processPackets() from the game loop.
Typical usage:
NeonClient client = new NeonClient("player1", NeonConfig.defaults());
client.setSessionConfigCallback(cfg -> System.out.println("tick rate: " + cfg.tickRate()));
if (client.connect(sessionId, "relay.example.com:9000")) {
Thread.ofVirtual().start(client::run);
}
-
Nested Class Summary
Nested classes/interfaces inherited from interface Lifecycle
Lifecycle.State -
Constructor Summary
ConstructorsConstructorDescriptionNeonClient(String name, NeonConfig config) Creates a new client with the given display name and configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()booleanConnects to a session through a relay.protected voiddoStart()protected voiddoStop()Returns the client ID assigned by the host, ornullif not yet connected.Returns the local address of the underlying UDP socket.voidDrains all currently buffered inbound packets and fires the relevant callbacks.booleanAttempts to rejoin the last session using the stored session token.booleanreconnect(int maxAttempts) Attempts to rejoin the last session using the stored session token, retrying up tomaxAttemptstimes with exponential backoff.voidrun()Drives the client processing loop untilAbstractLifecycle.stop()is called or an unrecoverable IO error occurs.voidsendPacket(byte[] payload, byte packetType, byte destId) Sends a game-specific payload through the relay.voidCallback fired when aPacketPayload.DisconnectNoticeis received for another peer.voidCallback fired when the host broadcasts itsPacketPayload.PacketTypeRegistry.voidCallback fired when aPacketPayload.Pongis received in response to an auto-ping.voidCallback fired when the host delivers aPacketPayload.SessionConfig.voidCallback fired for packets whose type is not handled internally.Methods inherited from class AbstractLifecycle
getState, isRunning, start, stop
-
Constructor Details
-
NeonClient
Creates a new client with the given display name and configuration. Binds a local UDP socket on an OS-assigned port.- Parameters:
name- player display name sent to the host during connectionconfig- protocol configuration- Throws:
IOException- if the local socket cannot be bound
-
-
Method Details
-
connect
Connects to a session through a relay. Blocks until the relay accepts or denies the connection, or until the connection timeout elapses.- Parameters:
sessionId- the session to joinrelayAddress- relay address in"host:port"format- Returns:
trueif the connection was accepted;falseif denied or timed out
-
doStart
- Specified by:
doStartin classAbstractLifecycle- Throws:
Exception
-
doStop
- Specified by:
doStopin classAbstractLifecycle- Throws:
Exception
-
run
public void run()Drives the client processing loop untilAbstractLifecycle.stop()is called or an unrecoverable IO error occurs. Drains inbound packets and fires auto-pings on each iteration. Intended to run in a dedicated virtual thread:Thread.ofVirtual().start(client::run); -
processPackets
Drains all currently buffered inbound packets and fires the relevant callbacks. Use this instead ofrun()when integrating with an external game loop.- Throws:
IOException- if a socket error occurs while receiving
-
reconnect
public boolean reconnect()Attempts to rejoin the last session using the stored session token. Uses the max reconnect attempts from configuration with exponential backoff. Creates a new socket if the existing one is closed.- Returns:
trueif reconnection succeeded
-
reconnect
public boolean reconnect(int maxAttempts) Attempts to rejoin the last session using the stored session token, retrying up tomaxAttemptstimes with exponential backoff.- Parameters:
maxAttempts- maximum number of reconnect attempts- Returns:
trueif reconnection succeeded
-
sendPacket
Sends a game-specific payload through the relay.- Parameters:
payload- raw payload bytespacketType- application-defined packet type byte registered viaGamePacketRegistrydestId- destination client ID, or0to broadcast to all session peers- Throws:
IOException- if the send fails
-
setPongCallback
Callback fired when aPacketPayload.Pongis received in response to an auto-ping. -
setSessionConfigCallback
Callback fired when the host delivers aPacketPayload.SessionConfig. The client ACKs automatically. -
setPacketTypeRegistryCallback
Callback fired when the host broadcasts itsPacketPayload.PacketTypeRegistry. -
setUnhandledPacketCallback
Callback fired for packets whose type is not handled internally. Arguments are(packetType, senderId). -
setDisconnectCallback
Callback fired when aPacketPayload.DisconnectNoticeis received for another peer. The argument is the disconnecting client's ID. -
getClientId
Returns the client ID assigned by the host, ornullif not yet connected. -
getLocalAddress
Returns the local address of the underlying UDP socket.- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-