Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create wrapper struct to hold proc #2

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/ruby/ext/grpc/rb_call_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ typedef struct callback_params {
grpc_credentials_plugin_metadata_cb callback;
} callback_params;

typedef struct grpc_rb_proc {
VALUE proc;
} grpc_rb_proc;

static VALUE grpc_rb_call_credentials_callback(VALUE callback_args) {
VALUE result = rb_hash_new();
VALUE metadata = rb_funcall(rb_ary_entry(callback_args, 0), rb_intern("call"),
Expand Down Expand Up @@ -125,7 +129,7 @@ static void grpc_rb_call_credentials_plugin_get_metadata(
void *state, grpc_auth_metadata_context context,
grpc_credentials_plugin_metadata_cb cb, void *user_data) {
callback_params *params = gpr_malloc(sizeof(callback_params));
params->get_metadata = (VALUE)state;
params->get_metadata = ((grpc_rb_proc*)state)->proc;
params->context = context;
params->user_data = user_data;
params->callback = cb;
Expand Down Expand Up @@ -242,17 +246,20 @@ static VALUE grpc_rb_call_credentials_init(VALUE self, VALUE proc) {
grpc_rb_call_credentials *wrapper = NULL;
grpc_call_credentials *creds = NULL;
grpc_metadata_credentials_plugin plugin;
grpc_rb_proc *proc_wrapper = gpr_malloc(sizeof(grpc_rb_proc));

TypedData_Get_Struct(self, grpc_rb_call_credentials,
&grpc_rb_call_credentials_data_type, wrapper);
proc_wrapper->proc = proc;
rb_gc_mark(proc_wrapper->proc);

plugin.get_metadata = grpc_rb_call_credentials_plugin_get_metadata;
plugin.destroy = grpc_rb_call_credentials_plugin_destroy;
if (!rb_obj_is_proc(proc)) {
rb_raise(rb_eTypeError, "Argument to CallCredentials#new must be a proc");
return Qnil;
}
plugin.state = (void*)proc;
plugin.state = (void*)proc_wrapper;
plugin.type = "";

creds = grpc_metadata_credentials_create_from_plugin(plugin, NULL);
Expand Down