Assign the SESSION_KEY to the connecting channel rather than the channel handler context

This commit is contained in:
atomicint
2015-04-13 14:28:29 -04:00
parent aba1dffa0e
commit a80086e726
2 changed files with 17 additions and 33 deletions
+5 -4
View File
@@ -68,11 +68,12 @@ public final class ApolloHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
try { try {
Attribute<Session> attribute = ctx.attr(NetworkConstants.SESSION_KEY); Channel channel = ctx.channel();
Attribute<Session> attribute = channel.attr(NetworkConstants.SESSION_KEY);
Session session = attribute.get(); Session session = attribute.get();
if (message instanceof HttpRequest || message instanceof JagGrabRequest) { if (message instanceof HttpRequest || message instanceof JagGrabRequest) {
session = new UpdateSession(ctx.channel(), serverContext); session = new UpdateSession(channel, serverContext);
} }
if (session != null) { if (session != null) {
@@ -86,11 +87,11 @@ public final class ApolloHandler extends ChannelInboundHandlerAdapter {
switch (handshakeMessage.getServiceId()) { switch (handshakeMessage.getServiceId()) {
case HandshakeConstants.SERVICE_GAME: case HandshakeConstants.SERVICE_GAME:
attribute.set(new LoginSession(ctx, serverContext)); attribute.set(new LoginSession(channel, serverContext));
break; break;
case HandshakeConstants.SERVICE_UPDATE: case HandshakeConstants.SERVICE_UPDATE:
attribute.set(new UpdateSession(ctx.channel(), serverContext)); attribute.set(new UpdateSession(channel, serverContext));
break; break;
} }
} }
+12 -29
View File
@@ -3,7 +3,6 @@ package org.apollo.net.session;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import java.io.IOException; import java.io.IOException;
import java.util.Optional; 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.game.model.entity.Player;
import org.apollo.io.player.PlayerLoaderResponse; import org.apollo.io.player.PlayerLoaderResponse;
import org.apollo.login.LoginService; import org.apollo.login.LoginService;
import org.apollo.net.ApolloHandler;
import org.apollo.net.NetworkConstants; import org.apollo.net.NetworkConstants;
import org.apollo.net.codec.game.GameMessageDecoder; import org.apollo.net.codec.game.GameMessageDecoder;
import org.apollo.net.codec.game.GameMessageEncoder; import org.apollo.net.codec.game.GameMessageEncoder;
@@ -33,26 +31,20 @@ import org.apollo.security.IsaacRandomPair;
*/ */
public final class LoginSession extends Session { public final class LoginSession extends Session {
/**
* The context of the {@link ApolloHandler}.
*/
private final ChannelHandlerContext channelContext;
/** /**
* The server context. * The server context.
*/ */
private final ServerContext serverContext; private final ServerContext context;
/** /**
* Creates a login session for the specified channel. * Creates a login session for the specified channel.
* *
* @param ctx The context of the {@link ApolloHandler}. * @param channel The channel.
* @param serverContext The server context. * @param context The server context.
*/ */
public LoginSession(ChannelHandlerContext ctx, ServerContext serverContext) { public LoginSession(Channel channel, ServerContext context) {
super(ctx.channel()); super(channel);
channelContext = ctx; this.context = context;
this.serverContext = serverContext;
} }
@Override @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. * Handles a login request.
* *
@@ -76,7 +59,7 @@ public final class LoginSession extends Session {
* @throws IOException If some I/O exception occurs. * @throws IOException If some I/O exception occurs.
*/ */
private void handleLoginRequest(LoginRequest request) throws IOException { private void handleLoginRequest(LoginRequest request) throws IOException {
LoginService loginService = serverContext.getService(LoginService.class); LoginService loginService = context.getService(LoginService.class);
loginService.submitLoadRequest(this, request); loginService.submitLoadRequest(this, request);
} }
@@ -87,7 +70,7 @@ public final class LoginSession extends Session {
* @param response The response. * @param response The response.
*/ */
public void handlePlayerLoaderResponse(LoginRequest request, PlayerLoaderResponse response) { public void handlePlayerLoaderResponse(LoginRequest request, PlayerLoaderResponse response) {
GameService service = serverContext.getService(GameService.class); GameService service = context.getService(GameService.class);
Channel channel = getChannel(); Channel channel = getChannel();
Optional<Player> optional = response.getPlayer(); Optional<Player> optional = response.getPlayer();
@@ -98,7 +81,7 @@ public final class LoginSession extends Session {
Player player = optional.get(); Player player = optional.get();
rights = player.getPrivilegeLevel().toInteger(); rights = player.getPrivilegeLevel().toInteger();
GameSession session = new GameSession(channel, serverContext, player); GameSession session = new GameSession(channel, context, player);
RegistrationStatus registration = service.registerPlayer(player, session); RegistrationStatus registration = service.registerPlayer(player, session);
if (registration != RegistrationStatus.OK) { if (registration != RegistrationStatus.OK) {
@@ -115,18 +98,18 @@ public final class LoginSession extends Session {
if (optional.isPresent()) { if (optional.isPresent()) {
IsaacRandomPair randomPair = request.getRandomPair(); IsaacRandomPair randomPair = request.getRandomPair();
Release release = serverContext.getRelease(); Release release = context.getRelease();
channel.pipeline().addFirst("messageEncoder", new GameMessageEncoder(release)); channel.pipeline().addFirst("messageEncoder", new GameMessageEncoder(release));
channel.pipeline().addBefore("messageEncoder", "gameEncoder", new GamePacketEncoder(randomPair.getEncodingRandom())); 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().addAfter("gameDecoder", "messageDecoder", new GameMessageDecoder(release));
channel.pipeline().remove("loginDecoder"); channel.pipeline().remove("loginDecoder");
channel.pipeline().remove("loginEncoder"); channel.pipeline().remove("loginEncoder");
channelContext.attr(NetworkConstants.SESSION_KEY).set(optional.get().getSession()); channel.attr(NetworkConstants.SESSION_KEY).set(optional.get().getSession());
} else { } else {
future.addListener(ChannelFutureListener.CLOSE); future.addListener(ChannelFutureListener.CLOSE);
} }