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); }
static PyObject *pygrpc_read_event_args(grpc_event *c_event) { if (c_event->data.read == NULL) { return PyTuple_Pack(7, read_event_kind, (PyObject *)c_event->tag, Py_None, Py_None, Py_None, Py_None, Py_None); } else { size_t length; size_t offset; grpc_byte_buffer_reader *reader; gpr_slice slice; char *c_bytes; PyObject *bytes; PyObject *event_args; length = grpc_byte_buffer_length(c_event->data.read); reader = grpc_byte_buffer_reader_create(c_event->data.read); c_bytes = gpr_malloc(length); offset = 0; while (grpc_byte_buffer_reader_next(reader, &slice)) { memcpy(c_bytes + offset, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); offset += GPR_SLICE_LENGTH(slice); } grpc_byte_buffer_reader_destroy(reader); bytes = PyBytes_FromStringAndSize(c_bytes, length); gpr_free(c_bytes); if (bytes == NULL) { return NULL; } event_args = PyTuple_Pack(7, read_event_kind, (PyObject *)c_event->tag, Py_None, Py_None, Py_None, bytes, Py_None); Py_DECREF(bytes); return event_args; } }
GPR_EXPORT intptr_t GPR_CALLTYPE grpcsharp_batch_context_recv_message_length( const grpcsharp_batch_context *ctx) { grpc_byte_buffer_reader reader; if (!ctx->recv_message) { return -1; } GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, ctx->recv_message)); intptr_t result = (intptr_t)grpc_byte_buffer_length(reader.buffer_out); grpc_byte_buffer_reader_destroy(&reader); return result; }
int byte_buffer_eq_string(grpc_byte_buffer *bb, const char *str) { grpc_byte_buffer_reader reader; grpc_byte_buffer *rbb; int res; grpc_byte_buffer_reader_init(&reader, bb); rbb = grpc_raw_byte_buffer_from_reader(&reader); res = byte_buffer_eq_slice(rbb, gpr_slice_from_copied_string(str)); grpc_byte_buffer_reader_destroy(&reader); grpc_byte_buffer_destroy(rbb); return res; }
int byte_buffer_eq_slice(grpc_byte_buffer *bb, gpr_slice b) { grpc_byte_buffer_reader reader; grpc_byte_buffer *rbb; int res; GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, bb) && "Couldn't init byte buffer reader"); rbb = grpc_raw_byte_buffer_from_reader(&reader); res = raw_byte_buffer_eq_slice(rbb, b); grpc_byte_buffer_reader_destroy(&reader); grpc_byte_buffer_destroy(rbb); return res; }
static void assert_read_ok(call_state *s, grpc_byte_buffer *b) { grpc_byte_buffer_reader *bb_reader = NULL; gpr_slice read_slice; unsigned i; bb_reader = grpc_byte_buffer_reader_create(b); while (grpc_byte_buffer_reader_next(bb_reader, &read_slice)) { for (i = 0; i < GPR_SLICE_LENGTH(read_slice); i++) { GPR_ASSERT(GPR_SLICE_START_PTR(read_slice)[i] == s->bytes_read % 256); s->bytes_read++; } gpr_slice_unref(read_slice); } grpc_byte_buffer_reader_destroy(bb_reader); }
/* * Copies data from recv_message to a buffer. Fatal error occurs if * buffer is too small. */ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_recv_message_to_buffer( const grpcsharp_batch_context *ctx, char *buffer, size_t buffer_len) { grpc_byte_buffer_reader reader; gpr_slice slice; size_t offset = 0; GPR_ASSERT(grpc_byte_buffer_reader_init(&reader, ctx->recv_message)); while (grpc_byte_buffer_reader_next(&reader, &slice)) { size_t len = GPR_SLICE_LENGTH(slice); GPR_ASSERT(offset + len <= buffer_len); memcpy(buffer + offset, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); offset += len; gpr_slice_unref(slice); } grpc_byte_buffer_reader_destroy(&reader); }
VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { VALUE rb_string; grpc_byte_buffer_reader reader; grpc_slice next; if (buffer == NULL) { return Qnil; } rb_string = rb_str_buf_new(grpc_byte_buffer_length(buffer)); if (!grpc_byte_buffer_reader_init(&reader, buffer)) { rb_raise(rb_eRuntimeError, "Error initializing byte buffer reader."); return Qnil; } while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { rb_str_cat(rb_string, (const char *) GRPC_SLICE_START_PTR(next), GRPC_SLICE_LENGTH(next)); grpc_slice_unref(next); } grpc_byte_buffer_reader_destroy(&reader); return rb_string; }
GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_reset(grpcsharp_batch_context* ctx) { grpcsharp_metadata_array_destroy_metadata_including_entries( &(ctx->send_initial_metadata)); grpc_byte_buffer_destroy(ctx->send_message); grpcsharp_metadata_array_destroy_metadata_including_entries( &(ctx->send_status_from_server.trailing_metadata)); grpcsharp_metadata_array_destroy_metadata_only(&(ctx->recv_initial_metadata)); if (ctx->recv_message_reader) { grpc_byte_buffer_reader_destroy(ctx->recv_message_reader); } grpc_byte_buffer_destroy(ctx->recv_message); grpcsharp_metadata_array_destroy_metadata_only( &(ctx->recv_status_on_client.trailing_metadata)); grpc_slice_unref(ctx->recv_status_on_client.status_details); memset(ctx, 0, sizeof(grpcsharp_batch_context)); }
static struct grpc_memory_counters send_snapshot_request(int call_idx, grpc_slice call_type) { grpc_metadata_array_init(&calls[call_idx].initial_metadata_recv); grpc_metadata_array_init(&calls[call_idx].trailing_metadata_recv); grpc_byte_buffer *response_payload_recv = NULL; memset(snapshot_ops, 0, sizeof(snapshot_ops)); op = snapshot_ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = GRPC_INITIAL_METADATA_WAIT_FOR_READY; op++; op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; op++; op->op = GRPC_OP_RECV_INITIAL_METADATA; op->data.recv_initial_metadata.recv_initial_metadata = &calls[call_idx].initial_metadata_recv; op++; op->op = GRPC_OP_RECV_MESSAGE; op->data.recv_message.recv_message = &response_payload_recv; op++; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; op->data.recv_status_on_client.trailing_metadata = &calls[call_idx].trailing_metadata_recv; op->data.recv_status_on_client.status = &calls[call_idx].status; op->data.recv_status_on_client.status_details = &calls[call_idx].details; op++; grpc_slice hostname = grpc_slice_from_static_string("localhost"); calls[call_idx].call = grpc_channel_create_call( channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, call_type, &hostname, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch( calls[call_idx].call, snapshot_ops, (size_t)(op - snapshot_ops), (void *)0, NULL)); grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); grpc_byte_buffer_reader reader; grpc_byte_buffer_reader_init(&reader, response_payload_recv); grpc_slice response = grpc_byte_buffer_reader_readall(&reader); struct grpc_memory_counters snapshot; snapshot.total_size_absolute = ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response)) ->total_size_absolute; snapshot.total_allocs_absolute = ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response)) ->total_allocs_absolute; snapshot.total_size_relative = ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response)) ->total_size_relative; snapshot.total_allocs_relative = ((struct grpc_memory_counters *)GRPC_SLICE_START_PTR(response)) ->total_allocs_relative; grpc_metadata_array_destroy(&calls[call_idx].initial_metadata_recv); grpc_metadata_array_destroy(&calls[call_idx].trailing_metadata_recv); grpc_slice_unref(response); grpc_byte_buffer_reader_destroy(&reader); grpc_byte_buffer_destroy(response_payload_recv); grpc_slice_unref(calls[call_idx].details); calls[call_idx].details = grpc_empty_slice(); grpc_call_destroy(calls[call_idx].call); calls[call_idx].call = NULL; return snapshot; }