@Override
    public void write(ChannelBuffer bb, OFBsnGentableEntryAddVer13 message) {
      int startIndex = bb.writerIndex();
      // fixed value property version = 4
      bb.writeByte((byte) 0x4);
      // fixed value property type = 4
      bb.writeByte((byte) 0x4);
      // length is length of variable message, will be updated at the end
      int lengthIndex = bb.writerIndex();
      bb.writeShort(U16.t(0));

      bb.writeInt(U32.t(message.xid));
      // fixed value property experimenter = 0x5c16c7L
      bb.writeInt(0x5c16c7);
      // fixed value property subtype = 0x2eL
      bb.writeInt(0x2e);
      message.tableId.write2Bytes(bb);
      // keyLength is length indicator for key, will be
      // udpated when key has been written
      int keyLengthIndex = bb.writerIndex();
      bb.writeShort(0);
      message.checksum.write16Bytes(bb);
      int keyStartIndex = bb.writerIndex();
      ChannelUtils.writeList(bb, message.key);
      // update field length member keyLength
      int keyLength = bb.writerIndex() - keyStartIndex;
      bb.setShort(keyLengthIndex, keyLength);
      ChannelUtils.writeList(bb, message.value);

      // update length field
      int length = bb.writerIndex() - startIndex;
      bb.setShort(lengthIndex, length);
    }
예제 #2
0
  @Test
  public void iternext() {
    Iternext dut = new Iternext(transcoder, transcoder);

    ChannelBuffer request = ChannelBuffers.buffer(2);
    request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x51});
    ChannelBuffer actual = ChannelBuffers.buffer(request.capacity());
    dut.encode(actual);
    assertEquals(request, actual);

    ChannelBuffer response = ChannelBuffers.buffer(1 + 4 + value.length);
    assertFalse(dut.decode(response));

    response.writeByte(BinaryCommand.ESUCCESS);
    assertFalse(dut.decode(response));
    response.resetReaderIndex();

    response.writeInt(value.length);
    response.writeBytes(value);
    assertTrue(dut.decode(response));
    assertArrayEquals(value, (byte[]) dut.getReturnValue());

    // error
    response.clear();
    response.writeByte(BinaryCommand.EUNKNOWN);
    assertTrue(dut.decode(response));
    assertNull(dut.getReturnValue());
  }
 @Override
 public void write(ChannelBuffer bb, OFGroupFeaturesStatsReplyVer12 message) {
   // fixed value property version = 3
   bb.writeByte((byte) 0x3);
   // fixed value property type = 19
   bb.writeByte((byte) 0x13);
   // fixed value property length = 56
   bb.writeShort((short) 0x38);
   bb.writeInt(U32.t(message.xid));
   // fixed value property statsType = 8
   bb.writeShort((short) 0x8);
   OFStatsReplyFlagsSerializerVer12.writeTo(bb, message.flags);
   // pad: 4 bytes
   bb.writeZero(4);
   bb.writeInt(U32.t(message.types));
   bb.writeInt(U32.t(message.capabilities));
   bb.writeInt(U32.t(message.maxGroupsAll));
   bb.writeInt(U32.t(message.maxGroupsSelect));
   bb.writeInt(U32.t(message.maxGroupsIndirect));
   bb.writeInt(U32.t(message.maxGroupsFf));
   bb.writeInt(U32.t(message.actionsAll));
   bb.writeInt(U32.t(message.actionsSelect));
   bb.writeInt(U32.t(message.actionsIndirect));
   bb.writeInt(U32.t(message.actionsFf));
 }
