Class TcpSession

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<Packet>
com.github.steveice10.packetlib.tcp.TcpSession
All Implemented Interfaces:
Session, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
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
    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)
     
    void
    Connects this session to its host and port.
    void
    connect(boolean wait)
    Connects this session to its host and port.
    void
    Disconnects the session.
    void
    disconnect(String reason, Throwable cause)
    Disconnects the session.
    void
    disconnect(net.kyori.adventure.text.Component reason)
    Disconnects the session.
    void
    disconnect(net.kyori.adventure.text.Component reason, 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).
    int
    Gets the connect timeout for this session in seconds.
    <T> T
    Gets the value of the given flag as an instance of the given type.
    <T> T
    getFlag(String key, 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.
    int
    Gets the read timeout for this session in seconds.
    Gets the remote address of the session.
    int
    Gets the write timeout for this session in seconds.
    boolean
    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).
    void
    setConnectTimeout(int timeout)
    Sets the connect timeout for this session in seconds.
    void
    setFlag(String key, Object value)
    Sets the value of a flag.
    void
    setReadTimeout(int timeout)
    Sets the read timeout for this session in seconds.
    void
    setWriteTimeout(int timeout)
    Sets the write timeout for this session in seconds.

    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

    handlerAdded, handlerRemoved, isSharable

    Methods inherited from class java.lang.Object

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

    Methods inherited from interface io.netty.channel.ChannelHandler

    handlerAdded, handlerRemoved

    Methods inherited from interface com.github.steveice10.packetlib.Session

    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
  • 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.
    • 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(String key)
      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:
      key - Key of the flag to check for.
      Returns:
      Whether this session has a flag set.
    • getFlag

      public <T> T getFlag(String key)
      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:
      key - Key of the flag.
      Returns:
      Value of the flag.
    • getFlag

      public <T> T getFlag(String key, 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:
      key - Key of the flag.
      def - Default value of the flag.
      Returns:
      Value of the flag.
    • setFlag

      public void setFlag(String key, Object 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
      Parameters:
      key - Key of the flag.
      value - Value to set the flag to.
    • 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(String reason)
      Description copied from interface: Session
      Disconnects the session.
      Specified by:
      disconnect in interface Session
      Parameters:
      reason - Reason for disconnecting.
    • disconnect

      public void disconnect(String reason, 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.
    • disconnect

      public void disconnect(net.kyori.adventure.text.Component reason)
      Description copied from interface: Session
      Disconnects the session.
      Specified by:
      disconnect in interface Session
      Parameters:
      reason - Reason for disconnecting.
    • disconnect

      public void disconnect(net.kyori.adventure.text.Component reason, 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