static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp, grpc_pollset *accepting_pollset, grpc_tcp_server_acceptor *acceptor) { gpr_free(acceptor); char *peer; char *last_colon; reconnect_server *server = (reconnect_server *)arg; gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); timestamp_list *new_tail; peer = grpc_endpoint_get_peer(tcp); grpc_endpoint_shutdown(exec_ctx, tcp, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Connected")); grpc_endpoint_destroy(exec_ctx, tcp); if (peer) { last_colon = strrchr(peer, ':'); if (server->peer == NULL) { server->peer = peer; } else { if (last_colon == NULL) { gpr_log(GPR_ERROR, "peer does not contain a ':'"); } else if (strncmp(server->peer, peer, (size_t)(last_colon - peer)) != 0) { gpr_log(GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer); } gpr_free(peer); } } new_tail = gpr_malloc(sizeof(timestamp_list)); new_tail->timestamp = now; new_tail->next = NULL; if (server->tail == NULL) { server->head = new_tail; server->tail = new_tail; } else { server->tail->next = new_tail; server->tail = new_tail; } pretty_print_backoffs(server); }
static void test_code(void) { /* iomgr.h */ grpc_iomgr_init(); grpc_iomgr_shutdown(); /* closure.h */ grpc_closure closure; closure.cb = NULL; closure.cb_arg = NULL; closure.next_data.scratch = 0; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; closure_list.head = NULL; closure_list.tail = NULL; grpc_closure_init(&closure, NULL, NULL); grpc_closure_create(NULL, NULL); grpc_closure_list_move(NULL, NULL); grpc_closure_list_append(NULL, NULL, GRPC_ERROR_CREATE("Foo")); grpc_closure_list_empty(closure_list); /* exec_ctx.h */ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_exec_ctx_flush(&exec_ctx); grpc_exec_ctx_finish(&exec_ctx); grpc_exec_ctx_sched(&exec_ctx, &closure, GRPC_ERROR_CREATE("Foo"), NULL); grpc_exec_ctx_enqueue_list(&exec_ctx, &closure_list, NULL); /* endpoint.h */ grpc_endpoint endpoint; grpc_endpoint_vtable vtable = {grpc_endpoint_read, grpc_endpoint_write, grpc_endpoint_get_workqueue, grpc_endpoint_add_to_pollset, grpc_endpoint_add_to_pollset_set, grpc_endpoint_shutdown, grpc_endpoint_destroy, grpc_endpoint_get_resource_user, grpc_endpoint_get_peer}; endpoint.vtable = &vtable; grpc_endpoint_read(&exec_ctx, &endpoint, NULL, NULL); grpc_endpoint_get_peer(&endpoint); grpc_endpoint_write(&exec_ctx, &endpoint, NULL, NULL); grpc_endpoint_shutdown(&exec_ctx, &endpoint); grpc_endpoint_destroy(&exec_ctx, &endpoint); grpc_endpoint_add_to_pollset(&exec_ctx, &endpoint, NULL); grpc_endpoint_add_to_pollset_set(&exec_ctx, &endpoint, NULL); /* executor.h */ grpc_executor_init(); grpc_executor_push(&closure, GRPC_ERROR_CREATE("Phi")); grpc_executor_shutdown(); /* pollset.h */ grpc_pollset_size(); grpc_pollset_init(NULL, NULL); grpc_pollset_shutdown(NULL, NULL, NULL); grpc_pollset_reset(NULL); grpc_pollset_destroy(NULL); GRPC_ERROR_UNREF(grpc_pollset_work(NULL, NULL, NULL, gpr_now(GPR_CLOCK_REALTIME), gpr_now(GPR_CLOCK_MONOTONIC))); GRPC_ERROR_UNREF(grpc_pollset_kick(NULL, NULL)); }
static char *endpoint_get_peer(grpc_endpoint *secure_ep) { secure_endpoint *ep = (secure_endpoint *)secure_ep; return grpc_endpoint_get_peer(ep->wrapped_ep); }
static void init_transport(grpc_chttp2_transport *t, const grpc_channel_args *channel_args, grpc_endpoint *ep, grpc_mdctx *mdctx, int is_client) { size_t i; int j; GPR_ASSERT(strlen(GRPC_CHTTP2_CLIENT_CONNECT_STRING) == GRPC_CHTTP2_CLIENT_CONNECT_STRLEN); memset(t, 0, sizeof(*t)); t->base.vtable = &vtable; t->ep = ep; /* one ref is for destroy, the other for when ep becomes NULL */ gpr_ref_init(&t->refs, 2); gpr_mu_init(&t->mu); grpc_mdctx_ref(mdctx); t->peer_string = grpc_endpoint_get_peer(ep); t->metadata_context = mdctx; t->endpoint_reading = 1; t->global.next_stream_id = is_client ? 1 : 2; t->global.is_client = is_client; t->global.outgoing_window = DEFAULT_WINDOW; t->global.incoming_window = DEFAULT_WINDOW; t->global.connection_window_target = DEFAULT_CONNECTION_WINDOW_TARGET; t->global.ping_counter = 1; t->global.pings.next = t->global.pings.prev = &t->global.pings; t->parsing.is_client = is_client; t->parsing.str_grpc_timeout = grpc_mdstr_from_string(t->metadata_context, "grpc-timeout", 0); t->parsing.deframe_state = is_client ? GRPC_DTS_FH_0 : GRPC_DTS_CLIENT_PREFIX_0; t->writing.is_client = is_client; grpc_connectivity_state_init(&t->channel_callback.state_tracker, GRPC_CHANNEL_READY, "transport"); gpr_slice_buffer_init(&t->global.qbuf); gpr_slice_buffer_init(&t->writing.outbuf); grpc_chttp2_hpack_compressor_init(&t->writing.hpack_compressor, mdctx); grpc_iomgr_closure_init(&t->writing_action, writing_action, t); grpc_iomgr_closure_init(&t->reading_action, reading_action, t); gpr_slice_buffer_init(&t->parsing.qbuf); grpc_chttp2_goaway_parser_init(&t->parsing.goaway_parser); grpc_chttp2_hpack_parser_init(&t->parsing.hpack_parser, t->metadata_context); if (is_client) { gpr_slice_buffer_add( &t->global.qbuf, gpr_slice_from_copied_string(GRPC_CHTTP2_CLIENT_CONNECT_STRING)); } /* 8 is a random stab in the dark as to a good initial size: it's small enough that it shouldn't waste memory for infrequently used connections, yet large enough that the exponential growth should happen nicely when it's needed. TODO(ctiller): tune this */ grpc_chttp2_stream_map_init(&t->parsing_stream_map, 8); grpc_chttp2_stream_map_init(&t->new_stream_map, 8); /* copy in initial settings to all setting sets */ for (i = 0; i < GRPC_CHTTP2_NUM_SETTINGS; i++) { t->parsing.settings[i] = grpc_chttp2_settings_parameters[i].default_value; for (j = 0; j < GRPC_NUM_SETTING_SETS; j++) { t->global.settings[j][i] = grpc_chttp2_settings_parameters[i].default_value; } } t->global.dirtied_local_settings = 1; /* Hack: it's common for implementations to assume 65536 bytes initial send window -- this should by rights be 0 */ t->global.force_send_settings = 1 << GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE; t->global.sent_local_settings = 0; /* configure http2 the way we like it */ if (is_client) { push_setting(t, GRPC_CHTTP2_SETTINGS_ENABLE_PUSH, 0); push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, 0); } push_setting(t, GRPC_CHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, DEFAULT_WINDOW); if (channel_args) { for (i = 0; i < channel_args->num_args; i++) { if (0 == strcmp(channel_args->args[i].key, GRPC_ARG_MAX_CONCURRENT_STREAMS)) { if (is_client) { gpr_log(GPR_ERROR, "%s: is ignored on the client", GRPC_ARG_MAX_CONCURRENT_STREAMS); } else if (channel_args->args[i].type != GRPC_ARG_INTEGER) { gpr_log(GPR_ERROR, "%s: must be an integer", GRPC_ARG_MAX_CONCURRENT_STREAMS); } else { push_setting(t, GRPC_CHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, channel_args->args[i].value.integer); } } else if (0 == strcmp(channel_args->args[i].key, GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER)) { if (channel_args->args[i].type != GRPC_ARG_INTEGER) { gpr_log(GPR_ERROR, "%s: must be an integer", GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER); } else if ((t->global.next_stream_id & 1) != (channel_args->args[i].value.integer & 1)) { gpr_log(GPR_ERROR, "%s: low bit must be %d on %s", GRPC_ARG_HTTP2_INITIAL_SEQUENCE_NUMBER, t->global.next_stream_id & 1, is_client ? "client" : "server"); } else { t->global.next_stream_id = channel_args->args[i].value.integer; } } } } }
static void http_connect_handshaker_do_handshake( grpc_exec_ctx* exec_ctx, grpc_handshaker* handshaker_in, grpc_tcp_server_acceptor* acceptor, grpc_closure* on_handshake_done, grpc_handshaker_args* args) { http_connect_handshaker* handshaker = (http_connect_handshaker*)handshaker_in; // Check for HTTP CONNECT channel arg. // If not found, invoke on_handshake_done without doing anything. const grpc_arg* arg = grpc_channel_args_find(args->args, GRPC_ARG_HTTP_CONNECT_SERVER); if (arg == NULL) { // Set shutdown to true so that subsequent calls to // http_connect_handshaker_shutdown() do nothing. gpr_mu_lock(&handshaker->mu); handshaker->shutdown = true; gpr_mu_unlock(&handshaker->mu); grpc_closure_sched(exec_ctx, on_handshake_done, GRPC_ERROR_NONE); return; } GPR_ASSERT(arg->type == GRPC_ARG_STRING); char* server_name = arg->value.string; // Get headers from channel args. arg = grpc_channel_args_find(args->args, GRPC_ARG_HTTP_CONNECT_HEADERS); grpc_http_header* headers = NULL; size_t num_headers = 0; char** header_strings = NULL; size_t num_header_strings = 0; if (arg != NULL) { GPR_ASSERT(arg->type == GRPC_ARG_STRING); gpr_string_split(arg->value.string, "\n", &header_strings, &num_header_strings); headers = gpr_malloc(sizeof(grpc_http_header) * num_header_strings); for (size_t i = 0; i < num_header_strings; ++i) { char* sep = strchr(header_strings[i], ':'); if (sep == NULL) { gpr_log(GPR_ERROR, "skipping unparseable HTTP CONNECT header: %s", header_strings[i]); continue; } *sep = '\0'; headers[num_headers].key = header_strings[i]; headers[num_headers].value = sep + 1; ++num_headers; } } // Save state in the handshaker object. gpr_mu_lock(&handshaker->mu); handshaker->args = args; handshaker->on_handshake_done = on_handshake_done; // Log connection via proxy. char* proxy_name = grpc_endpoint_get_peer(args->endpoint); gpr_log(GPR_INFO, "Connecting to server %s via HTTP proxy %s", server_name, proxy_name); gpr_free(proxy_name); // Construct HTTP CONNECT request. grpc_httpcli_request request; memset(&request, 0, sizeof(request)); request.host = server_name; request.http.method = "CONNECT"; request.http.path = server_name; request.http.hdrs = headers; request.http.hdr_count = num_headers; request.handshaker = &grpc_httpcli_plaintext; grpc_slice request_slice = grpc_httpcli_format_connect_request(&request); grpc_slice_buffer_add(&handshaker->write_buffer, request_slice); // Clean up. gpr_free(headers); for (size_t i = 0; i < num_header_strings; ++i) { gpr_free(header_strings[i]); } gpr_free(header_strings); // Take a new ref to be held by the write callback. gpr_ref(&handshaker->refcount); grpc_endpoint_write(exec_ctx, args->endpoint, &handshaker->write_buffer, &handshaker->request_done_closure); gpr_mu_unlock(&handshaker->mu); }
static void test_code(void) { /* iomgr.h */ grpc_iomgr_init(); grpc_iomgr_shutdown(); /* closure.h */ grpc_closure closure; closure.cb = NULL; closure.cb_arg = NULL; closure.final_data = 0; grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT; closure_list.head = NULL; closure_list.tail = NULL; grpc_closure_init(&closure, NULL, NULL); grpc_closure_create(NULL, NULL); grpc_closure_list_move(NULL, NULL); grpc_closure_list_add(NULL, NULL, true); bool x = grpc_closure_list_empty(closure_list); grpc_closure_next(&closure); /* exec_ctx.h */ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_exec_ctx_flush(&exec_ctx); grpc_exec_ctx_finish(&exec_ctx); grpc_exec_ctx_enqueue(&exec_ctx, &closure, x, NULL); grpc_exec_ctx_enqueue_list(&exec_ctx, &closure_list, NULL); /* endpoint.h */ grpc_endpoint endpoint; grpc_endpoint_vtable vtable = { grpc_endpoint_read, grpc_endpoint_write, grpc_endpoint_add_to_pollset, grpc_endpoint_add_to_pollset_set, grpc_endpoint_shutdown, grpc_endpoint_destroy, grpc_endpoint_get_peer}; endpoint.vtable = &vtable; grpc_endpoint_read(&exec_ctx, &endpoint, NULL, NULL); grpc_endpoint_get_peer(&endpoint); grpc_endpoint_write(&exec_ctx, &endpoint, NULL, NULL); grpc_endpoint_shutdown(&exec_ctx, &endpoint); grpc_endpoint_destroy(&exec_ctx, &endpoint); grpc_endpoint_add_to_pollset(&exec_ctx, &endpoint, NULL); grpc_endpoint_add_to_pollset_set(&exec_ctx, &endpoint, NULL); /* executor.h */ grpc_executor_init(); grpc_executor_enqueue(&closure, x); grpc_executor_shutdown(); /* pollset.h */ grpc_pollset_size(); grpc_pollset_init(NULL, NULL); grpc_pollset_shutdown(NULL, NULL, NULL); grpc_pollset_reset(NULL); grpc_pollset_destroy(NULL); grpc_pollset_work(NULL, NULL, NULL, gpr_now(GPR_CLOCK_REALTIME), gpr_now(GPR_CLOCK_MONOTONIC)); grpc_pollset_kick(NULL, NULL); }