Пример #1
0
  public ByteBuf encode(NettyMessage msg) throws Exception {
    ByteBuf sendBuf = Unpooled.buffer();
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;
    byte[] keyArray = null;
    Object value = null;

    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
      key = param.getKey();
      keyArray = key.getBytes("UTF-8");
      sendBuf.writeInt(keyArray.length);
      sendBuf.writeBytes(keyArray);
      value = param.getValue();
      marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
      marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes());
    return sendBuf;
  }
Пример #2
0
  @Override
  protected void encode(ChannelHandlerContext ctx, TransportFrame frame, ByteBuf out)
      throws Exception {
    out.writeByte('E');
    out.writeByte('Q');
    out.writeInt(frame.version().id);
    out.writeLong(frame.request());

    if (frame instanceof StreamableTransportFrame) {
      // message request
      // skip size header
      int sizePos = out.writerIndex();
      out.writerIndex(out.writerIndex() + 4);
      try (StreamOutput output = streamService.output(out)) {
        Streamable message = ((StreamableTransportFrame) frame).message();
        output.writeClass(message.getClass());
        output.writeStreamable(message);
      }
      int size = out.writerIndex() - sizePos - 4;
      out.setInt(sizePos, size);
    } else {
      // ping request
      out.writeInt(0);
    }
  }
Пример #3
0
 private static void setLengthField(int version, ByteBuf buffer, int writerIndex, int length) {
   if (version < 3) {
     buffer.setShort(writerIndex, length);
   } else {
     buffer.setInt(writerIndex, length);
   }
 }
Пример #4
0
  @Test(timeout = 1000)
  public void a() {
    ByteBuf buf = Unpooled.buffer(4);
    buf.setInt(0, 1);
    System.out.format("%08x%n", buf.getInt(0));
    ByteBuf leBuf = buf.order(ByteOrder.LITTLE_ENDIAN);
    // 打印出 '01000000'
    System.out.format("%08x%n", leBuf.getInt(0));

    assert buf != leBuf;
    assert buf == buf.order(ByteOrder.BIG_ENDIAN);
  }
Пример #5
0
 @Override
 public ByteBuf setInt(int index, int value) {
   return buf.setInt(index, value);
 }
Пример #6
0
  @Override
  @SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
  public void rewriteClientbound(ByteBuf packet, int oldId, int newId) {
    super.rewriteClientbound(packet, oldId, newId);

    // Special cases
    int readerIndex = packet.readerIndex();
    int packetId = DefinedPacket.readVarInt(packet);
    int packetIdLength = packet.readerIndex() - readerIndex;
    if (packetId == 0x0D /* Collect Item */) {
      DefinedPacket.readVarInt(packet);
      rewriteVarInt(packet, oldId, newId, packet.readerIndex());
    } else if (packetId == 0x1B /* Attach Entity */) {
      rewriteInt(packet, oldId, newId, readerIndex + packetIdLength + 4);
    } else if (packetId == 0x13 /* Destroy Entities */) {
      int count = DefinedPacket.readVarInt(packet);
      int[] ids = new int[count];
      for (int i = 0; i < count; i++) {
        ids[i] = DefinedPacket.readVarInt(packet);
      }
      packet.readerIndex(readerIndex + packetIdLength);
      packet.writerIndex(readerIndex + packetIdLength);
      DefinedPacket.writeVarInt(count, packet);
      for (int id : ids) {
        if (id == oldId) {
          id = newId;
        } else if (id == newId) {
          id = oldId;
        }
        DefinedPacket.writeVarInt(id, packet);
      }
    } else if (packetId == 0x0E /* Spawn Object */) {

      DefinedPacket.readVarInt(packet);
      int type = packet.readUnsignedByte();

      if (type == 60 || type == 90) {
        packet.skipBytes(14);
        int position = packet.readerIndex();
        int readId = packet.readInt();
        int changedId = -1;
        if (readId == oldId) {
          packet.setInt(position, newId);
          changedId = newId;
        } else if (readId == newId) {
          packet.setInt(position, oldId);
          changedId = oldId;
        }
        if (changedId != -1) {
          if (changedId == 0 && readId != 0) { // Trim off the extra data
            packet.readerIndex(readerIndex);
            packet.writerIndex(packet.readableBytes() - 6);
          } else if (changedId != 0 && readId == 0) { // Add on the extra data
            packet.readerIndex(readerIndex);
            packet.capacity(packet.readableBytes() + 6);
            packet.writerIndex(packet.readableBytes() + 6);
          }
        }
      }
    } else if (packetId == 0x0C /* Spawn Player */) {
      DefinedPacket.readVarInt(packet); // Entity ID
      int idLength = packet.readerIndex() - readerIndex - packetIdLength;
      UUID uuid = DefinedPacket.readUUID(packet);
      ProxiedPlayer player;
      if ((player = BungeeCord.getInstance().getPlayerByOfflineUUID(uuid)) != null) {
        int previous = packet.writerIndex();
        packet.readerIndex(readerIndex);
        packet.writerIndex(readerIndex + packetIdLength + idLength);
        DefinedPacket.writeUUID(player.getUniqueId(), packet);
        packet.writerIndex(previous);
      }
    } else if (packetId == 0x42 /* Combat Event */) {
      int event = packet.readUnsignedByte();
      if (event == 1 /* End Combat*/) {
        DefinedPacket.readVarInt(packet);
        rewriteInt(packet, oldId, newId, packet.readerIndex());
      } else if (event == 2 /* Entity Dead */) {
        int position = packet.readerIndex();
        rewriteVarInt(packet, oldId, newId, packet.readerIndex());
        packet.readerIndex(position);
        DefinedPacket.readVarInt(packet);
        rewriteInt(packet, oldId, newId, packet.readerIndex());
      }
    }
    packet.readerIndex(readerIndex);
  }
Пример #7
0
 /**
  * Sets the {@code int} at position {@code pos} in the Buffer to the value {@code i}.
  *
  * <p>The buffer will expand as necessary to accommodate any value written.
  */
 public Buffer setInt(int pos, int i) {
   ensureWritable(pos, 4);
   buffer.setInt(pos, i);
   return this;
 }
 @Override
 public ByteBuf setInt(int var1, int var2) {
   return a.setInt(var1, var2);
 }