Change handshaker API to take grpc_closure instead of grpc_iomgr_cb_func.

reviewable/pr8782/r1
Mark D. Roth 8 years ago
parent 27588bbfb9
commit 4b5cdb751e
  1. 11
      src/core/ext/client_channel/http_connect_handshaker.c
  2. 35
      src/core/lib/channel/handshaker.c
  3. 9
      src/core/lib/channel/handshaker.h

@ -55,7 +55,7 @@ typedef struct http_connect_handshaker {
// State saved while performing the handshake.
grpc_handshaker_args* args;
grpc_closure cb;
grpc_closure* on_handshake_done;
// Objects for processing the HTTP CONNECT request and response.
grpc_slice_buffer write_buffer;
@ -95,7 +95,8 @@ static void on_write_done(grpc_exec_ctx* exec_ctx, void* arg,
http_connect_handshaker* handshaker = arg;
if (error != GRPC_ERROR_NONE) {
// If the write failed, invoke the callback immediately with the error.
grpc_exec_ctx_sched(exec_ctx, &handshaker->cb, GRPC_ERROR_REF(error), NULL);
grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done,
GRPC_ERROR_REF(error), NULL);
} else {
// Otherwise, read the response.
grpc_endpoint_read(exec_ctx, handshaker->args->endpoint,
@ -172,7 +173,7 @@ static void on_read_done(grpc_exec_ctx* exec_ctx, void* arg,
}
done:
// Invoke handshake-done callback.
grpc_exec_ctx_sched(exec_ctx, &handshaker->cb, error, NULL);
grpc_exec_ctx_sched(exec_ctx, handshaker->on_handshake_done, error, NULL);
}
//
@ -191,11 +192,11 @@ static void http_connect_handshaker_shutdown(grpc_exec_ctx* exec_ctx,
static void http_connect_handshaker_do_handshake(
grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in,
gpr_timespec deadline, grpc_tcp_server_acceptor* acceptor,
grpc_iomgr_cb_func cb, grpc_handshaker_args* args) {
grpc_closure* on_handshake_done, grpc_handshaker_args* args) {
http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in;
// Save state in the handshaker object.
handshaker->args = args;
grpc_closure_init(&handshaker->cb, cb, args);
handshaker->on_handshake_done = on_handshake_done;
// Send HTTP CONNECT request.
gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s",
handshaker->server_name, handshaker->proxy_server);

@ -62,7 +62,7 @@ static void grpc_handshaker_do_handshake(grpc_exec_ctx* exec_ctx,
grpc_handshaker* handshaker,
gpr_timespec deadline,
grpc_tcp_server_acceptor* acceptor,
grpc_iomgr_cb_func on_handshake_done,
grpc_closure* on_handshake_done,
grpc_handshaker_args* args) {
handshaker->vtable->do_handshake(exec_ctx, handshaker, deadline, acceptor,
on_handshake_done, args);
@ -74,17 +74,16 @@ static void grpc_handshaker_do_handshake(grpc_exec_ctx* exec_ctx,
// State used while chaining handshakers.
struct grpc_handshaker_state {
// The index of the handshaker to invoke next.
// The index of the handshaker to invoke next and the closure to invoke it.
size_t index;
grpc_closure call_next_handshaker;
// The deadline for all handshakers.
gpr_timespec deadline;
// The acceptor to call the handshakers with.
grpc_tcp_server_acceptor* acceptor;
// The final callback and user_data to invoke after the last handshaker.
grpc_iomgr_cb_func on_handshake_done;
grpc_closure on_handshake_done;
void* user_data;
// Next closure to call.
grpc_closure next_cb;
};
struct grpc_handshake_manager {
@ -154,16 +153,14 @@ static void call_next_handshaker(grpc_exec_ctx* exec_ctx, void* arg,
// callback instead of chaining back to this function again.
if (error != GRPC_ERROR_NONE || mgr->state->index == mgr->count) {
args->user_data = mgr->state->user_data;
grpc_closure_init(&mgr->state->next_cb, mgr->state->on_handshake_done,
args);
grpc_exec_ctx_sched(exec_ctx, &mgr->state->next_cb, GRPC_ERROR_REF(error),
NULL);
grpc_exec_ctx_sched(exec_ctx, &mgr->state->on_handshake_done,
GRPC_ERROR_REF(error), NULL);
return;
}
// Call the next handshaker.
grpc_handshaker_do_handshake(
exec_ctx, mgr->handshakers[mgr->state->index], mgr->state->deadline,
mgr->state->acceptor, call_next_handshaker, args);
mgr->state->acceptor, &mgr->state->call_next_handshaker, args);
// If this is the last handshaker, clean up state.
if (mgr->state->index == mgr->count) {
gpr_free(mgr->state);
@ -178,14 +175,6 @@ void grpc_handshake_manager_do_handshake(
grpc_endpoint* endpoint, const grpc_channel_args* channel_args,
gpr_timespec deadline, grpc_tcp_server_acceptor* acceptor,
grpc_iomgr_cb_func on_handshake_done, void* user_data) {
// Construct state.
GPR_ASSERT(mgr->state == NULL);
mgr->state = gpr_malloc(sizeof(struct grpc_handshaker_state));
memset(mgr->state, 0, sizeof(*mgr->state));
mgr->state->deadline = deadline;
mgr->state->acceptor = acceptor;
mgr->state->on_handshake_done = on_handshake_done;
mgr->state->user_data = user_data;
// Construct handshaker args. These will be passed through all
// handshakers and eventually be freed by the final callback.
grpc_handshaker_args* args = gpr_malloc(sizeof(*args));
@ -193,10 +182,20 @@ void grpc_handshake_manager_do_handshake(
args->args = grpc_channel_args_copy(channel_args);
args->read_buffer = gpr_malloc(sizeof(*args->read_buffer));
grpc_slice_buffer_init(args->read_buffer);
// Construct state.
GPR_ASSERT(mgr->state == NULL);
mgr->state = gpr_malloc(sizeof(struct grpc_handshaker_state));
memset(mgr->state, 0, sizeof(*mgr->state));
grpc_closure_init(&mgr->state->call_next_handshaker, call_next_handshaker,
args);
mgr->state->deadline = deadline;
mgr->state->acceptor = acceptor;
grpc_closure_init(&mgr->state->on_handshake_done, on_handshake_done, args);
// While chaining between handshakers, we use args->user_data to
// store a pointer to the handshake manager. This will be
// changed to point to the caller-supplied user_data before calling
// the final callback.
args->user_data = mgr;
mgr->state->user_data = user_data;
call_next_handshaker(exec_ctx, args, GRPC_ERROR_NONE);
}

@ -75,15 +75,14 @@ typedef struct {
/// aborted in the middle).
void (*shutdown)(grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker);
/// Performs handshaking.
/// Takes ownership of \a args.
/// When finished, calls \a on_handshake_done with \a args, which the
/// callback takes ownership of.
/// Performs handshaking, modifying \a args as needed (e.g., to
/// replace \a endpoint with a wrapped endpoint).
/// When finished, invokes \a on_handshake_done.
/// \a acceptor will be NULL for client-side handshakers.
void (*do_handshake)(grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker,
gpr_timespec deadline,
grpc_tcp_server_acceptor* acceptor,
grpc_iomgr_cb_func on_handshake_done,
grpc_closure* on_handshake_done,
grpc_handshaker_args* args);
} grpc_handshaker_vtable;

Loading…
Cancel
Save