예제 #4
0
 @Override
 public ChannelBuffer encode(StateChangeMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(3);
   buffer.writeByte(message.getState());
   buffer.writeByte(message.getGameMode());
   return buffer;
 }
 private void encodeBoolean(boolean value, ChannelBuffer out) {
   if (value) {
     out.writeByte((byte) ControlMessageProtocolConstant.TYPE_CHARACTER_BOOL_TRUE);
   } else {
     out.writeByte((byte) ControlMessageProtocolConstant.TYPE_CHARACTER_BOOL_FALSE);
   }
 }
 @Override
 public void writeTo(ChannelBuffer data) {
   super.writeTo(data);
   data.writeByte(this.virtualLanPriorityCodePoint);
   data.writeShort((short) 0);
   data.writeByte((byte) 0);
 }
    @Override
    public void write(ChannelBuffer bb, OFBsnPduTxRequestVer10 message) {
      int startIndex = bb.writerIndex();
      // fixed value property version = 1
      bb.writeByte((byte) 0x1);
      // fixed value property type = 4
      bb.writeByte((byte) 0x4);
      // length is length of variable message, will be updated at the end
      int lengthIndex = bb.writerIndex();
      bb.writeShort(U16.t(0));

      bb.writeInt(U32.t(message.xid));
      // fixed value property experimenter = 0x5c16c7L
      bb.writeInt(0x5c16c7);
      // fixed value property subtype = 0x1fL
      bb.writeInt(0x1f);
      bb.writeInt(U32.t(message.txIntervalMs));
      message.portNo.write2Bytes(bb);
      bb.writeByte(U8.t(message.slotNum));
      // pad: 3 bytes
      bb.writeZero(3);
      bb.writeBytes(message.data);

      // update length field
      int length = bb.writerIndex() - startIndex;
      bb.setShort(lengthIndex, length);
    }
  @Test
  public void setmst() {
    String host = "host";
    int port = 1978;
    long timestamp = System.currentTimeMillis();
    int opts = RDB.ROCHKCON;
    Setmst dut = new Setmst(host, port, timestamp, opts);

    ChannelBuffer request = ChannelBuffers.buffer(2 + 4 + 4 + 8 + 4 + host.getBytes().length);
    request.writeBytes(new byte[] {(byte) 0xC8, (byte) 0x78});
    request.writeInt(host.getBytes().length);
    request.writeInt(port);
    request.writeLong(timestamp);
    request.writeInt(opts);
    request.writeBytes(host.getBytes());
    ChannelBuffer actual = ChannelBuffers.buffer(request.capacity());
    dut.encode(actual);
    assertEquals(request, actual);

    ChannelBuffer response = ChannelBuffers.buffer(1);
    assertFalse(dut.decode(response));

    response.writeByte(Command.ESUCCESS);
    assertTrue(dut.decode(response));
    assertTrue(dut.getReturnValue());

    // error
    response.clear();
    response.writeByte(Command.EUNKNOWN);
    assertTrue(dut.decode(response));
    assertFalse(dut.getReturnValue());
  }
    @Override
    public void write(ChannelBuffer bb, OFBsnGentableBucketStatsReplyVer13 message) {
      int startIndex = bb.writerIndex();
      // fixed value property version = 4
      bb.writeByte((byte) 0x4);
      // fixed value property type = 19
      bb.writeByte((byte) 0x13);
      // length is length of variable message, will be updated at the end
      int lengthIndex = bb.writerIndex();
      bb.writeShort(U16.t(0));

      bb.writeInt(U32.t(message.xid));
      // fixed value property statsType = 65535
      bb.writeShort((short) 0xffff);
      OFStatsReplyFlagsSerializerVer13.writeTo(bb, message.flags);
      // pad: 4 bytes
      bb.writeZero(4);
      // fixed value property experimenter = 0x5c16c7L
      bb.writeInt(0x5c16c7);
      // fixed value property subtype = 0x5L
      bb.writeInt(0x5);
      ChannelUtils.writeList(bb, message.entries);

      // update length field
      int length = bb.writerIndex() - startIndex;
      bb.setShort(lengthIndex, length);
    }
예제 #10
0
 @Override
 public void writeBody(ChannelBuffer buffer)
     throws UnrecoverablePduException, RecoverablePduException {
   ChannelBufferUtil.writeNullTerminatedString(buffer, this.messageId);
   ChannelBufferUtil.writeNullTerminatedString(buffer, this.finalDate);
   buffer.writeByte(this.messageState);
   buffer.writeByte(this.errorCode);
 }
 private void encodeCollection(Collection<?> collection, ChannelBuffer out)
     throws ProtocolException {
   out.writeByte((byte) ControlMessageProtocolConstant.CONTROL_CHARACTER_LIST_START);
   for (Object element : collection) {
     encode(element, out);
   }
   out.writeByte((byte) ControlMessageProtocolConstant.CONTROL_CHARACTER_LIST_END);
 }
예제 #12
0
 @Override
 public ChannelBuffer encode(EntityRotationMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(6);
   buffer.writeInt(message.getId());
   buffer.writeByte(message.getRotation());
   buffer.writeByte(message.getPitch());
   return buffer;
 }
  private void encodeInt(int value, ChannelBuffer out) {
    out.writeByte((byte) ControlMessageProtocolConstant.TYPE_CHARACTER_INT);

    out.writeByte((byte) (value >> 24));
    out.writeByte((byte) (value >> 16));
    out.writeByte((byte) (value >> 8));
    out.writeByte((byte) (value));
  }
 private void encodeMap(Map<?, ?> map, ChannelBuffer out) throws ProtocolException {
   out.writeByte((byte) ControlMessageProtocolConstant.CONTROL_CHARACTER_MAP_START);
   for (Object element : map.entrySet()) {
     Map.Entry<?, ?> entry = (Map.Entry<?, ?>) element;
     encode(entry.getKey(), out);
     encode(entry.getValue(), out);
   }
   out.writeByte((byte) ControlMessageProtocolConstant.CONTROL_CHARACTER_MAP_END);
 }
