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
  • Nested Class Summary

    Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler

    io.netty.channel.ChannelHandler.Sharable
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected String
     
    protected int
     
    static boolean
    Controls whether non-priority packets are handled in a separate event loop
  • Constructor Summary

    Constructors
    Constructor
    Description
    TcpSession(String host, int port, PacketProtocol protocol)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a listener to this session.
    void
    Calls an event on the listeners of this session.
    void
    Notifies all listeners that a packet was just received.
    void
    Notifies all listeners that a packet was just sent.
    void
    channelActive(io.netty.channel.ChannelHandlerContext ctx)
     
    void
    channelInactive(io.netty.channel.ChannelHandlerContext ctx)
     
    protected void
    channelRead0(io.netty.channel.ChannelHandlerContext ctx, Packet packet)
     
    void
    Connects this session to its host and port.
    void
    connect(boolean wait)
    Connects this session to its host and port.
    void
    connect(boolean wait, boolean transferring)
    Connects this session to its host and port.
    void
    disconnect(@NonNull net.kyori.adventure.text.Component reason, @Nullable Throwable cause)
    Disconnects the session.
    void
    Enables encryption for this session.
    void
    exceptionCaught(io.netty.channel.ChannelHandlerContext ctx, Throwable cause)
     
    io.netty.channel.Channel
     
    int
    Gets the compression packet length threshold for this session (-1 = disabled).
    <T> T
    getFlag(Flag<T> flag)
    Gets the value of the given flag as an instance of the given type.
    <T> T
    getFlag(Flag<T> flag, T def)
    Gets the value of the given flag as an instance of the given type.
    Gets this session's set flags.
    Gets the host the session is connected to.
    Gets the listeners listening on this session.
    Gets the local address of the session.
    Gets the packet protocol of the session.
    int
    Gets the port the session is connected to.
    Gets the remote address of the session.
    boolean
    hasFlag(Flag<?> flag)
    Checks whether this session has a flag set.
    boolean
    Returns true if the session is connected.
    void
    Removes a listener from this session.
    void
    send(Packet packet)
    Sends a packet.
    void
    setCompressionThreshold(int threshold, boolean validateDecompression)
    Sets the compression packet length threshold for this session (-1 = disabled).
    <T> void
    setFlag(Flag<T> flag, T value)
    Sets the value of a flag.
    void
    Sets the values for a collection of flags.

    Methods inherited from class io.netty.channel.SimpleChannelInboundHandler

    acceptInboundMessage, channelRead

    Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter

    channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered

    Methods inherited from class io.netty.channel.ChannelHandlerAdapter

    ensureNotSharable, handlerAdded, handlerRemoved, isSharable

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface io.netty.channel.ChannelHandler

    handlerAdded, handlerRemoved

    Methods inherited from interface org.geysermc.mcprotocollib.network.Session

    disconnect, disconnect, disconnect, getCodecHelper
  • 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
    • 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()
    • 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>