Class BasePacket

java.lang.Object
eu.cloudnetservice.driver.network.protocol.BasePacket
All Implemented Interfaces:
Packet

public class BasePacket extends Object implements Packet
The default implementation of a packet. Each subclass of a packet might implement the packet interface themselves, but it is much more convenient and easy to extend from this class.
Since:
4.0
  • Field Details

    • channel

      protected final int channel
    • dataBuf

      protected final DataBuf dataBuf
    • prioritized

      protected final boolean prioritized
    • creationStamp

      protected final Instant creationStamp
    • uniqueId

      protected UUID uniqueId
  • Constructor Details

    • BasePacket

      public BasePacket(int channel, @NonNull @NonNull DataBuf dataBuf)
      Constructs a new base packet instance.
      Parameters:
      channel - the channel to which the packet was sent.
      dataBuf - the buffer (or content) of the packet.
      Throws:
      NullPointerException - if the given buffer is null.
    • BasePacket

      public BasePacket(int channel, boolean prioritized, @NonNull @NonNull DataBuf dataBuf)
      Constructs a new base packet instance.
      Parameters:
      channel - the channel to which the packet was sent.
      prioritized - if the packet should be prioritized.
      dataBuf - the buffer (or content) of the packet.
      Throws:
      NullPointerException - if the given buffer is null.
  • Method Details

    • constructResponse

      @NonNull public @NonNull Packet constructResponse(@NonNull @NonNull DataBuf content)
      Constructs a new packet as a response to this packet. This is used for query communication where responding to a query is very common. The resulting packet will
      1. have a packet id set to -1 for query identification.
      2. have the same query unique id as this packet has.
      3. have the given content buffer as the content set.
      Specified by:
      constructResponse in interface Packet
      Parameters:
      content - the content of the response to this packet.
      Returns:
      a new packet representing a response to this packet.
    • uniqueId

      @Nullable public @Nullable UUID uniqueId()
      Get the unique id of this packet. The unique id of the packet is only set when this packet is a query packet and expects a response. In this case the response to this packet should have the packet id set to -1 and the same query unique id as this packet has. Normally a response is constructed by Packet.constructResponse(DataBuf).
      Specified by:
      uniqueId in interface Packet
      Returns:
      the unique id of this packet, or null if this packet is not a query packet.
    • uniqueId

      public void uniqueId(@Nullable @Nullable UUID uniqueId)
      Sets the unique id of this packet. If the unique id is set and the packet is sent the packet will be handled as a query packet. This method should not be used directly to set or change the unique id of a packet. The unique id handling is made by the query manager used to send the packet as a query.
      Specified by:
      uniqueId in interface Packet
      Parameters:
      uniqueId - the new unique id of this packet or null if this packet is not a query packet.
    • channel

      public int channel()
      Get the channel id to which this packet was sent. Listeners can be registered to that channel and will be notified if a packet was received for the specified channel. Each packet id should be unique within the whole network.
      Specified by:
      channel in interface Packet
      Returns:
      the channel id to which this packet was sent.
    • prioritized

      public boolean prioritized()
      Get if this packet is prioritized. If a packet is marked as high priority, it will instantly get handled on the receiving component without getting queued.

      This option should be used with care, as each thread which will normally read from the channel will be blocked with the packet handling of this packet. Blocking the handler thread too long will cause other packets to be delayed for no visible reason.

      Specified by:
      prioritized in interface Packet
      Returns:
      if this packet is prioritized.
    • readable

      public boolean readable()
      Get if this packet still has readable bytes left. Useful to verify that from a packet can actually be read instead of running into exceptions because the end of the buffer has been reached.
      Specified by:
      readable in interface Packet
      Returns:
      true if this packet still has data to read, false otherwise.
    • content

      @NonNull public @NonNull DataBuf content()
      Get the content of this packet. This method call always returns the same buffer to the caller, handling the buffer release and transactional reading is crucial to not run into exceptions when multiple handlers are handling the same packet.
      Specified by:
      content in interface Packet
      Returns:
      the content of this packet.
    • creation

      @NonNull public @NonNull Instant creation()
      Get an epoch timestamp of the creation time of this packet. When the packet is created by a decoder, the time will be used as the creation millis. There is no guarantee for this time to be the exact same time as when the packet gets sent to the component, nor when a listener first received the packet.
      Specified by:
      creation in interface Packet
      Returns:
      the creation timestamp of this packet.