Class PacketHandler

java.lang.Object
com.hypixel.hytale.server.core.io.PacketHandler
All Implemented Interfaces:
ConnectionHandler, IPacketReceiver
Direct Known Subclasses:
GenericConnectionPacketHandler, GenericPacketHandler, InitialPacketHandler

public abstract class PacketHandler extends Object implements IPacketReceiver, ConnectionHandler
  • Field Details

    • MAX_PACKET_ID

      public static final int MAX_PACKET_ID
      Maximum packet ID for sizing arrays. Should be > max packet ID in the protocol.
      See Also:
    • DEFAULT_STREAM_PRIORITIES

      @Nonnull public static final Map<NetworkChannel, io.netty.handler.codec.quic.QuicStreamPriority> DEFAULT_STREAM_PRIORITIES
      Default QUIC stream priorities per network channel.
  • Constructor Details

    • PacketHandler

      public PacketHandler(@Nonnull ChannelConnection channel, @Nonnull ProtocolVersion protocolVersion)
      Default Constructor for a PacketHandler instance
      Parameters:
      channel - The channel affiliated with this packet handler
      protocolVersion - The network protocol version
  • Method Details

    • getChannel

      @Nonnull public ChannelConnection getChannel()
      Returns:
      the base channel (channel 0)
    • getChannel

      @Nullable public ChannelConnection getChannel(@Nonnull StreamType type)
      Returns the channel for the given stream type, or null if not set.
      Parameters:
      type - The stream type
      Returns:
      the channel for the stream type, or null if not set
    • getIdentifier

      @Nonnull public abstract String getIdentifier()
      Returns:
      the unique identifier for this handler
    • getProtocolVersion

      @Nonnull public ProtocolVersion getProtocolVersion()
      Returns:
      the network protocol version for the associated channel
    • registered

      public final void registered(@Nullable ConnectionHandler oldHandler)
      Description copied from interface: ConnectionHandler
      Called when this handler is registered on a connection, potentially replacing an old handler.
      Specified by:
      registered in interface ConnectionHandler
      Parameters:
      oldHandler - The previous handler, or null if this is the first handler
    • unregistered

      public final void unregistered(@Nullable ConnectionHandler newHandler)
      Description copied from interface: ConnectionHandler
      Called when this handler is unregistered from a connection, potentially being replaced by a new handler.
      Specified by:
      unregistered in interface ConnectionHandler
      Parameters:
      newHandler - The new handler replacing this one, or null
    • handle

      public void handle(@Nonnull ToServerPacket packet)
      Description copied from interface: ConnectionHandler
      Called to handle the given incoming packet.
      Specified by:
      handle in interface ConnectionHandler
      Parameters:
      packet - The packet to be handled
    • accept

      public abstract void accept(@Nonnull ToServerPacket packet)
      Called to accept the given incoming packet
      Parameters:
      packet - The packet instance to accept
    • logCloseMessage

      public void logCloseMessage()
      Description copied from interface: ConnectionHandler
      Called to log that this handler's connection has been closed.
      Specified by:
      logCloseMessage in interface ConnectionHandler
    • closed

      public void closed(@Nullable NetworkChannel networkChannel)
      Description copied from interface: ConnectionHandler
      Called when the affiliated network channel has been closed.
      Specified by:
      closed in interface ConnectionHandler
      Parameters:
      networkChannel - The network channel that was closed, or null if unknown
    • setQueuePackets

      public void setQueuePackets(boolean queuePackets)
      Sets whether to queue packets, or send them immediately
      Parameters:
      queuePackets - true if packets should be queued, false otherwise
    • tryFlush

      public void tryFlush()
      Called to attempt to flush the outbound packets for this handler
    • write

      public void write(@Nonnull ToClientPacket... packets)
      Writes the given packets outbound to the client
      Parameters:
      packets - The packets that are to be written
    • write

      public void write(@Nonnull ToClientPacket[] packets, @Nonnull ToClientPacket finalPacket)
      This is a helper method, useful when something being sent over the network is split into multiple data packets and then a final packet that constructs that data.
      Parameters:
      packets - The collection of data for the given packet
      finalPacket - The final packet that constructs the data
    • write

      public void write(@Nonnull ToClientPacket packet)
      Description copied from interface: IPacketReceiver
      Writes the packet, caching it first to prevent race conditions between the world thread and netty writing the packet.
      Specified by:
      write in interface IPacketReceiver
      Parameters:
      packet - the packet to send.
    • writeNoCache

      public void writeNoCache(@Nonnull ToClientPacket packet)
      Description copied from interface: IPacketReceiver
      Writes the packet without first caching the packet. This method should only be used if you don't store the sent packet to be reused. eg. packetHandler.writeNoCache(new Ping(...))
      Specified by:
      writeNoCache in interface IPacketReceiver
      Parameters:
      packet - the packet to send.
    • writePacket

      public boolean writePacket(@Nonnull ToClientPacket packet, boolean cache)
      Writes a packet to the player.
      Parameters:
      packet - The packet that is to be written
      cache - Determines whether to generate a cached copy of the packet before writing, ensuring that if the packet is modified after being written it can't race with the data actually being written via netty.
      Returns:
      true if the packet was written, false if a packet filter swallowed it.
    • disconnect

      public void disconnect(@Nonnull Message message)
      Called to disconnect this connection for the given reason.
      Parameters:
      message - The formatted reason in which the connection is being disconnected
    • disconnect

      public void disconnect(@Nonnull FormattedMessage message)
      Called to disconnect this connection for the given reason.
      Parameters:
      message - The formatted reason in which the connection is being disconnected
    • getPacketStatsRecorder

      @Nullable public PacketStatsRecorder getPacketStatsRecorder()
      Returns:
      the packet stats recorder for this connection, or null if not set
    • getPingInfo

      @Nonnull public PacketHandler.PingInfo getPingInfo(@Nonnull PongType pongType)
      Returns the ping info from the provided pong type
      Parameters:
      pongType - The given pong type
      Returns:
      the cached ping info instance
    • getOperationTimeoutThreshold

      public long getOperationTimeoutThreshold()
      The threshold (in milliseconds) used when timing out a client operation.
    • tickPing

      public void tickPing(float dt)
      Called to tick the ping packet timer, sending a ping packet if the timer has finished
      Parameters:
      dt - The delta time of the current tick
    • sendPing

      public void sendPing()
      Called to send a ping packet to the associated client
    • handlePong

      public void handlePong(@Nonnull Pong packet)
      Called to handle the provided pong packet
      Parameters:
      packet - The pong packet to be handled
    • getAuth

      @Nullable public PlayerAuthentication getAuth()
      Returns:
      the authentication instance for the associated player
    • getAccountProfile

      @Nullable public ProfileServiceClient.PublicGameProfile getAccountProfile()
      Returns the account to track for lookups, bans, and kicks.

      Override this method for authenticated handlers. The profile must be available before registration and remain available through cleanup. The account UUID and whether this method returns null must remain stable for this handler's lifetime. Keep this method nonblocking and free of side effects. Registry membership does not perform access checks.

      Returns:
      the account to track, or null to exclude this handler
    • stillActive

      public boolean stillActive()
      Returns:
      true if the channel is still active, false otherwise
    • getQueuedPacketsCount

      public int getQueuedPacketsCount()
      Returns:
      the count of packets that are currently queued
    • isLocalConnection

      public boolean isLocalConnection()
      Returns:
      true if the connection is local, false otherwise
    • isLANConnection

      public boolean isLANConnection()
      Returns:
      true if the connection is LAN, false otherwise
    • getSniHostname

      @Nullable public String getSniHostname()
    • checkStreamOpenRateLimit

      public boolean checkStreamOpenRateLimit()
      Checks if a stream open request is rate limited. Returns true if the request should be rejected due to rate limiting. Uses atomic compare-and-swap to prevent TOCTOU races.
      Returns:
      true if rate limited (reject), false if allowed
    • getDisconnectReason

      @Nonnull public PacketHandler.DisconnectReason getDisconnectReason()
      Returns:
      the current disconnect reason for the associated player
    • setClientReadyForChunksFuture

      public void setClientReadyForChunksFuture(@Nonnull CompletableFuture<Void> clientReadyFuture)
      Called to set the ready for chunks completable future, for when the client player is ready for chunks
      Parameters:
      clientReadyFuture - The completable future to be set
    • getClientReadyForChunksFuture

      @Nullable public CompletableFuture<Void> getClientReadyForChunksFuture()
      Returns:
      the completable future to execute when the client player is ready for chunks
    • getChannel

      @Nonnull public ChannelConnection getChannel(@Nonnull NetworkChannel networkChannel)
    • setChannel

      public void setChannel(@Nonnull NetworkChannel networkChannel, @Nonnull ChannelConnection channel)
    • setChannel

      public void setChannel(@Nonnull StreamType type, @Nullable ChannelConnection channel)
      Sets or removes a channel for the given auxiliary stream type.
      Parameters:
      type - The stream type
      channel - The channel, or null to remove
    • compareAndSetChannel

      public boolean compareAndSetChannel(@Nonnull StreamType type, @Nullable ChannelConnection expected, @Nullable ChannelConnection newValue)
      Atomically replaces a channel only if it currently matches the expected value. Prevents a closing old stream from clobbering a newly-registered replacement.
      Parameters:
      type - The stream type
      expected - The expected current channel (or null)
      newValue - The new channel to set (or null to remove)
      Returns:
      true if the swap succeeded, false if the current value didn't match expected
    • getAuxiliaryChannelCount

      public int getAuxiliaryChannelCount()
      Returns:
      the number of currently open auxiliary streams
    • logConnectionTimings

      public static void logConnectionTimings(@Nonnull ChannelConnection connection, @Nonnull String message, @Nonnull Level level)
      Called to log connection timings for a specific network event. Delegates to ChannelConnection.logConnectionTimings(String, Level).
      Parameters:
      connection - The associated connection to log timings for
      message - The readable message of the network event
      level - Determines the log level of the trace