|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2026 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/Geyser |
| 24 | + */ |
| 25 | + |
| 26 | +package org.geysermc.geyser.network; |
| 27 | + |
| 28 | +import io.netty.channel.Channel; |
| 29 | +import io.netty.channel.DefaultEventLoopGroup; |
| 30 | +import io.netty.util.concurrent.DefaultThreadFactory; |
| 31 | +import lombok.Getter; |
| 32 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 33 | +import org.cloudburstmc.protocol.bedrock.BedrockPeer; |
| 34 | +import org.cloudburstmc.protocol.bedrock.BedrockServerSession; |
| 35 | +import org.cloudburstmc.protocol.bedrock.BedrockSession; |
| 36 | +import org.cloudburstmc.protocol.bedrock.netty.codec.packet.BedrockPacketCodec; |
| 37 | +import org.geysermc.geyser.GeyserImpl; |
| 38 | +import org.geysermc.geyser.session.GeyserSession; |
| 39 | + |
| 40 | +/** |
| 41 | + * Transport-agnostic logic for turning an established {@link BedrockServerSession} into a fully wired Geyser |
| 42 | + * session. Both the built-in RakNet transport (via {@link GeyserServerInitializer}) and extension-provided |
| 43 | + * transports funnel through here, so session creation stays consistent regardless of |
| 44 | + * how the connection was established. |
| 45 | + */ |
| 46 | +public class GeyserSessionInitializer { |
| 47 | + private final GeyserImpl geyser; |
| 48 | + |
| 49 | + /** |
| 50 | + * The event loop group {@link GeyserSession}s run their per-player logic on. Shared across all transports. |
| 51 | + */ |
| 52 | + // There is a constructor that doesn't require inputting threads, but older Netty versions don't have it |
| 53 | + @Getter |
| 54 | + private final DefaultEventLoopGroup eventLoopGroup = new DefaultEventLoopGroup(0, new DefaultThreadFactory("Geyser player thread")); |
| 55 | + |
| 56 | + public GeyserSessionInitializer(GeyserImpl geyser) { |
| 57 | + this.geyser = geyser; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a {@link BedrockPeer} that produces Geyser sessions. Useful for transports that build their own |
| 62 | + * Netty pipeline by hand (instead of extending cloudburst's |
| 63 | + * {@link org.cloudburstmc.protocol.bedrock.netty.initializer.BedrockServerInitializer}), which is necessary |
| 64 | + * for non-RakNet transports. |
| 65 | + * |
| 66 | + * @param channel the connection channel |
| 67 | + * @return a peer wired to create Geyser sessions |
| 68 | + */ |
| 69 | + public BedrockPeer createPeer(Channel channel) { |
| 70 | + return new GeyserBedrockPeer(channel, this::createSession); |
| 71 | + } |
| 72 | + |
| 73 | + private BedrockSession createSession(BedrockPeer peer, int subClientId) { |
| 74 | + BedrockServerSession session = new BedrockServerSession(peer, subClientId); |
| 75 | + this.initializeSession(session); |
| 76 | + return session; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Wires a freshly created {@link BedrockServerSession} into Geyser: creates the {@link GeyserSession}, |
| 81 | + * installs the invalid-packet guard, and sets the upstream packet handler. |
| 82 | + * |
| 83 | + * @param bedrockServerSession the session to initialize |
| 84 | + */ |
| 85 | + public void initializeSession(@NonNull BedrockServerSession bedrockServerSession) { |
| 86 | + try { |
| 87 | + bedrockServerSession.setLogging(this.geyser.config().debugMode()); |
| 88 | + GeyserSession session = new GeyserSession(this.geyser, bedrockServerSession, this.eventLoopGroup.next()); |
| 89 | + |
| 90 | + if (!bedrockServerSession.isSubClient()) { |
| 91 | + Channel channel = bedrockServerSession.getPeer().getChannel(); |
| 92 | + channel.pipeline().addAfter(BedrockPacketCodec.NAME, InvalidPacketHandler.NAME, new InvalidPacketHandler(session)); |
| 93 | + } |
| 94 | + |
| 95 | + bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(this.geyser, session)); |
| 96 | + } catch (Throwable e) { |
| 97 | + // Error must be caught or it will be swallowed |
| 98 | + this.geyser.getLogger().error("Error occurred while initializing player!", e); |
| 99 | + bedrockServerSession.disconnect(e.getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments