Пример #1
0
 public void dispose() {
   if (!isDispose) {
     isDispose = true;
   }
   if (player != null) {
     if (player.getCurrentHall() != null) {
       player.getCurrentHall().kickPlayer(this);
     }
     player = null;
   }
   if (readBuffer != null) {
     BufferPool.getInstance().releaseBuffer(readBuffer);
     readBuffer = null;
   }
   if (channel != null) {
     try {
       channel.close();
     } catch (IOException e) {
       e.printStackTrace();
     } finally {
       channel = null;
     }
   }
 }
Пример #2
0
 public void startRecv() {
   if (channel.isOpen()) {
     readBuffer = BufferPool.getInstance().getBuffer();
     channel.read(readBuffer, this, AIOSocketMgr.getInstance().getReadHandler());
   }
 }
  private void requestLogicServerRoom(ByteBuffer buffer) {
    int length = Integer.MIN_VALUE;
    int type = Integer.MIN_VALUE;

    int roomType = Integer.MIN_VALUE;
    int roomId = Integer.MIN_VALUE;
    String roomTitle = null;
    int peopleCount = Integer.MIN_VALUE;
    String ownerGuid = null;
    List<String> playerList = new ArrayList<String>();
    Map<String, String> heroList = new HashMap<String, String>();
    Map<String, Integer> groupList = new HashMap<String, Integer>();
    Map<String, Integer> positionList = new HashMap<String, Integer>();
    String playerGuid = null;
    int playerGroup = Integer.MIN_VALUE;
    int playerPosition = Integer.MIN_VALUE;
    String heroCardId = null;
    byte[] dst;
    while (buffer.hasRemaining()) {
      length = buffer.getInt();
      type = buffer.get();
      if (type == EnumProtocol.TYPE_INT) {
        if (roomType == Integer.MIN_VALUE) {
          roomType = buffer.getInt();
        } else if (roomId == Integer.MIN_VALUE) {
          roomId = buffer.getInt();
        } else if (peopleCount == Integer.MIN_VALUE) {
          peopleCount = buffer.getInt();
        } else if (playerGroup == Integer.MIN_VALUE) {
          playerGroup = buffer.getInt();
        } else if (playerPosition == Integer.MIN_VALUE) {
          playerPosition = buffer.getInt();
        }
      } else if (type == EnumProtocol.TYPE_STRING) {
        if (roomTitle == null) {
          dst = new byte[length];
          buffer.get(dst);
          try {
            roomTitle = new String(dst, "UTF-8");
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
          }
        } else if (ownerGuid == null) {
          dst = new byte[length];
          buffer.get(dst);
          try {
            ownerGuid = new String(dst, "UTF-8");
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
          }
        } else if (heroList.size() < peopleCount) {
          if (playerGuid == null) {
            dst = new byte[length];
            buffer.get(dst);
            try {
              playerGuid = new String(dst, "UTF-8");
            } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
            }
          } else if (heroCardId == null) {
            dst = new byte[length];
            buffer.get(dst);
            try {
              heroCardId = new String(dst, "UTF-8");
            } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
            }
          }
        }

        if (playerGuid != null
            && playerGroup != Integer.MIN_VALUE
            && playerPosition != Integer.MIN_VALUE
            && heroCardId != null) {
          heroList.put(playerGuid, heroCardId);
          groupList.put(playerGuid, playerGroup);
          positionList.put(playerGuid, playerPosition);

          playerGuid = null;
          playerGroup = Integer.MIN_VALUE;
          playerPosition = Integer.MIN_VALUE;
          heroCardId = null;
        }
      }
    }

    if (roomType == 0) {
      BattleRoom room = BattleHall.getInstance().addRoom(roomId);
      if (room != null) {
        room.setTitle(roomTitle);
        room.setPeopleCount(peopleCount);
        room.initialize();
        Iterator<Entry<String, String>> it = heroList.entrySet().iterator();
        Entry<String, String> en;
        String guid;
        while (it.hasNext()) {
          en = it.next();
          guid = en.getKey();
          room.addHeroCardId(guid, en.getValue());
          room.addPlayerGuid(guid, groupList.get(guid));
          room.addPlayerPosition(guid, positionList.get(guid));
        }

        log.info("[RequestRoom] 房间创建成功,等待客户端连接, room id = " + roomId);
        // TODO 通知GameServer创建房间成功
        DatagramPacket p = DatagramPacketPool.getInstance().getObject();
        p.setSocketAddress(LogicServer.gameServerAdd);

        ByteBuffer bf = BufferPool.getInstance().getBuffer();

        bf.putShort(EnumProtocol.BASE_REQUEST_LOGIC_SERVER_ROOM_CONFIRM);

        bf.putInt(4);
        bf.put((byte) EnumProtocol.TYPE_INT);
        bf.putInt(roomType);

        bf.putInt(4);
        bf.put((byte) EnumProtocol.TYPE_INT);
        bf.putInt(roomId);

        bf.flip();

        byte[] dest = new byte[bf.remaining()];
        bf.get(dest, 0, dest.length);

        p.setData(dest);

        GameServerConnector.getInstance().send(p);
      } else {
        log.error("[RequestRoom] 创建房间失败, room id = " + roomId);
        // TODO 通知GameServer创建房间失败
      }
    }
  }