From a80086e726131cee74de5e8fbddb1e28f070a67b Mon Sep 17 00:00:00 2001 From: atomicint Date: Mon, 13 Apr 2015 14:28:29 -0400 Subject: [PATCH] Assign the SESSION_KEY to the connecting channel rather than the channel handler context --- src/org/apollo/net/ApolloHandler.java | 9 +++-- src/org/apollo/net/session/LoginSession.java | 41 ++++++-------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/org/apollo/net/ApolloHandler.java b/src/org/apollo/net/ApolloHandler.java index f78d86a6..214860b1 100644 --- a/src/org/apollo/net/ApolloHandler.java +++ b/src/org/apollo/net/ApolloHandler.java @@ -68,11 +68,12 @@ public final class ApolloHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception { try { - Attribute attribute = ctx.attr(NetworkConstants.SESSION_KEY); + Channel channel = ctx.channel(); + Attribute attribute = channel.attr(NetworkConstants.SESSION_KEY); Session session = attribute.get(); if (message instanceof HttpRequest || message instanceof JagGrabRequest) { - session = new UpdateSession(ctx.channel(), serverContext); + session = new UpdateSession(channel, serverContext); } if (session != null) { @@ -86,11 +87,11 @@ public final class ApolloHandler extends ChannelInboundHandlerAdapter { switch (handshakeMessage.getServiceId()) { case HandshakeConstants.SERVICE_GAME: - attribute.set(new LoginSession(ctx, serverContext)); + attribute.set(new LoginSession(channel, serverContext)); break; case HandshakeConstants.SERVICE_UPDATE: - attribute.set(new UpdateSession(ctx.channel(), serverContext)); + attribute.set(new UpdateSession(channel, serverContext)); break; } } diff --git a/src/org/apollo/net/session/LoginSession.java b/src/org/apollo/net/session/LoginSession.java index 9a137fc0..1d92adf8 100644 --- a/src/org/apollo/net/session/LoginSession.java +++ b/src/org/apollo/net/session/LoginSession.java @@ -3,7 +3,6 @@ package org.apollo.net.session; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; -import io.netty.channel.ChannelHandlerContext; import java.io.IOException; import java.util.Optional; @@ -14,7 +13,6 @@ import org.apollo.game.model.World.RegistrationStatus; import org.apollo.game.model.entity.Player; import org.apollo.io.player.PlayerLoaderResponse; import org.apollo.login.LoginService; -import org.apollo.net.ApolloHandler; import org.apollo.net.NetworkConstants; import org.apollo.net.codec.game.GameMessageDecoder; import org.apollo.net.codec.game.GameMessageEncoder; @@ -33,26 +31,20 @@ import org.apollo.security.IsaacRandomPair; */ public final class LoginSession extends Session { - /** - * The context of the {@link ApolloHandler}. - */ - private final ChannelHandlerContext channelContext; - /** * The server context. */ - private final ServerContext serverContext; + private final ServerContext context; /** * Creates a login session for the specified channel. * - * @param ctx The context of the {@link ApolloHandler}. - * @param serverContext The server context. + * @param channel The channel. + * @param context The server context. */ - public LoginSession(ChannelHandlerContext ctx, ServerContext serverContext) { - super(ctx.channel()); - channelContext = ctx; - this.serverContext = serverContext; + public LoginSession(Channel channel, ServerContext context) { + super(channel); + this.context = context; } @Override @@ -60,15 +52,6 @@ public final class LoginSession extends Session { } - /** - * Gets the release. - * - * @return The release. - */ - public Release getRelease() { - return serverContext.getRelease(); - } - /** * Handles a login request. * @@ -76,7 +59,7 @@ public final class LoginSession extends Session { * @throws IOException If some I/O exception occurs. */ private void handleLoginRequest(LoginRequest request) throws IOException { - LoginService loginService = serverContext.getService(LoginService.class); + LoginService loginService = context.getService(LoginService.class); loginService.submitLoadRequest(this, request); } @@ -87,7 +70,7 @@ public final class LoginSession extends Session { * @param response The response. */ public void handlePlayerLoaderResponse(LoginRequest request, PlayerLoaderResponse response) { - GameService service = serverContext.getService(GameService.class); + GameService service = context.getService(GameService.class); Channel channel = getChannel(); Optional optional = response.getPlayer(); @@ -98,7 +81,7 @@ public final class LoginSession extends Session { Player player = optional.get(); rights = player.getPrivilegeLevel().toInteger(); - GameSession session = new GameSession(channel, serverContext, player); + GameSession session = new GameSession(channel, context, player); RegistrationStatus registration = service.registerPlayer(player, session); if (registration != RegistrationStatus.OK) { @@ -115,18 +98,18 @@ public final class LoginSession extends Session { if (optional.isPresent()) { IsaacRandomPair randomPair = request.getRandomPair(); - Release release = serverContext.getRelease(); + Release release = context.getRelease(); channel.pipeline().addFirst("messageEncoder", new GameMessageEncoder(release)); channel.pipeline().addBefore("messageEncoder", "gameEncoder", new GamePacketEncoder(randomPair.getEncodingRandom())); - channel.pipeline().addBefore("handler", "gameDecoder", new GamePacketDecoder(randomPair.getDecodingRandom(), serverContext.getRelease())); + channel.pipeline().addBefore("handler", "gameDecoder", new GamePacketDecoder(randomPair.getDecodingRandom(), context.getRelease())); channel.pipeline().addAfter("gameDecoder", "messageDecoder", new GameMessageDecoder(release)); channel.pipeline().remove("loginDecoder"); channel.pipeline().remove("loginEncoder"); - channelContext.attr(NetworkConstants.SESSION_KEY).set(optional.get().getSession()); + channel.attr(NetworkConstants.SESSION_KEY).set(optional.get().getSession()); } else { future.addListener(ChannelFutureListener.CLOSE); }