Пример #1
0
/* grpc_run_batch_stack_build_result fills constructs a ruby BatchResult struct
   after the results have run */
static VALUE grpc_run_batch_stack_build_result(run_batch_stack *st) {
  size_t i = 0;
  VALUE result = rb_struct_new(grpc_rb_sBatchResult, Qnil, Qnil, Qnil, Qnil,
                               Qnil, Qnil, Qnil, Qnil, NULL);
  for (i = 0; i < st->op_num; i++) {
    switch (st->ops[i].op) {
      case GRPC_OP_SEND_INITIAL_METADATA:
        rb_struct_aset(result, sym_send_metadata, Qtrue);
        break;
      case GRPC_OP_SEND_MESSAGE:
        rb_struct_aset(result, sym_send_message, Qtrue);
        break;
      case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
        rb_struct_aset(result, sym_send_close, Qtrue);
        break;
      case GRPC_OP_SEND_STATUS_FROM_SERVER:
        rb_struct_aset(result, sym_send_status, Qtrue);
        break;
      case GRPC_OP_RECV_INITIAL_METADATA:
        rb_struct_aset(result, sym_metadata,
                       grpc_rb_md_ary_to_h(&st->recv_metadata));
      case GRPC_OP_RECV_MESSAGE:
        rb_struct_aset(result, sym_message,
                       grpc_rb_byte_buffer_to_s(st->recv_message));
        break;
      case GRPC_OP_RECV_STATUS_ON_CLIENT:
        rb_struct_aset(
            result, sym_status,
            rb_struct_new(
                grpc_rb_sStatus, UINT2NUM(st->recv_status),
                (GRPC_SLICE_START_PTR(st->recv_status_details) == NULL
                     ? Qnil
                     : grpc_rb_slice_to_ruby_string(st->recv_status_details)),
                grpc_rb_md_ary_to_h(&st->recv_trailing_metadata), NULL));
        break;
      case GRPC_OP_RECV_CLOSE_ON_SERVER:
        rb_struct_aset(result, sym_send_close, Qtrue);
        break;
      default:
        break;
    }
  }
  return result;
}
Пример #2
0
/**
 * Decodes protobuf message.
 *
 * buffer - pointer to the buffer with message data
 * buffer_size - size of the buffee
 *
 * Returns ruby array with decoded fields.
 *
 */
static VALUE decode_protobuf( uint8_t *buffer, long buffer_size )
{
    // Will return array of fields
    VALUE message = rb_ary_new();

    while ( buffer_size > 0 )
    {
        // New field
        VALUE field = rb_struct_new( rb_sProtobufCoderField, Qnil, Qnil, Qnil );

        // Read field tag
        uint64_t tag = read_varint( &buffer, &buffer_size );

        uint64_t field_number = tag >> 3;
        uint64_t wire_type    = tag & 0x07;

        rb_struct_aset( field, ID2SYM( rb_intern( "number" )), INT2FIX( field_number ));
        rb_struct_aset( field, ID2SYM( rb_intern( "type" )), INT2FIX( wire_type ));

        switch ( wire_type )
        {
            case WT_VARINT:
                // Read VARINT
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), INT2NUM( read_varint( &buffer, &buffer_size )));
                break;

            case WT_STRING:
                // First - read one VARINT with string length, then read "length" bytes
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), read_bytes( &buffer, &buffer_size, read_varint( &buffer, &buffer_size )));
                break;

            case WT_64BIT:
                // Read 8 bytes
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), read_bytes( &buffer, &buffer_size, 8 ));
                break;

            case WT_32BIT:
                // Read 4 bytes
                rb_struct_aset( field, ID2SYM( rb_intern( "value" )), read_bytes( &buffer, &buffer_size, 4 ));
                break;

            default:
                rb_raise( rb_eProtobufCoderDecodingError, "invalid wire type: %lld", wire_type );
        }

        rb_ary_push( message, field );
    }

    return message;
}
Пример #3
0
static VALUE struct_spec_rb_struct_aset(VALUE self, VALUE st, VALUE key, VALUE value) {
  return rb_struct_aset(st, key, value);
}