예제 #1
0
  public void handleGameRoomJoin(Player player, Channel channel, ChannelBuffer buffer) {
    String refKey = NettyUtils.readString(buffer);

    GameRoom gameRoom = lookupService.gameRoomLookup(refKey);
    if (null != gameRoom) {
      PlayerSession playerSession = gameRoom.createPlayerSession();
      playerSession.setConnectParameter(
          NettyUtils.NETTY_CHANNEL, channel); // TODO is this required?
      gameRoom.onLogin(playerSession);
      LOG.trace("Sending GAME_ROOM_JOIN_SUCCESS to channel {}", channel.getId());
      ChannelFuture future =
          channel.write(NettyUtils.createBufferForOpcode(Events.GAME_ROOM_JOIN_SUCCESS));
      connectToGameRoom(gameRoom, playerSession, future);
      loginUdp(playerSession, buffer);
    } else {
      // Write failure and close channel.
      ChannelFuture future =
          channel.write(NettyUtils.createBufferForOpcode(Events.GAME_ROOM_JOIN_FAILURE));
      future.addListener(ChannelFutureListener.CLOSE);
      LOG.error(
          "Invalid ref key provided by client: {}. Channel {} will be closed",
          refKey,
          channel.getId());
    }
  }
예제 #2
0
 public void handleLogin(Player player, Channel channel) {
   if (null != player) {
     channel.write(NettyUtils.createBufferForOpcode(Events.LOG_IN_SUCCESS));
   } else {
     // Write future and close channel
     closeChannelWithLoginFailure(channel);
   }
 }
  @Override
  public void applyProtocol(PlayerSession playerSession) {
    LOG.trace("Going to apply protocol on session: {}", playerSession);

    ChannelPipeline pipeline = NettyUtils.getPipeLineOfConnection(playerSession);
    NettyUtils.clearPipeline(pipeline);
    // Upstream handlers or encoders (i.e towards server) are added to
    // pipeline now.
    pipeline.addLast("lengthDecoder", createLengthBasedFrameDecoder());
    pipeline.addLast("messageBufferEventDecoder", messageBufferEventDecoder);
    pipeline.addLast("eventHandler", new DefaultToServerHandler(playerSession));

    // Downstream handlers - Filter for data which flows from server to
    // client. Note that the last handler added is actually the first
    // handler for outgoing data.
    pipeline.addLast("lengthFieldPrepender", lengthFieldPrepender);
    pipeline.addLast("messageBufferEventEncoder", messageBufferEventEncoder);
  }
예제 #4
0
 /**
  * Helper method which will close the channel after writing {@link Events#LOG_IN_FAILURE} to
  * remote connection.
  *
  * @param channel The tcp connection to remote machine that will be closed.
  */
 private void closeChannelWithLoginFailure(Channel channel) {
   ChannelFuture future = channel.write(NettyUtils.createBufferForOpcode(Events.LOG_IN_FAILURE));
   future.addListener(ChannelFutureListener.CLOSE);
 }
예제 #5
0
 /**
  * This method adds the player session to the {@link SessionRegistryService}. The key being the
  * remote udp address of the client and the session being the value.
  *
  * @param playerSession
  * @param buffer Used to read the remote address of the client which is attempting to connect via
  *     udp.
  */
 protected void loginUdp(PlayerSession playerSession, ChannelBuffer buffer) {
   InetSocketAddress remoteAdress = NettyUtils.readSocketAddress(buffer);
   if (null != remoteAdress) {
     sessionRegistryService.putSession(remoteAdress, playerSession);
   }
 }