Class NeonHost
java.lang.Object
com.quietterminal.neon.core.AbstractLifecycle
com.quietterminal.neon.host.NeonHost
- All Implemented Interfaces:
Lifecycle, AutoCloseable
Session host for the Neon multiplayer protocol.
A NeonHost registers a session with a NeonRelay,
accepts client connections, manages session state, and reliably delivers PacketPayload.SessionConfig
to each connecting client. The host always occupies client ID 1; player IDs start at 2.
Typical usage:
NeonHost host = new NeonHost(sessionId, "relay.example.com:9000", NeonConfig.defaults());
host.setClientConnectCallback((id, name, sid) -> System.out.println(name + " joined"));
Thread.ofVirtual().start(() -> host.startAndRun());
-
Nested Class Summary
Nested classes/interfaces inherited from interface Lifecycle
Lifecycle.State -
Constructor Summary
ConstructorsConstructorDescriptionNeonHost(int sessionId, String relayAddress, NeonConfig config) Creates a new host for the given session. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()protected voiddoStart()protected voiddoStop()Returns the local address of the underlying UDP socket.voidsendPacket(byte[] payload, byte packetType, byte destId) Sends a game-specific payload through the relay.voidCallback fired when a client successfully joins or rejoins the session.voidCallback fired when a connection request is denied.voidCallback fired when a client disconnects.voidsetGamePacketRegistry(GamePacketRegistry registry) Sets the game packet registry used to advertise custom packet types to connecting clients.voidCallback fired when a PING is received from a client.voidCallback fired for game-specific packets not handled by the protocol layer.voidRegisters with the relay and then runs the packet processing loop untilAbstractLifecycle.stop()is called.Methods inherited from class AbstractLifecycle
getState, isRunning, start, stop
-
Constructor Details
-
NeonHost
Creates a new host for the given session.- Parameters:
sessionId- unique session identifierrelayAddress- relay address in"host:port"formatconfig- protocol configuration- Throws:
IOException- if the local socket cannot be bound
-
-
Method Details
-
setClientConnectCallback
Callback fired when a client successfully joins or rejoins the session. Arguments are(clientId, playerName, sessionId). -
setClientDenyCallback
Callback fired when a connection request is denied. Arguments are(playerName, reason). -
setClientDisconnectCallback
-
setPingReceivedCallback
-
setUnhandledPacketCallback
Callback fired for game-specific packets not handled by the protocol layer. Arguments are(packetType, senderId). -
setGamePacketRegistry
Sets the game packet registry used to advertise custom packet types to connecting clients. If not set, an empty registry is sent. -
doStart
- Specified by:
doStartin classAbstractLifecycle- Throws:
Exception
-
doStop
- Specified by:
doStopin classAbstractLifecycle- Throws:
Exception
-
startAndRun
Registers with the relay and then runs the packet processing loop untilAbstractLifecycle.stop()is called. Intended to run in a dedicated virtual thread.- Throws:
Exception- if registration with the relay fails
-
sendPacket
Sends a game-specific payload through the relay.- Parameters:
payload- raw payload bytespacketType- application-defined packet type bytedestId- destination client ID, or0to broadcast to all session peers- Throws:
IOException- if the send fails
-
getLocalAddress
Returns the local address of the underlying UDP socket.- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-