Interface Session

All Known Implementing Classes:
TcpClientSession, TcpServerSession, TcpSession

public interface Session
A network session.
  • Method Details

    • connect

      void connect()
      Connects this session to its host and port.
    • connect

      void connect(boolean wait)
      Connects this session to its host and port.
      Parameters:
      wait - Whether to wait for the connection to be established before returning.
    • connect

      void connect(boolean wait, boolean transferring)
      Connects this session to its host and port.
      Parameters:
      wait - Whether to wait for the connection to be established before returning.
      transferring - Whether the session is a client being transferred.
    • getHost

      String getHost()
      Gets the host the session is connected to.
      Returns:
      The connected host.
    • getPort

      int getPort()
      Gets the port the session is connected to.
      Returns:
      The connected port.
    • getLocalAddress

      SocketAddress getLocalAddress()
      Gets the local address of the session.
      Returns:
      The local address, or null if the session is not connected.
    • getRemoteAddress

      SocketAddress getRemoteAddress()
      Gets the remote address of the session.
      Returns:
      The remote address, or null if the session is not connected.
    • getPacketProtocol

      MinecraftProtocol getPacketProtocol()
      Gets the packet protocol of the session.
      Returns:
      The session's packet protocol.
    • getCodecHelper

      PacketCodecHelper getCodecHelper()
      Gets the session's PacketCodecHelper.
      Returns:
      The session's packet codec helper.
    • getFlags

      Map<String,Object> getFlags()
      Gets this session's set flags. If this session belongs to a server, the server's flags will be included in the results.
      Returns:
      This session's flags.
    • hasFlag

      boolean hasFlag(Flag<?> flag)
      Checks whether this session has a flag set. If this session belongs to a server, the server's flags will also be checked.
      Parameters:
      flag - Flag to check for.
      Returns:
      Whether this session has a flag set.
    • getFlag

      <T> T getFlag(Flag<T> flag)
      Gets the value of the given flag as an instance of the given type. If this session belongs to a server, the server's flags will be checked for the flag as well.
      Type Parameters:
      T - Type of the flag.
      Parameters:
      flag - Flag to check for.
      Returns:
      Value of the flag.
      Throws:
      IllegalStateException - If the flag's value isn't of the required type.
    • getFlag

      <T> T getFlag(Flag<T> flag, T def)
      Gets the value of the given flag as an instance of the given type. If this session belongs to a server, the server's flags will be checked for the flag as well. If the flag is not set, the specified default value will be returned.
      Type Parameters:
      T - Type of the flag.
      Parameters:
      flag - Flag to check for.
      def - Default value of the flag.
      Returns:
      Value of the flag.
      Throws:
      IllegalStateException - If the flag's value isn't of the required type.
    • setFlag

      <T> void setFlag(Flag<T> flag, T value)
      Sets the value of a flag. This does not change a server's flags if this session belongs to a server.
      Type Parameters:
      T - Type of the flag.
      Parameters:
      flag - Flag to check for.
      value - Value to set the flag to.
    • setFlags

      void setFlags(Map<String,Object> flags)
      Sets the values for a collection of flags.
      Parameters:
      flags - Collection of flags
    • getListeners

      List<SessionListener> getListeners()
      Gets the listeners listening on this session.
      Returns:
      This session's listeners.
    • addListener

      void addListener(SessionListener listener)
      Adds a listener to this session.
      Parameters:
      listener - Listener to add.
    • removeListener

      void removeListener(SessionListener listener)
      Removes a listener from this session.
      Parameters:
      listener - Listener to remove.
    • callPacketReceived

      void callPacketReceived(Packet packet)
      Notifies all listeners that a packet was just received.
      Parameters:
      packet - Packet to notify.
    • callPacketSending

      Packet callPacketSending(Packet packet)
    • callConnected

      void callConnected()
    • callDisconnecting

      void callDisconnecting(net.kyori.adventure.text.Component reason, Throwable cause)
    • callDisconnected

      void callDisconnected(net.kyori.adventure.text.Component reason, Throwable cause)
    • callPacketSent

      void callPacketSent(Packet packet)
      Notifies all listeners that a packet was just sent.
      Parameters:
      packet - Packet to notify.
    • callPacketError

      boolean callPacketError(Throwable throwable)
    • getCompressionThreshold

      int getCompressionThreshold()
      Gets the compression packet length threshold for this session (-1 = disabled).
      Returns:
      This session's compression threshold.
    • setCompressionThreshold

      void setCompressionThreshold(int threshold, int level, boolean validateDecompression)
      Sets the compression packet length threshold for this session (-1 = disabled).
      Parameters:
      threshold - The new compression threshold.
      level - Higher level = more compression, but more CPU. -1 = default level
      validateDecompression - whether to validate that the decompression fits within size checks.
    • enableEncryption

      void enableEncryption(SecretKey key)
      Enables encryption for this session.
      Parameters:
      key - the secret key to encrypt with
    • getConnectTimeout

      int getConnectTimeout()
      Gets the connect timeout for this session in seconds.
      Returns:
      The session's connect timeout.
    • setConnectTimeout

      void setConnectTimeout(int timeout)
      Sets the connect timeout for this session in seconds.
      Parameters:
      timeout - Connect timeout to set.
    • getReadTimeout

      int getReadTimeout()
      Gets the read timeout for this session in seconds.
      Returns:
      The session's read timeout.
    • setReadTimeout

      void setReadTimeout(int timeout)
      Sets the read timeout for this session in seconds.
      Parameters:
      timeout - Read timeout to set.
    • getWriteTimeout

      int getWriteTimeout()
      Gets the write timeout for this session in seconds.
      Returns:
      The session's write timeout.
    • setWriteTimeout

      void setWriteTimeout(int timeout)
      Sets the write timeout for this session in seconds.
      Parameters:
      timeout - Write timeout to set.
    • isConnected

      boolean isConnected()
      Returns true if the session is connected.
      Returns:
      True if the session is connected.
    • send

      Future<Void> send(@NonNull Packet packet)
      Sends a packet.
      Parameters:
      packet - Packet to send.
    • send

      void send(@NonNull Packet packet, @NonNull io.netty.channel.ChannelFutureListener listener)
    • sendDirect

      Future<Void> sendDirect(@NonNull Packet packet)
      Sends a packet without calling listeners
      Parameters:
      packet - Packet to send
      Returns:
    • sendDelayedDirect

      void sendDelayedDirect(@NonNull Packet packet)
      Writes a packet without flushing
      Parameters:
      packet - Packet to send
    • flush

      void flush()
      Flushes all pending packets.
    • sendBundleDirect

      void sendBundleDirect(@NonNull Packet... packets)
    • sendBundleDirect

      void sendBundleDirect(@NonNull List<Packet> packets)
    • sendBundle

      void sendBundle(@NonNull List<Packet> packets)
    • sendBundle

      void sendBundle(@NonNull Packet... packets)
    • sendAsync

      void sendAsync(@NonNull Packet packet)
      Sends a packet on the event loop
    • sendScheduledAsync

      void sendScheduledAsync(@NonNull Packet packet, long delay, TimeUnit unit)
    • disconnect

      default void disconnect(@NonNull String reason)
      Disconnects the session. This method just wraps the reason into a Component. It is recommended to use Components instead as they provide more flexibility.
      Parameters:
      reason - Reason for disconnecting.
      See Also:
    • disconnect

      default void disconnect(@NonNull String reason, @Nullable Throwable cause)
      Disconnects the session. This method just wraps the reason into a Component. It is recommended to use Components instead as they provide more flexibility.
      Parameters:
      reason - Reason for disconnecting.
      cause - Throwable responsible for disconnecting.
      See Also:
    • disconnect

      default void disconnect(@NonNull net.kyori.adventure.text.Component reason)
      Disconnects the session.
      Parameters:
      reason - Reason for disconnecting.
    • disconnect

      void disconnect(@NonNull net.kyori.adventure.text.Component reason, @Nullable Throwable cause)
      Disconnects the session.
      Parameters:
      reason - Reason for disconnecting.
      cause - Throwable responsible for disconnecting.
    • setAutoRead

      void setAutoRead(boolean autoRead)
      Auto read in netty means that the server is automatically reading from the channel. Turning it off means that we won't get more packets being decoded until we turn it back on. We use this to hold off on reading packets until we are ready to process them. For example this is used for switching inbound states with switchInboundState(ProtocolState).
      Parameters:
      autoRead - Whether to enable auto read. Default is true.
    • getChannel

      io.netty.channel.Channel getChannel()
      Returns the underlying netty channel of this session.
      Returns:
      The netty channel
    • switchInboundState

      default void switchInboundState(ProtocolState protocolState)
      Changes the inbound state of the session and then re-enables auto read. This is used after a terminal packet was handled and the session is ready to receive more packets in the new state.
    • switchOutboundState

      default void switchOutboundState(ProtocolState protocolState)
      Flushes all packets that are due to be sent and changes the outbound state of the session. This makes sure no other threads have scheduled packets to be sent.