Class NetworkSession

java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<Packet>
org.geysermc.mcprotocollib.network.session.NetworkSession
All Implemented Interfaces:
io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler, Session
Direct Known Subclasses:
ClientNetworkSession, ServerNetworkSession

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

    • remoteAddress

      protected final SocketAddress remoteAddress
    • protocol

      protected final PacketProtocol protocol
    • packetHandlerExecutor

      protected final Executor packetHandlerExecutor
    • disconnected

      protected boolean disconnected
  • Constructor Details

  • Method Details

    • 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.
    • getFlagSupplied

      public <T> T getFlagSupplied(Flag<T> flag, Supplier<T> defSupplier)
      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:
      getFlagSupplied in interface Session
      Type Parameters:
      T - Type of the flag.
      Parameters:
      flag - Flag to check for.
      defSupplier - Default value supplier.
      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.
    • setCompression

      public void setCompression(@Nullable CompressionConfig compressionConfig)
      Description copied from interface: Session
      Sets the compression config for this session.
      Specified by:
      setCompression in interface Session
      Parameters:
      compressionConfig - the compression to compress with, or null to disable compression
    • setEncryption

      public void setEncryption(@Nullable EncryptionConfig encryptionConfig)
      Description copied from interface: Session
      Sets encryption for this session.
      Specified by:
      setEncryption in interface Session
      Parameters:
      encryptionConfig - the encryption to encrypt with, or null to disable encryption
    • 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(@NonNull Packet packet, @Nullable Runnable onSent)
      Description copied from interface: Session
      Sends a packet and runs the specified callback when the packet has been sent.
      Specified by:
      send in interface Session
      Parameters:
      packet - Packet to send.
      onSent - Callback to run when the packet has been sent.
    • 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.
    • setAutoRead

      public void setAutoRead(boolean autoRead)
      Description copied from interface: Session
      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 Session.switchInboundState(Runnable).
      Specified by:
      setAutoRead in interface Session
      Parameters:
      autoRead - Whether to enable auto read. Default is true.
    • getChannel

      public io.netty.channel.Channel getChannel()
      Description copied from interface: Session
      Returns the underlying netty channel of this session.
      Specified by:
      getChannel in interface Session
      Returns:
      The netty channel
    • getPacketHandlerExecutor

      public Executor getPacketHandlerExecutor()
      Description copied from interface: Session
      Returns the executor that handles packet handling.
      Specified by:
      getPacketHandlerExecutor in interface Session
      Returns:
      The packet handler executor
    • 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>