Interface Session
- All Known Subinterfaces:
ClientSession,ServerSession
- All Known Implementing Classes:
ClientNetworkSession,NetworkSession,ServerNetworkSession
public interface Session
A network session.
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(SessionListener listener) Adds a listener to this session.voidcallEvent(SessionEvent event) Calls an event on the listeners of this session.voidcallPacketReceived(Packet packet) Notifies all listeners that a packet was just received.voidcallPacketSent(Packet packet) Notifies all listeners that a packet was just sent.default voiddisconnect(@NonNull String reason) Disconnects the session.default voiddisconnect(@NonNull String reason, @Nullable Throwable cause) Disconnects the session.default voiddisconnect(@NonNull net.kyori.adventure.text.Component reason) Disconnects the session.voiddisconnect(@NonNull net.kyori.adventure.text.Component reason, @Nullable Throwable cause) Disconnects the session.io.netty.channel.ChannelReturns the underlying netty channel of this session.default <T> TGets the value of the given flag as an instance of the given type.default <T> TgetFlags()Gets this session's set flags.<T> TgetFlagSupplied(Flag<T> flag, Supplier<T> defSupplier) Gets the value of the given flag as an instance of the given type.Gets the listeners listening on this session.Gets the local address of the session.Returns the executor that handles packet handling.Gets the packet protocol of the session.Gets the remote address of the session.booleanChecks whether this session has a flag set.booleanReturns true if the session is connected.voidremoveListener(SessionListener listener) Removes a listener from this session.default voidSends a packet.voidSends a packet and runs the specified callback when the packet has been sent.voidsetAutoRead(boolean autoRead) Auto read in netty means that the server is automatically reading from the channel.voidsetCompression(@Nullable CompressionConfig compressionConfig) Sets the compression config for this session.voidsetEncryption(@Nullable EncryptionConfig encryptionConfig) Sets encryption for this session.<T> voidSets the value of a flag.voidSets the values for a collection of flags.default voidswitchInboundState(Runnable switcher) Changes the inbound state of the session and then re-enables auto read.default voidswitchOutboundState(Runnable switcher) Flushes all packets that are due to be sent and changes the outbound state of the session.
-
Method Details
-
getLocalAddress
SocketAddress getLocalAddress()Gets the local address of the session.- Returns:
- The local address, or null if the session is not connected.
-
getRemoteAddress
SocketAddress getRemoteAddress()Gets the remote address of the session.- Returns:
- The remote address, or null if the session is not connected.
-
getPacketProtocol
MinecraftProtocol getPacketProtocol()Gets the packet protocol of the session.- Returns:
- The session's packet protocol.
-
getFlags
Gets this session's set flags. If this session belongs to a server, the server's flags will be included in the results.- Returns:
- This session's flags.
-
hasFlag
Checks whether this session has a flag set. If this session belongs to a server, the server's flags will also be checked.- Parameters:
flag- Flag to check for.- Returns:
- Whether this session has a flag set.
-
getFlag
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.- Type Parameters:
T- Type of the flag.- Parameters:
flag- Flag to check for.- Returns:
- Value of the flag.
- Throws:
IllegalStateException- If the flag's value isn't of the required type.
-
getFlag
- See Also:
-
getFlagSupplied
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.- Type Parameters:
T- Type of the flag.- Parameters:
flag- Flag to check for.defSupplier- Default value supplier.- Returns:
- Value of the flag.
- Throws:
IllegalStateException- If the flag's value isn't of the required type.
-
setFlag
Sets the value of a flag. This does not change a server's flags if this session belongs to a server.- Type Parameters:
T- Type of the flag.- Parameters:
flag- Flag to check for.value- Value to set the flag to.
-
setFlags
Sets the values for a collection of flags.- Parameters:
flags- Collection of flags
-
getListeners
List<SessionListener> getListeners()Gets the listeners listening on this session.- Returns:
- This session's listeners.
-
addListener
Adds a listener to this session.- Parameters:
listener- Listener to add.
-
removeListener
Removes a listener from this session.- Parameters:
listener- Listener to remove.
-
callEvent
Calls an event on the listeners of this session.- Parameters:
event- Event to call.
-
callPacketReceived
Notifies all listeners that a packet was just received.- Parameters:
packet- Packet to notify.
-
callPacketSent
Notifies all listeners that a packet was just sent.- Parameters:
packet- Packet to notify.
-
setCompression
Sets the compression config for this session.- Parameters:
compressionConfig- the compression to compress with, or null to disable compression
-
setEncryption
Sets encryption for this session.- Parameters:
encryptionConfig- the encryption to encrypt with, or null to disable encryption
-
isConnected
boolean isConnected()Returns true if the session is connected.- Returns:
- True if the session is connected.
-
send
Sends a packet.- Parameters:
packet- Packet to send.
-
send
Sends a packet and runs the specified callback when the packet has been sent.- Parameters:
packet- Packet to send.onSent- Callback to run when the packet has been sent.
-
disconnect
Disconnects the session. This method just wraps the reason into aComponent. It is recommended to use Components instead as they provide more flexibility.- Parameters:
reason- Reason for disconnecting.- See Also:
-
disconnect
Disconnects the session. This method just wraps the reason into aComponent. It is recommended to use Components instead as they provide more flexibility.- Parameters:
reason- Reason for disconnecting.cause- Throwable responsible for disconnecting.- See Also:
-
disconnect
default void disconnect(@NonNull net.kyori.adventure.text.Component reason) Disconnects the session.- Parameters:
reason- Reason for disconnecting.
-
disconnect
Disconnects the session.- Parameters:
reason- Reason for disconnecting.cause- Throwable responsible for disconnecting.
-
setAutoRead
void setAutoRead(boolean autoRead) 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 withswitchInboundState(Runnable).- Parameters:
autoRead- Whether to enable auto read. Default is true.
-
getChannel
io.netty.channel.Channel getChannel()Returns the underlying netty channel of this session.- Returns:
- The netty channel
-
getPacketHandlerExecutor
Executor getPacketHandlerExecutor()Returns the executor that handles packet handling.- Returns:
- The packet handler executor
-
switchInboundState
Changes the inbound state of the session and then re-enables auto read. This is used after a terminal packet was handled and the session is ready to receive more packets in the new state.- Parameters:
switcher- The runnable that switches the inbound state.
-
switchOutboundState
Flushes all packets that are due to be sent and changes the outbound state of the session. This makes sure no other threads have scheduled packets to be sent.- Parameters:
switcher- The runnable that switches the outbound state.
-