예제 #15
0
 @Override
 public ChannelBuffer encode(OpenWindowMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
   buffer.writeByte(message.getId());
   buffer.writeByte(message.getType());
   ChannelBufferUtils.writeString(buffer, message.getTitle());
   buffer.writeByte(message.getSlots());
   return buffer;
 }
예제 #16
0
  @Override
  public ChannelBuffer encode(PlayerRotationMessage message) throws IOException {
    ChannelBuffer buffer = ChannelBuffers.buffer(3);

    buffer.writeByte(message.getPlayerId());
    buffer.writeByte((byte) ((int) (message.getYaw() * 256 / 360) & 255));
    buffer.writeByte((byte) ((int) (message.getPitch() * 256 / 360) & 255));

    return buffer;
  }
예제 #17
0
 @Override
 public ChannelBuffer encode(RespawnMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
   buffer.writeInt(message.getDimension());
   buffer.writeByte(message.getDifficulty());
   buffer.writeByte(message.getGameMode());
   buffer.writeShort(ChannelBufferUtils.getShifts(message.getWorldHeight()) - 1);
   ChannelBufferUtils.writeString(buffer, message.getWorldType());
   return buffer;
 }
예제 #18
0
 @Override
 public ChannelBuffer encode(UseBedMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(14);
   buffer.writeInt(message.getId());
   buffer.writeByte(message.getUsed());
   buffer.writeInt(message.getX());
   buffer.writeByte(message.getY());
   buffer.writeInt(message.getZ());
   return buffer;
 }
예제 #19
0
 @Override
 public ChannelBuffer encode(BlockActionMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(14);
   buffer.writeInt(message.getX());
   buffer.writeShort(message.getY());
   buffer.writeInt(message.getZ());
   buffer.writeByte(message.getFirstByte());
   buffer.writeByte(message.getSecondByte());
   buffer.writeShort(message.getBlockId());
   return buffer;
 }
예제 #20
0
 @Override
 protected void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception {
   HttpRequest request = (HttpRequest) message;
   buf.writeBytes(request.getMethod().toString().getBytes("ASCII"));
   buf.writeByte(SP);
   buf.writeBytes(request.getUri().getBytes("ASCII"));
   buf.writeByte(SP);
   buf.writeBytes(request.getProtocolVersion().toString().getBytes("ASCII"));
   buf.writeByte(CR);
   buf.writeByte(LF);
 }
  @Override
  protected Object decode(
      final ChannelHandlerContext ctx, final Channel channel, final ChannelBuffer buffer)
      throws Exception {

    while (buffer.readable()) {

      byte c = buffer.readByte();

      // check if last character read was DLE
      if (foundDLE) {
        foundDLE = false;

        if (c == DleStxEtxConstants.STX && !foundPacket) {

          foundPacket = true;

        } else if (c == DleStxEtxConstants.ETX && foundPacket) {

          ChannelBuffer packetRead = packet;
          resetDecodingState();
          return packetRead;

        } else if (c == DleStxEtxConstants.DLE && foundPacket) {

          // Stuffed DLE found
          packet.writeByte(DleStxEtxConstants.DLE);

        } else {

          if (log.isWarnEnabled()) {
            log.warn(
                "Incomplete packet received: {}",
                StringUtils.toHexString(
                    packet.array(), packet.readerIndex(), packet.readableBytes()));
          }
          resetDecodingState();
        }

      } else {

        if (c == DleStxEtxConstants.DLE) {
          // log.trace("Plain DLE received");
          foundDLE = true;
        } else if (foundPacket) {
          packet.writeByte(c);
        }
      }
    }

    // decoding is not yet complete, we'll need more bytes until we find DLE
    // ETX
    return null;
  }
 @Override
 public ChannelBuffer encode(RelativeEntityPositionRotationMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(9);
   buffer.writeInt(message.getId());
   buffer.writeByte(message.getDeltaX());
   buffer.writeByte(message.getDeltaY());
   buffer.writeByte(message.getDeltaZ());
   buffer.writeByte(message.getRotation());
   buffer.writeByte(message.getPitch());
   return buffer;
 }
