Class NeonHost

All Implemented Interfaces:
Lifecycle, AutoCloseable

public final class NeonHost extends AbstractLifecycle implements 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());
  • Constructor Details

    • NeonHost

      public NeonHost(int sessionId, String relayAddress, NeonConfig config) throws IOException
      Creates a new host for the given session.
      Parameters:
      sessionId - unique session identifier
      relayAddress - relay address in "host:port" format
      config - protocol configuration
      Throws:
      IOException - if the local socket cannot be bound
  • Method Details

    • setClientConnectCallback

      public void setClientConnectCallback(TriConsumer<Byte,String,Integer> cb)
      Callback fired when a client successfully joins or rejoins the session. Arguments are (clientId, playerName, sessionId).
    • setClientDenyCallback

      public void setClientDenyCallback(BiConsumer<String,String> cb)
      Callback fired when a connection request is denied. Arguments are (playerName, reason).
    • setClientDisconnectCallback

      public void setClientDisconnectCallback(Consumer<Byte> cb)
      Callback fired when a client disconnects. The argument is the disconnecting client's ID.
    • setPingReceivedCallback

      public void setPingReceivedCallback(Consumer<Byte> cb)
      Callback fired when a PING is received from a client. The argument is the sender's client ID.
    • setUnhandledPacketCallback

      public void setUnhandledPacketCallback(BiConsumer<Byte,Byte> cb)
      Callback fired for game-specific packets not handled by the protocol layer. Arguments are (packetType, senderId).
    • setGamePacketRegistry

      public void setGamePacketRegistry(GamePacketRegistry registry)
      Sets the game packet registry used to advertise custom packet types to connecting clients. If not set, an empty registry is sent.
    • doStart

      protected void doStart() throws Exception
      Specified by:
      doStart in class AbstractLifecycle
      Throws:
      Exception
    • doStop

      protected void doStop() throws Exception
      Specified by:
      doStop in class AbstractLifecycle
      Throws:
      Exception
    • startAndRun

      public void startAndRun() throws Exception
      Registers with the relay and then runs the packet processing loop until AbstractLifecycle.stop() is called. Intended to run in a dedicated virtual thread.
      Throws:
      Exception - if registration with the relay fails
    • sendPacket

      public void sendPacket(byte[] payload, byte packetType, byte destId) throws IOException
      Sends a game-specific payload through the relay.
      Parameters:
      payload - raw payload bytes
      packetType - application-defined packet type byte
      destId - destination client ID, or 0 to broadcast to all session peers
      Throws:
      IOException - if the send fails
    • getLocalAddress

      public InetSocketAddress getLocalAddress() throws IOException
      Returns the local address of the underlying UDP socket.
      Throws:
      IOException
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception