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

      PacketProtocol 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.
    • callEvent

      void callEvent(SessionEvent event)
      Calls an event on the listeners of this session.
      Parameters:
      event - Event to call.
    • callPacketReceived

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

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

      void setCompression(@Nullable CompressionConfig compressionConfig)
      Sets the compression config for this session.
      Parameters:
      compressionConfig - the compression to compress with, or null to disable compression
    • setEncryption

      void setEncryption(@Nullable EncryptionConfig encryptionConfig)
      Sets encryption for this session.
      Parameters:
      encryptionConfig - the encryption to encrypt with, or null to disable encryption
    • isConnected

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

      default void send(@NonNull Packet packet)
      Sends a packet.
      Parameters:
      packet - Packet to send.
    • send

      void send(@NonNull Packet packet, @Nullable Runnable onSent)
      Sends a packet and runs the specified callback when the packet has been sent.
      Parameters:
      packet - Packet to send.
      onSent - Callback to run when the packet has been sent.
    • 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(Runnable).
      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(Runnable switcher)
      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.
      Parameters:
      switcher - The runnable that switches the inbound state.
    • switchOutboundState

      default void switchOutboundState(Runnable switcher)
      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.
      Parameters:
      switcher - The runnable that switches the outbound state.