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

    • USE_EVENT_LOOP_FOR_PACKETS

      public static boolean USE_EVENT_LOOP_FOR_PACKETS
      Controls whether non-priority packets are handled in a separate event loop
    • 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 PacketProtocol 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.
    • callEvent

      public void callEvent(SessionEvent event)
      Description copied from interface: Session
      Calls an event on the listeners of this session.
      Specified by:
      callEvent in interface Session
      Parameters:
      event - Event to call.
    • 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, 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.
      validateDecompression - whether to validate that the decompression fits within size checks.
    • enableEncryption

      public void enableEncryption(PacketEncryption encryption)
      Description copied from interface: Session
      Enables encryption for this session.
      Specified by:
      enableEncryption in interface Session
      Parameters:
      encryption - the encryption 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 void send(Packet packet)
      Description copied from interface: Session
      Sends a packet.
      Specified by:
      send in interface Session
      Parameters:
      packet - Packet to send.
    • disconnect

      public void disconnect(@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.
    • getChannel

      public io.netty.channel.Channel getChannel()
    • 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>