예제 #1
0
  private void encodeCommand(@Nonnull AbstractCommand cmd) throws IOException {
    if (cmd.getId() != CommandList.CMD_KEEPALIVE) {
      log.debug("SND: {}", cmd);
    }

    buffer.clear();
    buffer.put((byte) cmd.getId());
    buffer.put((byte) (cmd.getId() ^ COMMAND_XOR_MASK));

    // keep some space for the length and the CRC
    int headerLenCRC = buffer.position();
    buffer.putShort((short) 0);
    buffer.putShort((short) 0);

    int startOfCmd = buffer.position();
    // encode command into net protocol
    cmd.encode(this);

    int length = buffer.position() - startOfCmd;
    buffer.flip();
    buffer.position(startOfCmd);
    int crc = NetComm.getCRC(buffer, length);
    buffer.position(headerLenCRC);
    buffer.putShort((short) length);
    buffer.putShort((short) crc);
    buffer.position(0);

    if (NetComm.isDumpingActive()) {
      NetComm.dump("snd => ", buffer);
      buffer.flip();
    }

    outChannel.write(buffer);
  }