Class TcpSession

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<Packet>
org.geysermc.mcprotocollib.network.tcp.TcpSession
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, Session
Direct Known Subclasses:
TcpClientSession, TcpServerSession

public abstract class TcpSession extends io.netty.channel.SimpleChannelInboundHandler<Packet> implements Session
  • Field Details

    • ID

      public static String ID
    • READ_TIMEOUT_HANDLER_ID

      public static String READ_TIMEOUT_HANDLER_ID
    • WRITE_TIMEOUT_HANDLER_ID

      public static String WRITE_TIMEOUT_HANDLER_ID
    • PROXY_HANDLER_ID

      public static String PROXY_HANDLER_ID
    • HA_PROXY_ENCODER_ID

      public static String HA_PROXY_ENCODER_ID
    • host

      protected String host
    • port

      protected int port
    • disconnected

      protected boolean disconnected
  • Constructor Details

  • Method Details

    • connect

      public void connect()
      Description copied from interface: Session
      Connects this session to its host and port.
      Specified by:
      connect in interface Session
    • connect

      public void connect(boolean wait)
      Description copied from interface: Session
      Connects this session to its host and port.
      Specified by:
      connect in interface Session
      Parameters:
      wait - Whether to wait for the connection to be established before returning.
    • connect

      public void connect(boolean wait, boolean transferring)
      Description copied from interface: Session
      Connects this session to its host and port.
      Specified by:
      connect in interface Session
      Parameters:
      wait - Whether to wait for the connection to be established before returning.
      transferring - Whether the session is a client being transferred.
    • getHost

      public String getHost()
      Description copied from interface: Session
      Gets the host the session is connected to.
      Specified by:
      getHost in interface Session
      Returns:
      The connected host.
    • getPort

      public int getPort()
      Description copied from interface: Session
      Gets the port the session is connected to.
      Specified by:
      getPort in interface Session
      Returns:
      The connected port.
    • getLocalAddress

      public SocketAddress getLocalAddress()
      Description copied from interface: Session
      Gets the local address of the session.
      Specified by:
      getLocalAddress in interface Session
      Returns:
      The local address, or null if the session is not connected.
    • getRemoteAddress

      public SocketAddress getRemoteAddress()
      Description copied from interface: Session
      Gets the remote address of the session.
      Specified by:
      getRemoteAddress in interface Session
      Returns:
      The remote address, or null if the session is not connected.
    • getPacketProtocol

      public MinecraftProtocol getPacketProtocol()
      Description copied from interface: Session
      Gets the packet protocol of the session.
      Specified by:
      getPacketProtocol in interface Session
      Returns:
      The session's packet protocol.
    • getFlags

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

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

      public <T> T getFlag(Flag<T> flag)
      Description copied from interface: Session
      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.
      Specified by:
      getFlag in interface Session
      Type Parameters:
      T - Type of the flag.
      Parameters:
      flag - Flag to check for.
      Returns:
      Value of the flag.
    • getFlag

      public <T> T getFlag(Flag<T> flag, T def)
      Description copied from interface: Session
      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.
      Specified by:
      getFlag in interface Session
      Type Parameters:
      T - Type of the flag.
      Parameters:
      flag - Flag to check for.
      def - Default value of the flag.
      Returns:
      Value of the flag.
    • setFlag

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

      public void setFlags(Map<String,Object> flags)
      Description copied from interface: Session
      Sets the values for a collection of flags.
      Specified by:
      setFlags in interface Session
      Parameters:
      flags - Collection of flags
    • getListeners

      public List<SessionListener> getListeners()
      Description copied from interface: Session
      Gets the listeners listening on this session.
      Specified by:
      getListeners in interface Session
      Returns:
      This session's listeners.
    • addListener

      public void addListener(SessionListener listener)
      Description copied from interface: Session
      Adds a listener to this session.
      Specified by:
      addListener in interface Session
      Parameters:
      listener - Listener to add.
    • removeListener

      public void removeListener(SessionListener listener)
      Description copied from interface: Session
      Removes a listener from this session.
      Specified by:
      removeListener in interface Session
      Parameters:
      listener - Listener to remove.
    • callPacketSending

      public Packet callPacketSending(Packet packet)
      Specified by:
      callPacketSending in interface Session
    • callConnected

      public void callConnected()
      Specified by:
      callConnected in interface Session
    • callDisconnecting

      public void callDisconnecting(net.kyori.adventure.text.Component reason, Throwable cause)
      Specified by:
      callDisconnecting in interface Session
    • callDisconnected

      public void callDisconnected(net.kyori.adventure.text.Component reason, Throwable cause)
      Specified by:
      callDisconnected in interface Session
    • callPacketError

      public boolean callPacketError(Throwable throwable)
      Specified by:
      callPacketError in interface Session
    • callPacketReceived

      public void callPacketReceived(Packet packet)
      Description copied from interface: Session
      Notifies all listeners that a packet was just received.
      Specified by:
      callPacketReceived in interface Session
      Parameters:
      packet - Packet to notify.
    • callPacketSent

      public void callPacketSent(Packet packet)
      Description copied from interface: Session
      Notifies all listeners that a packet was just sent.
      Specified by:
      callPacketSent in interface Session
      Parameters:
      packet - Packet to notify.
    • getCompressionThreshold

      public int getCompressionThreshold()
      Description copied from interface: Session
      Gets the compression packet length threshold for this session (-1 = disabled).
      Specified by:
      getCompressionThreshold in interface Session
      Returns:
      This session's compression threshold.
    • setCompressionThreshold

      public void setCompressionThreshold(int threshold, int level, boolean validateDecompression)
      Description copied from interface: Session
      Sets the compression packet length threshold for this session (-1 = disabled).
      Specified by:
      setCompressionThreshold in interface Session
      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

      public void enableEncryption(SecretKey key)
      Description copied from interface: Session
      Enables encryption for this session.
      Specified by:
      enableEncryption in interface Session
      Parameters:
      key - the secret key to encrypt with
    • getConnectTimeout

      public int getConnectTimeout()
      Description copied from interface: Session
      Gets the connect timeout for this session in seconds.
      Specified by:
      getConnectTimeout in interface Session
      Returns:
      The session's connect timeout.
    • setConnectTimeout

      public void setConnectTimeout(int timeout)
      Description copied from interface: Session
      Sets the connect timeout for this session in seconds.
      Specified by:
      setConnectTimeout in interface Session
      Parameters:
      timeout - Connect timeout to set.
    • getReadTimeout

      public int getReadTimeout()
      Description copied from interface: Session
      Gets the read timeout for this session in seconds.
      Specified by:
      getReadTimeout in interface Session
      Returns:
      The session's read timeout.
    • setReadTimeout

      public void setReadTimeout(int timeout)
      Description copied from interface: Session
      Sets the read timeout for this session in seconds.
      Specified by:
      setReadTimeout in interface Session
      Parameters:
      timeout - Read timeout to set.
    • getWriteTimeout

      public int getWriteTimeout()
      Description copied from interface: Session
      Gets the write timeout for this session in seconds.
      Specified by:
      getWriteTimeout in interface Session
      Returns:
      The session's write timeout.
    • setWriteTimeout

      public void setWriteTimeout(int timeout)
      Description copied from interface: Session
      Sets the write timeout for this session in seconds.
      Specified by:
      setWriteTimeout in interface Session
      Parameters:
      timeout - Write timeout to set.
    • isConnected

      public boolean isConnected()
      Description copied from interface: Session
      Returns true if the session is connected.
      Specified by:
      isConnected in interface Session
      Returns:
      True if the session is connected.
    • send

      public Future<Void> send(@NotNull @NotNull Packet packet)
      Description copied from interface: Session
      Sends a packet.
      Specified by:
      send in interface Session
      Parameters:
      packet - Packet to send.
    • send

      public void send(@NonNull @NonNull Packet packet, @NonNull @NonNull io.netty.channel.ChannelFutureListener listener)
      Specified by:
      send in interface Session
    • sendDirect

      public Future<Void> sendDirect(@NotNull @NotNull Packet packet)
      Description copied from interface: Session
      Sends a packet without calling listeners
      Specified by:
      sendDirect in interface Session
      Parameters:
      packet - Packet to send
      Returns:
    • sendDelayedDirect

      public void sendDelayedDirect(@NotNull @NotNull Packet packet)
      Description copied from interface: Session
      Writes a packet without flushing
      Specified by:
      sendDelayedDirect in interface Session
      Parameters:
      packet - Packet to send
    • flush

      public void flush()
      Description copied from interface: Session
      Flushes all pending packets.
      Specified by:
      flush in interface Session
    • sendBundleDirect

      public void sendBundleDirect(@NonNull @NonNull Packet... packets)
      Specified by:
      sendBundleDirect in interface Session
    • sendBundleDirect

      public void sendBundleDirect(@NonNull @NonNull List<Packet> packets)
      Specified by:
      sendBundleDirect in interface Session
    • sendBundle

      public void sendBundle(@NonNull @NonNull Packet... packets)
      Specified by:
      sendBundle in interface Session
    • sendBundle

      public void sendBundle(@NonNull @NonNull List<Packet> packets)
      Specified by:
      sendBundle in interface Session
    • sendAsync

      public void sendAsync(@NotNull @NotNull Packet packet)
      Description copied from interface: Session
      Sends a packet on the event loop
      Specified by:
      sendAsync in interface Session
    • sendScheduledAsync

      public void sendScheduledAsync(@NonNull @NonNull Packet packet, long delay, TimeUnit unit)
      Specified by:
      sendScheduledAsync in interface Session
    • disconnect

      public void disconnect(@NonNull @NonNull net.kyori.adventure.text.Component reason, @Nullable Throwable cause)
      Description copied from interface: Session
      Disconnects the session.
      Specified by:
      disconnect in interface Session
      Parameters:
      reason - Reason for disconnecting.
      cause - Throwable responsible for disconnecting.
    • refreshReadTimeoutHandler

      protected void refreshReadTimeoutHandler()
    • refreshReadTimeoutHandler

      protected void refreshReadTimeoutHandler(io.netty.channel.Channel channel)
    • refreshWriteTimeoutHandler

      protected void refreshWriteTimeoutHandler()
    • refreshWriteTimeoutHandler

      protected void refreshWriteTimeoutHandler(io.netty.channel.Channel channel)
    • channelActive

      public void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Specified by:
      channelActive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelActive in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • channelInactive

      public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Specified by:
      channelInactive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelInactive in class io.netty.channel.ChannelInboundHandlerAdapter
      Throws:
      Exception
    • exceptionCaught

      public void exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelHandler
      Specified by:
      exceptionCaught in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      exceptionCaught in class io.netty.channel.ChannelInboundHandlerAdapter
    • channelRead0

      protected void channelRead0(io.netty.channel.ChannelHandlerContext ctx, Packet packet)
      Specified by:
      channelRead0 in class io.netty.channel.SimpleChannelInboundHandler<Packet>
    • getProtocol

      public MinecraftProtocol getProtocol()
    • getChannel

      public io.netty.channel.Channel getChannel()
    • isDisconnected

      public boolean isDisconnected()
    • setHost

      public void setHost(String host)
    • setPort

      public void setPort(int port)
    • setCompressionThreshold

      public void setCompressionThreshold(int compressionThreshold)
    • setListeners

      public void setListeners(SessionListener[] listeners)
    • setChannel

      public void setChannel(io.netty.channel.Channel channel)
    • setDisconnected

      public void setDisconnected(boolean disconnected)