예제 #1
0
  private int msgEncodeLength(TaskMessage taskMsg) {
    if (taskMsg == null) return 0;

    int size = 6; // INT + SHORT
    if (taskMsg.message() != null) size += taskMsg.message().length;
    return size;
  }
예제 #2
0
  /**
   * write a TaskMessage into a stream
   *
   * <p>Each TaskMessage is encoded as: task ... short(2) len ... int(4) payload ... byte[] *
   */
  private void writeTaskMessage(ChannelBufferOutputStream bout, TaskMessage message)
      throws Exception {
    int payload_len = 0;
    if (message.message() != null) payload_len = message.message().length;

    int task_id = message.task();
    if (task_id > Short.MAX_VALUE)
      throw new RuntimeException("Task ID should not exceed " + Short.MAX_VALUE);

    bout.writeShort((short) task_id);
    bout.writeInt(payload_len);
    if (payload_len > 0) bout.write(message.message());
  }