Example #1
0
static void read_compressed_slice(grpc_compression_algorithm algorithm,
                                  int input_size) {
  gpr_slice input_slice;
  gpr_slice_buffer sliceb_in;
  gpr_slice_buffer sliceb_out;
  grpc_byte_buffer *buffer;
  grpc_byte_buffer_reader reader;
  gpr_slice read_slice;
  int read_count = 0;

  gpr_slice_buffer_init(&sliceb_in);
  gpr_slice_buffer_init(&sliceb_out);

  input_slice = gpr_slice_malloc(input_size);
  memset(GPR_SLICE_START_PTR(input_slice), 'a', input_size);
  gpr_slice_buffer_add(&sliceb_in, input_slice); /* takes ownership */
  GPR_ASSERT(grpc_msg_compress(algorithm, &sliceb_in, &sliceb_out));

  buffer = grpc_raw_compressed_byte_buffer_create(sliceb_out.slices,
                                                  sliceb_out.count, algorithm);
  grpc_byte_buffer_reader_init(&reader, buffer);

  while (grpc_byte_buffer_reader_next(&reader, &read_slice)) {
    GPR_ASSERT(memcmp(GPR_SLICE_START_PTR(read_slice),
                      GPR_SLICE_START_PTR(input_slice) + read_count,
                      GPR_SLICE_LENGTH(read_slice)) == 0);
    read_count += GPR_SLICE_LENGTH(read_slice);
    gpr_slice_unref(read_slice);
  }
  GPR_ASSERT(read_count == input_size);
  grpc_byte_buffer_reader_destroy(&reader);
  grpc_byte_buffer_destroy(buffer);
  gpr_slice_buffer_destroy(&sliceb_out);
  gpr_slice_buffer_destroy(&sliceb_in);
}
Example #2
0
grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb) {
  switch (bb->type) {
    case GRPC_BB_RAW:
      return grpc_raw_compressed_byte_buffer_create(
          bb->data.raw.slice_buffer.slices, bb->data.raw.slice_buffer.count,
          bb->data.raw.compression);
  }
  GPR_UNREACHABLE_CODE(return NULL);
}
Example #3
0
static void receiving_stream_ready(grpc_exec_ctx *exec_ctx, void *bctlp,
                                   int success) {
  batch_control *bctl = bctlp;
  grpc_call *call = bctl->call;

  if (call->receiving_stream == NULL) {
    *call->receiving_buffer = NULL;
    if (gpr_unref(&bctl->steps_to_complete)) {
      post_batch_completion(exec_ctx, bctl);
    }
  } else if (call->receiving_stream->length >
             grpc_channel_get_max_message_length(call->channel)) {
    cancel_with_status(exec_ctx, call, GRPC_STATUS_INTERNAL,
                       "Max message size exceeded");
    grpc_byte_stream_destroy(call->receiving_stream);
    call->receiving_stream = NULL;
    *call->receiving_buffer = NULL;
    if (gpr_unref(&bctl->steps_to_complete)) {
      post_batch_completion(exec_ctx, bctl);
    }
  } else {
    call->test_only_last_message_flags = call->receiving_stream->flags;
    if ((call->receiving_stream->flags & GRPC_WRITE_INTERNAL_COMPRESS) &&
        (call->compression_algorithm > GRPC_COMPRESS_NONE)) {
      *call->receiving_buffer = grpc_raw_compressed_byte_buffer_create(
          NULL, 0, call->compression_algorithm);
    } else {
      *call->receiving_buffer = grpc_raw_byte_buffer_create(NULL, 0);
    }
    grpc_closure_init(&call->receiving_slice_ready, receiving_slice_ready,
                      bctl);
    continue_receiving_slices(exec_ctx, bctl);
    /* early out */
    return;
  }
}
Example #4
0
grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices,
                                              size_t nslices) {
  return grpc_raw_compressed_byte_buffer_create(slices, nslices,
                                                GRPC_COMPRESS_NONE);
}