예제 #23
0
 @Override
 protected void encodeInitialLine(final ChannelBuffer buf, final HttpMessage message)
     throws Exception {
   final MappingHttpRequest request = (MappingHttpRequest) message;
   buf.writeBytes(request.getMethod().toString().getBytes("ASCII"));
   buf.writeByte(HttpUtils.SP);
   buf.writeBytes(request.getServicePath().getBytes("ASCII"));
   buf.writeByte(HttpUtils.SP);
   buf.writeBytes(request.getProtocolVersion().toString().getBytes("ASCII"));
   buf.writeBytes(HttpUtils.CRLF);
 }
예제 #24
0
  /**
   * Writes a boolean back into the buffer.
   *
   * @param buffer The destination buffer
   * @param value The boolean to be encoded
   * @throws NullPointerException of <code>buffer</code> is <code>null</code>
   * @throws IndexOutOfBoundsException if not enough space is available in <code>buffer</code> to
   *     encode the whole <code>value</code>
   */
  public static void writeBoolean(ChannelBuffer buffer, boolean value)
      throws NullPointerException, IndexOutOfBoundsException {

    if (buffer == null) {
      throw new NullPointerException("buffer cannot be null");
    }

    buffer.writeByte(DT_BOOL);
    buffer.writeByte(FLAG_NONE);
    buffer.writeByte(value ? 1 : 0);
  }
예제 #25
0
 @Override
 public ChannelBuffer encode(EntityTeleportMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.buffer(18);
   buffer.writeInt(message.getEntityId());
   buffer.writeInt(message.getX());
   buffer.writeInt(message.getY());
   buffer.writeInt(message.getZ());
   buffer.writeByte(message.getRotation());
   buffer.writeByte(message.getPitch());
   return buffer;
 }
예제 #26
0
 @Override
 public ChannelBuffer encode(WindowClickMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
   buffer.writeByte(message.getWindowInstanceId());
   buffer.writeShort(message.getSlot());
   buffer.writeByte(message.isRightClick() ? 1 : 0);
   buffer.writeShort(message.getTransaction());
   buffer.writeByte(message.isShift() ? 1 : 0);
   ChannelBufferUtils.writeItemStack(buffer, message.getItem());
   return buffer;
 }
예제 #27
0
 @Override
 public ChannelBuffer encodeToServer(PlayerLoginRequestMessage message) {
   PlayerLoginRequestMessage server = message;
   ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
   buffer.writeInt(server.getId());
   ChannelBufferUtils.writeString(buffer, server.getWorldType());
   buffer.writeByte(server.getGameMode());
   buffer.writeByte(server.getDimension());
   buffer.writeByte(server.getDifficulty());
   buffer.writeByte(0);
   buffer.writeByte(server.getMaxPlayers());
   return buffer;
 }
  private void sendReply(Channel channel, ChannelBuffer data) {
    ChannelBuffer header = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, 16);
    header.writeBytes(ChannelBuffers.copiedBuffer(ByteOrder.LITTLE_ENDIAN, prefix, CHARSET));
    header.writeInt((int) deviceUniqueId);
    header.writeInt((int) serverId);
    header.writeShort(data.readableBytes());
    header.writeByte(checksum(data));
    header.writeByte(checksum(header));

    if (channel != null) {
      channel.write(ChannelBuffers.copiedBuffer(header, data));
    }
  }
예제 #29
0
  @Override
  public ChannelBuffer encode(PlayerSpawnMessage message) throws IOException {
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();

    buffer.writeByte(message.getPlayerId());
    ChannelBufferUtils.writeString(buffer, message.getName());
    buffer.writeShort((short) (message.getX() * 32));
    buffer.writeShort((short) (message.getY() * 32));
    buffer.writeShort((short) (message.getZ() * 32));
    buffer.writeByte((byte) ((int) (message.getYaw() * 256 / 360) & 255));
    buffer.writeByte((byte) ((int) (message.getPitch() * 256 / 360) & 255));

    return buffer;
  }
예제 #30
0
 @Override
 public ChannelBuffer encode(PlayerSpawnMessage message) throws IOException {
   ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
   buffer.writeInt(message.getEntityId());
   ChannelBufferUtils.writeString(buffer, message.getName());
   buffer.writeInt(message.getX());
   buffer.writeInt(message.getY());
   buffer.writeInt(message.getZ());
   buffer.writeByte(message.getYaw());
   buffer.writeByte(message.getPitch());
   buffer.writeShort(message.getId());
   ChannelBufferUtils.writeParameters(buffer, message.getParameters());
   return buffer;
 }